Example usage for io.vertx.core.http HttpServerFileUpload name

List of usage examples for io.vertx.core.http HttpServerFileUpload name

Introduction

In this page you can find the example usage for io.vertx.core.http HttpServerFileUpload name.

Prototype

String name();

Source Link

Usage

From source file:fr.wseduc.rack.controllers.RackController.java

License:Open Source License

private Handler<HttpServerFileUpload> getUploadHandler(final Buffer fileBuffer, final JsonObject metadata,
        final HttpServerRequest request) {
    return new Handler<HttpServerFileUpload>() {
        @Override//from w ww  .jav a2  s. c o  m
        public void handle(final HttpServerFileUpload upload) {
            upload.handler(new Handler<Buffer>() {
                @Override
                public void handle(Buffer data) {
                    fileBuffer.appendBuffer(data);
                }
            });
            upload.endHandler(new Handler<Void>() {
                @Override
                public void handle(Void v) {
                    metadata.put("name", upload.name());
                    metadata.put("filename", upload.filename());
                    metadata.put("content-type", upload.contentType());
                    metadata.put("content-transfer-encoding", upload.contentTransferEncoding());
                    metadata.put("charset", upload.charset());
                    metadata.put("size", upload.size());
                    if (metadata.getLong("size", 0l).equals(0l)) {
                        metadata.put("size", fileBuffer.length());
                    }

                    if (storage.getValidator() != null) {
                        request.pause();
                        storage.getValidator().process(metadata, new JsonObject(),
                                new Handler<AsyncResult<Void>>() {
                                    @Override
                                    public void handle(AsyncResult<Void> voidAsyncResult) {
                                        if (voidAsyncResult.succeeded()) {
                                            request.resume();
                                        } else {
                                            badRequest(request, voidAsyncResult.cause().getMessage());
                                            return;
                                        }
                                    }
                                });
                    }
                }
            });
        }
    };
}

From source file:org.entcore.common.storage.impl.GridfsStorage.java

License:Open Source License

public static JsonObject metadata(HttpServerFileUpload upload) {
    JsonObject metadata = new JsonObject();
    metadata.put("name", upload.name());
    metadata.put("filename", upload.filename());
    metadata.put("content-type", upload.contentType());
    metadata.put("content-transfer-encoding", upload.contentTransferEncoding());
    metadata.put("charset", upload.charset());
    metadata.put("size", upload.size());
    return metadata;
}

From source file:org.entcore.directory.controllers.ImportController.java

License:Open Source License

