use of io.vertx.core.file.FileProps in project georocket by georocket.
the class FileStore method getOne.
@Override
public void getOne(String path, Handler<AsyncResult<ChunkReadStream>> handler) {
String absolutePath = Paths.get(root, path).toString();
// check if chunk exists
FileSystem fs = vertx.fileSystem();
ObservableFuture<Boolean> observable = RxHelper.observableFuture();
fs.exists(absolutePath, observable.toHandler());
observable.flatMap(exists -> {
if (!exists) {
return Observable.error(new FileNotFoundException("Could not find chunk: " + path));
}
return Observable.just(exists);
}).flatMap(exists -> {
// get chunk's size
ObservableFuture<FileProps> propsObservable = RxHelper.observableFuture();
fs.props(absolutePath, propsObservable.toHandler());
return propsObservable;
}).map(props -> props.size()).flatMap(size -> {
// open chunk
ObservableFuture<AsyncFile> openObservable = RxHelper.observableFuture();
OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
fs.open(absolutePath, openOptions, openObservable.toHandler());
return openObservable.map(f -> new FileChunkReadStream(size, f));
}).subscribe(readStream -> {
// send chunk to peer
handler.handle(Future.succeededFuture(readStream));
}, err -> {
handler.handle(Future.failedFuture(err));
});
}
use of io.vertx.core.file.FileProps in project vert.x by eclipse.
the class CoreExamples method exampleFuture1.
public void exampleFuture1(Vertx vertx, Handler<HttpServerRequest> requestHandler) {
FileSystem fs = vertx.fileSystem();
Future<FileProps> future = fs.props("/my_file.txt");
future.onComplete((AsyncResult<FileProps> ar) -> {
if (ar.succeeded()) {
FileProps props = ar.result();
System.out.println("File size = " + props.size());
} else {
System.out.println("Failure: " + ar.cause().getMessage());
}
});
}
use of io.vertx.core.file.FileProps in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
String filename = "upload.txt";
FileSystem fs = vertx.fileSystem();
WebClient client = WebClient.create(vertx);
fs.props(filename, ares -> {
FileProps props = ares.result();
System.out.println("props is " + props);
long size = props.size();
HttpRequest<Buffer> req = client.put(8080, "localhost", "/");
req.putHeader("content-length", "" + size);
fs.open(filename, new OpenOptions(), ares2 -> {
req.sendStream(ares2.result(), ar -> {
if (ar.succeeded()) {
HttpResponse<Buffer> response = ar.result();
System.out.println("Got HTTP response with status " + response.statusCode());
} else {
ar.cause().printStackTrace();
}
});
});
});
}
Aggregations