Search in sources :

Example 6 with FileProps

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));
    });
}
Also used : PathUtils(io.georocket.util.PathUtils) AsyncFile(io.vertx.core.file.AsyncFile) OpenOptions(io.vertx.core.file.OpenOptions) ObservableFuture(io.vertx.rx.java.ObservableFuture) IndexedStore(io.georocket.storage.indexed.IndexedStore) Vertx(io.vertx.core.Vertx) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) FileProps(io.vertx.core.file.FileProps) Observable(rx.Observable) Buffer(io.vertx.core.buffer.Buffer) Paths(java.nio.file.Paths) FileSystem(io.vertx.core.file.FileSystem) RxHelper(io.vertx.rx.java.RxHelper) Preconditions(com.google.common.base.Preconditions) Queue(java.util.Queue) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ChunkReadStream(io.georocket.storage.ChunkReadStream) ConfigConstants(io.georocket.constants.ConfigConstants) OpenOptions(io.vertx.core.file.OpenOptions) ObservableFuture(io.vertx.rx.java.ObservableFuture) FileSystem(io.vertx.core.file.FileSystem) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with FileProps

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());
        }
    });
}
Also used : FileSystem(io.vertx.core.file.FileSystem) FileProps(io.vertx.core.file.FileProps)

Example 8 with FileProps

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();
                }
            });
        });
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) OpenOptions(io.vertx.core.file.OpenOptions) FileSystem(io.vertx.core.file.FileSystem) WebClient(io.vertx.ext.web.client.WebClient) FileProps(io.vertx.core.file.FileProps)

Aggregations

FileProps (io.vertx.core.file.FileProps)8 FileSystem (io.vertx.core.file.FileSystem)7 OpenOptions (io.vertx.core.file.OpenOptions)3 Buffer (io.vertx.core.buffer.Buffer)2 AsyncFile (io.vertx.core.file.AsyncFile)2 HttpServerRequest (io.vertx.core.http.HttpServerRequest)2 Preconditions (com.google.common.base.Preconditions)1 ConfigConstants (io.georocket.constants.ConfigConstants)1 ChunkReadStream (io.georocket.storage.ChunkReadStream)1 IndexedStore (io.georocket.storage.indexed.IndexedStore)1 PathUtils (io.georocket.util.PathUtils)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 io.vertx.core (io.vertx.core)1 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 HttpMethod (io.vertx.core.http.HttpMethod)1