public static Flowable<Buffer> toBufferFlow(io.vertx.reactivex.core.file.AsyncFile file) { return file.toFlowable() .map(io.vertx.reactivex.core.buffer.Buffer::getDelegate) .doOnTerminate(file::close) .doOnCancel(file::close); }
public static Flowable<Buffer> toBufferFlow(AsyncFile file) { return toBufferFlow(new io.vertx.reactivex.core.file.AsyncFile(file)); }
private void subscribe(byte[] expected, AsyncFile file, int times) { file.setReadPos(0); Flowable<Buffer> flowable = file.toFlowable().map(io.vertx.reactivex.core.buffer.Buffer::getDelegate); Buffer actual = Buffer.buffer(); flowable.subscribe( actual::appendBuffer, this::fail, () -> { assertEquals(Buffer.buffer(expected), actual); if (times > 0) { subscribe(expected, file, times - 1); } else { testComplete(); } }); }
@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()) ); }); }
/** * 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); }); }