private void uploadImport(final HttpServerRequest request, final Handler<AsyncResult<ImportInfos>> handler) {
    request.pause();/*w ww .  ja v  a  2  s . c  o m*/
    final String importId = UUID.randomUUID().toString();
    final String path = config.getString("wizard-path", "/tmp") + File.separator + importId;
    request.setExpectMultipart(true);
    request.endHandler(new Handler<Void>() {
        @Override
        public void handle(Void v) {
            final ImportInfos importInfos = new ImportInfos();
            importInfos.setId(importId);
            importInfos.setPath(path);
            importInfos.setStructureId(request.formAttributes().get("structureId"));
            importInfos.setStructureExternalId(request.formAttributes().get("structureExternalId"));
            importInfos.setPreDelete(paramToBoolean(request.formAttributes().get("predelete")));
            importInfos.setTransition(paramToBoolean(request.formAttributes().get("transition")));
            importInfos.setStructureName(request.formAttributes().get("structureName"));
            importInfos.setUAI(request.formAttributes().get("UAI"));
            importInfos.setLanguage(I18n.acceptLanguage(request));
            if (isNotEmpty(request.formAttributes().get("classExternalId"))) {
                importInfos.setOverrideClass(request.formAttributes().get("classExternalId"));
            }
            try {
                importInfos.setFeeder(request.formAttributes().get("type"));
            } catch (IllegalArgumentException | NullPointerException e) {
                handler.handle(
                        new DefaultAsyncResult<ImportInfos>(new ImportException("invalid.import.type", e)));
                deleteImportPath(vertx, path);
                return;
            }
            UserUtils.getUserInfos(eb, request, new Handler<UserInfos>() {
                @Override
                public void handle(UserInfos user) {
                    if (user == null) {
                        handler.handle(
                                new DefaultAsyncResult<ImportInfos>(new ImportException("invalid.admin")));
                        deleteImportPath(vertx, path);
                        return;
                    }
                    importInfos.validate(
                            user.getFunctions() != null
                                    && user.getFunctions().containsKey(DefaultFunctions.SUPER_ADMIN),
                            vertx, new Handler<AsyncResult<String>>() {
                                @Override
                                public void handle(AsyncResult<String> validate) {
                                    if (validate.succeeded()) {
                                        if (validate.result() == null) {
                                            handler.handle(new DefaultAsyncResult<>(importInfos));
                                        } else {
                                            handler.handle(new DefaultAsyncResult<ImportInfos>(
                                                    new ImportException(validate.result())));
                                            deleteImportPath(vertx, path);
                                        }
                                    } else {
                                        handler.handle(new DefaultAsyncResult<ImportInfos>(validate.cause()));
                                        log.error("Validate error", validate.cause());
                                        deleteImportPath(vertx, path);
                                    }
                                }
                            });
                }
            });
        }
    });
    request.exceptionHandler(new Handler<Throwable>() {
        @Override
        public void handle(Throwable event) {
            handler.handle(new DefaultAsyncResult<ImportInfos>(event));
            deleteImportPath(vertx, path);
        }
    });
    request.uploadHandler(new Handler<HttpServerFileUpload>() {
        @Override
        public void handle(final HttpServerFileUpload upload) {
            if (!upload.filename().toLowerCase().endsWith(".csv")) {
                handler.handle(
                        new DefaultAsyncResult<ImportInfos>(new ImportException("invalid.file.extension")));
                return;
            }
            final String filename = path + File.separator + upload.name();
            upload.endHandler(new Handler<Void>() {
                @Override
                public void handle(Void event) {
                    log.info("File " + upload.filename() + " uploaded as " + upload.name());
                }
            });
            upload.streamToFileSystem(filename);
        }
    });
    vertx.fileSystem().mkdir(path, new Handler<AsyncResult<Void>>() {
        @Override
        public void handle(AsyncResult<Void> event) {
            if (event.succeeded()) {
                request.resume();
            } else {
                handler.handle(
                        new DefaultAsyncResult<ImportInfos>(new ImportException("mkdir.error", event.cause())));
            }
        }
    });
}

From source file:org.wisdom.framework.vertx.file.MixedFileUpload.java

License:Apache License

/**
 * Creates an instance of {@link org.wisdom.framework.vertx.file.VertxFileUpload}.
 *
 * @param vertx     the Vert.X instance//www  . j  av a  2s . com
 * @param upload    the upload object
 * @param limitSize the threshold. If the amount of uploaded data is below this limit,
 *                  {@link org.wisdom.framework.vertx.file.MemoryFileUpload} is used to backend the uploaded file.
 *                  Otherwise, it uses a {@link org.wisdom.framework.vertx.file.DiskFileUpload}.
 */
public MixedFileUpload(final Vertx vertx, final HttpServerFileUpload upload, final long limitSize,
        final long maxSize, final Handler<Result> errorHandler) {
    super(upload, errorHandler);
    delegate = new MemoryFileUpload(upload, errorHandler);

    upload.exceptionHandler(event -> LoggerFactory.getLogger(MixedFileUpload.class)
            .error("Cannot read the uploaded item {} ({})", upload.name(), upload.filename(), event))
            .endHandler(event -> delegate.close()).handler(event -> {
                if (event != null) {
                    // We are still in memory.
                    if (delegate instanceof MemoryFileUpload) {
                        MemoryFileUpload mem = (MemoryFileUpload) delegate;
                        checkSize(mem.buffer.length() + event.length(), maxSize, upload);
                        if (mem.buffer.length() + event.length() > limitSize) {
                            // Switch to disk file upload.
                            DiskFileUpload disk = new DiskFileUpload(vertx, upload, errorHandler);
                            // Initial push (mem + current buffer)
                            disk.push(mem.buffer.appendBuffer(event));
                            // No cleanup required for the memory based backend.
                            delegate = disk;

                            // the disk based implementation use a pump.
                        } else {
                            delegate.push(event);
                        }
                    }
                }
            });
}