Search in sources :

Example 31 with FileSystem

use of io.vertx.core.file.FileSystem in project vertx-web by vert-x3.

the class StaticHandlerImpl method isFileExisting.

private synchronized void isFileExisting(RoutingContext context, String file, Handler<AsyncResult<Boolean>> resultHandler) {
    FileSystem fs = context.vertx().fileSystem();
    wrapInTCCLSwitch(() -> fs.exists(file, resultHandler));
}
Also used : FileSystem(io.vertx.core.file.FileSystem)

Example 32 with FileSystem

use of io.vertx.core.file.FileSystem 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 33 with FileSystem

use of io.vertx.core.file.FileSystem in project georocket by georocket.

the class FileStore method doDeleteChunks.

@Override
protected void doDeleteChunks(Queue<String> paths, Handler<AsyncResult<Void>> handler) {
    if (paths.isEmpty()) {
        handler.handle(Future.succeededFuture());
        return;
    }
    String path = paths.poll();
    FileSystem fs = vertx.fileSystem();
    String absolutePath = Paths.get(root, path).toString();
    fs.exists(absolutePath, existAr -> {
        if (existAr.failed()) {
            handler.handle(Future.failedFuture(existAr.cause()));
        } else {
            if (existAr.result()) {
                fs.delete(absolutePath, deleteAr -> {
                    if (deleteAr.failed()) {
                        handler.handle(Future.failedFuture(deleteAr.cause()));
                    } else {
                        doDeleteChunks(paths, handler);
                    }
                });
            } else {
                doDeleteChunks(paths, handler);
            }
        }
    });
}
Also used : FileSystem(io.vertx.core.file.FileSystem)

Example 34 with FileSystem

use of io.vertx.core.file.FileSystem in project vertx-zero by silentbalanceyh.

the class FileResolver method resolve.

@Override
public Epsilon<T> resolve(final RoutingContext context, final Epsilon<T> income) {
    final Set<FileUpload> fileUploads = context.fileUploads();
    if (Values.ONE == fileUploads.size()) {
        final FileUpload fileUpload = fileUploads.iterator().next();
        // Returned directly reference for FileUpload
        if (income.getArgType().isAssignableFrom(FileUpload.class)) {
            income.setValue((T) fileUpload);
        } else if (income.getArgType() == File.class) {
            // File object construction
            final Object ret = ZeroSerializer.getValue(income.getArgType(), fileUpload.uploadedFileName());
            income.setValue((T) ret);
        } else if (income.getArgType().isArray()) {
            // byte[]
            if (byte.class == income.getArgType().getComponentType() || Byte.class == income.getArgType().getComponentType()) {
                final FileSystem fileSystem = context.vertx().fileSystem();
                final Buffer buffer = fileSystem.readFileBlocking(fileUpload.uploadedFileName());
                income.setValue((T) buffer.getBytes());
            }
        } else if (Buffer.class.isAssignableFrom(income.getArgType())) {
            // Buffer
            final FileSystem fileSystem = context.vertx().fileSystem();
            final Buffer buffer = fileSystem.readFileBlocking(fileUpload.uploadedFileName());
            income.setValue((T) buffer);
        }
    } else {
        // Multi Files only support Set<FileUpload>
        income.setValue((T) fileUploads);
    }
    return income;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) File(java.io.File) FileUpload(io.vertx.ext.web.FileUpload)

Example 35 with FileSystem

use of io.vertx.core.file.FileSystem in project vert.x by eclipse.

the class CoreExamples method exampleFutureComposition1.

public void exampleFutureComposition1(Vertx vertx) {
    FileSystem fs = vertx.fileSystem();
    Future<Void> future = fs.createFile("/foo").compose(v -> {
        // When the file is created (fut1), execute this:
        return fs.writeFile("/foo", Buffer.buffer());
    }).compose(v -> {
        // When the file is written (fut2), execute this:
        return fs.move("/foo", "/bar");
    });
}
Also used : Logger(io.vertx.core.impl.logging.Logger) AsyncFile(io.vertx.core.file.AsyncFile) Arrays(java.util.Arrays) OpenOptions(io.vertx.core.file.OpenOptions) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) io.vertx.core.http(io.vertx.core.http) FileProps(io.vertx.core.file.FileProps) TimeUnit(java.util.concurrent.TimeUnit) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) FileSystem(io.vertx.core.file.FileSystem) JsonObject(io.vertx.core.json.JsonObject) NetClient(io.vertx.core.net.NetClient) SocketAddress(io.vertx.core.net.SocketAddress) FileSystem(io.vertx.core.file.FileSystem)

Aggregations

FileSystem (io.vertx.core.file.FileSystem)44 Buffer (io.vertx.core.buffer.Buffer)22 Vertx (io.vertx.core.Vertx)21 JsonObject (io.vertx.core.json.JsonObject)21 Future (io.vertx.core.Future)19 Handler (io.vertx.core.Handler)19 Context (io.vertx.core.Context)18 EventBus (io.vertx.core.eventbus.EventBus)18 TestContext (io.vertx.ext.unit.TestContext)18 Before (org.junit.Before)18 Test (org.junit.Test)17 Async (io.vertx.ext.unit.Async)16 StandardCharsets (java.nio.charset.StandardCharsets)16 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)15 HttpURLConnection (java.net.HttpURLConnection)15 Constants (org.eclipse.hono.util.Constants)15 CoreMatchers.is (org.hamcrest.CoreMatchers.is)15 Assert.assertThat (org.junit.Assert.assertThat)15 RunWith (org.junit.runner.RunWith)15 Mockito (org.mockito.Mockito)15