@Override public Flowable<Buffer> read(String binaryUuid) { String path = getFilePath(binaryUuid); Flowable<Buffer> obs = FileSystem.newInstance(Mesh.vertx().fileSystem()) .rxOpen(path, new OpenOptions()) .toFlowable() .flatMap(RxUtil::toBufferFlow); return obs; }
@Override public Completable delete(String binaryUuid) { String path = getFilePath(binaryUuid); return FileSystem.newInstance(Mesh.vertx().fileSystem()) .rxDelete(path) // Don't fail if the file is not even in the local storage .onErrorComplete(e -> { Throwable cause = e.getCause(); if (cause != null) { return cause instanceof NoSuchFileException; } else { return e instanceof NoSuchFileException; } }); }
protected Single<TemplateVariants> loadVariants() { String path = name; int lastSlash = path.lastIndexOf('/'); String templateDir; String namePart; if(lastSlash != -1) { templateDir = path.substring(0, lastSlash); namePart = path.substring(lastSlash+1); } else { templateDir = ""; // current dir namePart = path; } FileSystem fs = AppGlobals.get().getVertx().fileSystem(); return fs.rxReadDir(templateDir) .map(list -> loadVariants(fs, templateDir, namePart, list)); }
private Single<SQLConnection> createTableIfNeeded(SQLConnection connection) { return vertx.fileSystem().rxReadFile("tables.sql") .map(Buffer::toString) .flatMapCompletable(connection::rxExecute) .toSingleDefault(connection); }
public FilesystemRepositoryConnectorProxyImpl(Vertx vertx, FilesystemRepositoryOptions configuration) { this.fileSystem = FileSystem.newInstance(vertx.fileSystem()); this.config = configuration; }
/** * Opens a file and reads its properties. * * @param path * Path to the file * @return */ public static Single<PropReadFileStream> openFile(Vertx vertx, String path) { FileSystem fs = vertx.fileSystem(); return Single.zip(fs.rxProps(path).map(props -> props.getDelegate()), fs.rxOpen(path, openOptions).map(file -> file.getDelegate()), (props, file) -> { return new PropReadFileStream(props, file, path); }); }
public Single<String> selectVariant(Request request){ return loadVariants() .flatMap(variants -> { // no variant if(variants.variants.isEmpty()) return Single.just(variants.defaultTemplate); Variant selectedVariant = request.selectVariant(new ArrayList<>(variants.variants.keySet())); // no acceptable variant if(selectedVariant == null) { // if it does not exist, that's special String template = variants.defaultTemplate; FileSystem fs = AppGlobals.get().getVertx().fileSystem(); return fs.rxExists(template) .map(exists -> { if(exists) return template; throw new WebApplicationException(Status.NOT_ACCEPTABLE); }); } return Single.just(variants.variants.get(selectedVariant)); }); }
@Test public void testAsyncFile() throws Exception { String fileName = "some-file.dat"; int chunkSize = 1000; int chunks = 10; byte[] expected = TestUtils.randomAlphaString(chunkSize * chunks).getBytes(); createFile(fileName, expected); vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), onSuccess(file -> subscribe(expected, file, 3))); await(); }
@Override public void process(ClientRequest request, Handler<AsyncResult<ClientResponse>> result) { final String localFilePath = config.getCatalogue() + StringUtils.stripStart(request.getPath(), "/"); final Optional<String> contentType = Optional .ofNullable(MimeMapping.getMimeTypeForFilename(localFilePath)); LOGGER.debug("Fetching file `{}` from local repository.", localFilePath); fileSystem.rxReadFile(localFilePath) .map(buffer -> new ClientResponse().setStatusCode(HttpResponseStatus.OK.code()) .setHeaders(headers(contentType)).setBody(buffer.getDelegate())) .subscribe( response -> result.handle(Future.succeededFuture(response)), error -> { LOGGER.error(ERROR_MESSAGE, error); result.handle(Future.succeededFuture(processError(error))); } ); }
public FilesystemRepositoryConnectorProxyImpl(Vertx vertx, FilesystemRepositoryOptions configuration) { this.fileSystem = FileSystem.newInstance(vertx.fileSystem()); this.config = configuration; }
public Single<String> selectVariant(Request request){ return loadVariants() .flatMap(variants -> { // no variant if(variants.variants.isEmpty()) return Single.just(variants.defaultTemplate); Variant selectedVariant = request.selectVariant(new ArrayList<>(variants.variants.keySet())); // no acceptable variant if(selectedVariant == null) { // if it does not exist, that's special String template = variants.defaultTemplate; FileSystem fs = AppGlobals.get().getVertx().fileSystem(); return fs.rxExists(template) .map(exists -> { if(exists) return template; throw new WebApplicationException(Status.NOT_ACCEPTABLE); }); } return Single.just(variants.variants.get(selectedVariant)); }); }
@Override public Completable store(Flowable<Buffer> stream, String uuid) { return Completable.defer(() -> { FileSystem fileSystem = FileSystem.newInstance(Mesh.vertx().fileSystem()); String path = getFilePath(uuid); log.debug("Saving data for field to path {" + path + "}"); MeshUploadOptions uploadOptions = Mesh.mesh().getOptions().getUploadOptions(); File uploadFolder = new File(uploadOptions.getDirectory(), getSegmentedPath(uuid)); if (!uploadFolder.exists()) { if (!uploadFolder.mkdirs()) { log.error("Failed to create target folder {" + uploadFolder.getAbsolutePath() + "}"); throw error(BAD_REQUEST, "node_error_upload_failed"); } if (log.isDebugEnabled()) { log.debug("Created folder {" + uploadFolder.getAbsolutePath() + "}"); } } File targetFile = new File(uploadFolder, uuid + ".bin"); return fileSystem.rxOpen(targetFile.getAbsolutePath(), new OpenOptions()).flatMapCompletable(file -> stream .map(io.vertx.reactivex.core.buffer.Buffer::new) .doOnNext(file::write) .ignoreElements() .andThen(file.rxFlush()) .andThen(file.rxClose()) .doOnError(err -> file.close()) ); }); }
@Override public void process(ClientRequest request, Handler<AsyncResult<ClientResponse>> result) { final String localFilePath = catalogue + StringUtils.stripStart(request.getPath(), "/"); final Optional<String> contentType = Optional .ofNullable(MimeMapping.getMimeTypeForFilename(localFilePath)); LOGGER.debug("Fetching file `{}` from local repository.", localFilePath); fileSystem.rxReadFile(localFilePath) .map(buffer -> new ClientResponse().setStatusCode(HttpResponseStatus.OK.code()) .setHeaders(headers(contentType)).setBody(buffer.getDelegate())) .subscribe( response -> result.handle(Future.succeededFuture(response)), error -> { LOGGER.error(ERROR_MESSAGE, error); result.handle(Future.succeededFuture(processError(error))); } ); }
public FilesystemRepositoryConnectorProxyImpl(Vertx vertx, JsonObject configuration) { this.fileSystem = FileSystem.newInstance(vertx.fileSystem()); this.catalogue = configuration.getString("catalogue"); }
private Single<JsonObject> loadConfig(JsonObject config) { if(config != null) { AppGlobals.get().setConfig(config); return Single.just(config); } String path = "conf/config.json"; return vertx.fileSystem().rxExists(path) .flatMap(exists -> { if(exists) { ConfigStoreOptions fileStore = new ConfigStoreOptions() .setType("file") .setConfig(new JsonObject().put("path", path)); ConfigRetrieverOptions configRetrieverOptions = new ConfigRetrieverOptions() .addStore(fileStore); ConfigRetriever retriever = ConfigRetriever.create(vertx, configRetrieverOptions); return retriever.rxGetConfig().map(loadedConfig -> { AppGlobals.get().setConfig(loadedConfig); return loadedConfig; }); } else { // empty config JsonObject emptyConfig = new JsonObject(); AppGlobals.get().setConfig(emptyConfig); return Single.just(emptyConfig); } }); }
protected Single<TemplateVariants> loadVariants() { String path = name; int lastSlash = path.lastIndexOf('/'); String templateDir; String namePart; if(lastSlash != -1) { templateDir = path.substring(0, lastSlash); namePart = path.substring(lastSlash+1); } else { templateDir = ""; // current dir namePart = path; } FileSystem fs = AppGlobals.get().getVertx().fileSystem(); return fs.rxReadDir(templateDir) .map(list -> loadVariants(fs, templateDir, namePart, list)); }
@Override public void process(ClientRequest request, Handler<AsyncResult<ClientResponse>> result) { final String localFilePath = config.getCatalogue() + StringUtils.stripStart(request.getPath(), "/"); final Optional<String> contentType = Optional .ofNullable(MimeMapping.getMimeTypeForFilename(localFilePath)); LOGGER.debug("Fetching file `{}` from local repository.", localFilePath); fileSystem.rxReadFile(localFilePath) .map(buffer -> new ClientResponse().setStatusCode(HttpResponseStatus.OK.code()) .setHeaders(headers(contentType)).setBody(buffer.getDelegate())) .subscribe( response -> result.handle(Future.succeededFuture(response)), error -> { LOGGER.error(ERROR_MESSAGE, error); result.handle(Future.succeededFuture(processError(error))); } ); }
protected Single<JsonObject> loadConfig(JsonObject config) { if(config != null) { AppGlobals.get().setConfig(config); return Single.just(config); } String path = "conf/config.json"; return vertx.fileSystem().rxExists(path) .flatMap(exists -> { if(exists) { ConfigStoreOptions fileStore = new ConfigStoreOptions() .setType("file") .setConfig(new JsonObject().put("path", path)); ConfigRetrieverOptions configRetrieverOptions = new ConfigRetrieverOptions() .addStore(fileStore); ConfigRetriever retriever = ConfigRetriever.create(vertx, configRetrieverOptions); return retriever.rxGetConfig().map(loadedConfig -> { AppGlobals.get().setConfig(loadedConfig); return loadedConfig; }); } else { // empty config JsonObject emptyConfig = new JsonObject(); AppGlobals.get().setConfig(emptyConfig); return Single.just(emptyConfig); } }); }