use of io.vertx.rxjava.core.file.FileSystem in project georocket by georocket.
the class ElasticsearchInstaller method downloadNoCheck.
/**
* Download and extract Elasticsearch to a given location no matter if the
* destination folder exists or not.
* @param downloadUrl the URL to the ZIP file containing Elasticsearch
* @param destPath the path to the destination folder where the downloaded ZIP
* file should be extracted to
* @param strip <code>true</code> if the first path element of all items in the
* ZIP file should be stripped away.
* @return emitting the path to the extracted Elasticsearch
*/
private Single<String> downloadNoCheck(String downloadUrl, String destPath, boolean strip) {
log.info("Downloading Elasticsearch ...");
log.info("Source: " + downloadUrl);
log.info("Dest: " + destPath);
// download the archive, extract it and finally delete it
return downloadArchive(downloadUrl).flatMap(archivePath -> {
return extractArchive(archivePath, destPath, strip).doAfterTerminate(() -> {
FileSystem fs = vertx.fileSystem();
fs.deleteBlocking(archivePath);
});
});
}
use of io.vertx.rxjava.core.file.FileSystem in project georocket by georocket.
the class ElasticsearchInstaller method downloadArchive.
/**
* Download the ZIP file containing Elasticsearch to a temporary location
* @param downloadUrl the URL to the ZIP file
* @return a single emitting the location of the temporary file
*/
private Single<String> downloadArchive(String downloadUrl) {
// create temporary file
File archiveFile;
try {
archiveFile = File.createTempFile("elasticsearch", "tmp");
} catch (IOException e) {
return Single.error(e);
}
// open temporary file
FileSystem fs = vertx.fileSystem();
String archivePath = archiveFile.getAbsolutePath();
OpenOptions openOptions = new OpenOptions().setCreate(true).setWrite(true).setTruncateExisting(true);
return fs.rxOpen(archivePath, openOptions).flatMap(file -> {
return doDownload(downloadUrl, file).doAfterTerminate(() -> {
file.flush();
file.close();
}).map(v -> archivePath);
});
}
use of io.vertx.rxjava.core.file.FileSystem in project georocket by georocket.
the class ImporterVerticle method onImport.
/**
* Receives a name of a file to import
* @param msg the event bus message containing the filename
* @return a single that will emit an item when the file has
* been imported
*/
protected Single<Void> onImport(Message<JsonObject> msg) {
JsonObject body = msg.body();
String filename = body.getString("filename");
String filepath = incoming + "/" + filename;
String layer = body.getString("layer", "/");
String contentType = body.getString("contentType");
String correlationId = body.getString("correlationId");
String fallbackCRSString = body.getString("fallbackCRSString");
// get tags
JsonArray tagsArr = body.getJsonArray("tags");
List<String> tags = tagsArr != null ? tagsArr.stream().flatMap(o -> o != null ? Stream.of(o.toString()) : Stream.of()).collect(Collectors.toList()) : null;
// get properties
JsonObject propertiesObj = body.getJsonObject("properties");
Map<String, Object> properties = propertiesObj != null ? propertiesObj.getMap() : null;
// generate timestamp for this import
long timestamp = System.currentTimeMillis();
onImportingStarted(correlationId, filepath, contentType, layer, tagsArr, timestamp);
FileSystem fs = vertx.fileSystem();
OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
return fs.rxOpen(filepath, openOptions).flatMap(f -> importFile(contentType, f, correlationId, filename, timestamp, layer, tags, properties, fallbackCRSString).doAfterTerminate(() -> {
// delete file from 'incoming' folder
log.info("Deleting " + filepath + " from incoming folder");
f.rxClose().flatMap(v -> fs.rxDelete(filepath)).subscribe(v -> {
}, err -> {
log.error("Could not delete file from 'incoming' folder", err);
});
})).doOnSuccess(chunkCount -> {
onImportingFinished(correlationId, filepath, contentType, layer, chunkCount, System.currentTimeMillis() - timestamp, null);
}).doOnError(err -> {
onImportingFinished(correlationId, filepath, contentType, layer, null, System.currentTimeMillis() - timestamp, err);
}).map(count -> null);
}
use of io.vertx.rxjava.core.file.FileSystem in project georocket by georocket.
the class ImportCommand method importFile.
/**
* Upload a file to GeoRocket
* @param path path to file to import
* @param client the GeoRocket client
* @param vertx the Vert.x instance
* @return an observable that will emit when the file has been uploaded
*/
protected Observable<Void> importFile(String path, GeoRocketClient client, Vertx vertx) {
// open file
FileSystem fs = vertx.fileSystem();
OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
return fs.rxOpen(path, openOptions).flatMap(f -> fs.rxProps(path).map(props -> Pair.of(f, props.size()))).flatMapObservable(f -> {
ObservableFuture<Void> o = RxHelper.observableFuture();
Handler<AsyncResult<Void>> handler = o.toHandler();
AsyncFile file = f.getLeft().getDelegate();
WriteStream<Buffer> out = client.getStore().startImport(layer, tags, properties, Optional.of(f.getRight()), fallbackCRS, handler);
AtomicBoolean fileClosed = new AtomicBoolean();
Pump pump = Pump.pump(file, out);
file.endHandler(v -> {
file.close();
out.end();
fileClosed.set(true);
});
Handler<Throwable> exceptionHandler = t -> {
if (!fileClosed.get()) {
file.endHandler(null);
file.close();
}
handler.handle(Future.failedFuture(t));
};
file.exceptionHandler(exceptionHandler);
out.exceptionHandler(exceptionHandler);
pump.start();
return o;
});
}
Aggregations