assertEquals("file", upload.name()); assertEquals("tmp-0.txt", upload.filename()); assertEquals("image/gif", upload.contentType()); String uploadedFileName; if (!streamToDisk) { upload.handler(tot::appendBuffer); uploadedFileName = null; } else { uploadedFileName = new File(testDir, UUID.randomUUID().toString()).getPath(); upload.streamToFileSystem(uploadedFileName); upload.endHandler(v -> { if (streamToDisk) { Buffer uploaded = vertx.fileSystem().readFileBlocking(uploadedFileName); assertEquals(content, tot); assertTrue(upload.isSizeAvailable()); assertEquals(content.length(), upload.size()); complete(); });
@Override public void start() throws Exception { vertx.createHttpServer().requestHandler(req -> { if (req.uri().equals("/")) { // Serve the index page req.response().sendFile("index.html"); } else if (req.uri().startsWith("/form")) { req.setExpectMultipart(true); req.uploadHandler(upload -> { upload.exceptionHandler(cause -> { req.response().setChunked(true).end("Upload failed"); }); upload.endHandler(v -> { req.response().setChunked(true).end("Successfully uploaded to " + upload.filename()); }); // FIXME - Potential security exploit! In a real system you must check this filename // to make sure you're not saving to a place where you don't want! // Or better still, just use Vert.x-Web which controls the upload area. upload.streamToFileSystem(upload.filename()); }); } else { req.response().setStatusCode(404); req.response().end(); } }).listen(8080); } }
@Override public String charSet() { return upload.charset(); }
if (bodyLimit != -1 && upload.isSizeAvailable()) { long size = uploadSize + upload.size(); if (size > bodyLimit) { failed = true; upload.streamToFileSystem(uploadedFileName); FileUploadImpl fileUpload = new FileUploadImpl(uploadedFileName, upload); fileUploads.add(fileUpload); upload.exceptionHandler(t -> { deleteFileUploads(); context.fail(t); }); upload.endHandler(v -> uploadEnded());
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) {
public YokeFileUpload(@NotNull final Vertx vertx, @NotNull final HttpServerFileUpload fileUpload, @NotNull String uploadDir) { this.fileSystem = vertx.fileSystem(); this.filename = fileUpload.filename(); this.name = fileUpload.name(); this.contentType = fileUpload.contentType(); this.contentTransferEncoding = fileUpload.contentTransferEncoding(); this.charset = fileUpload.charset(); this.size = fileUpload.size(); if (!uploadDir.endsWith(File.separator)) { uploadDir += File.separator; } this.path = uploadDir + UUID.randomUUID().toString(); }
private void testFileUploadFormMultipart(int size) throws Exception { Buffer content = Buffer.buffer(TestUtils.randomAlphaString(size)); vertx.fileSystem().writeFileBlocking(testFile.getPath(), content); server.requestHandler(req -> { req.setExpectMultipart(true); req.uploadHandler(upload -> { Buffer fileBuffer = Buffer.buffer(); assertEquals("file", upload.name()); assertEquals("test.txt", upload.filename()); assertEquals("text/plain", upload.contentType()); upload.handler(fileBuffer::appendBuffer); upload.endHandler(v -> assertEquals(content, fileBuffer)); }); req.endHandler(v -> { assertEquals("vert.x", req.getFormAttribute("toolkit")); assertEquals("jvm", req.getFormAttribute("runtime")); req.response().end(); }); }); startServer(); MultipartForm form = MultipartForm.create() .attribute("toolkit", "vert.x") .attribute("runtime", "jvm") .textFileUpload("file", testFile.getName(), testFile.getPath(), "text/plain"); HttpRequest<Buffer> builder = client.post("somepath"); builder.sendMultipartForm(form, onSuccess(resp -> complete())); await(); }
BHandler(RoutingContext context) { this.context = context; Set<FileUpload> fileUploads = context.fileUploads(); final String contentType = context.request().getHeader(HttpHeaders.CONTENT_TYPE); isMultipart = contentType != null && contentType.contains("multipart/form-data"); isUrlEncoded = contentType != null && contentType.contains("application/x-www-form-urlencoded"); if (isMultipart || isUrlEncoded) { makeUploadDir(context.vertx().fileSystem()); context.request().setExpectMultipart(true); context.request().uploadHandler(upload -> { // *** cse begin *** if (uploadsDir == null) { failed = true; CommonExceptionData data = new CommonExceptionData("not support file upload."); throw new ErrorDataDecoderException(ExceptionFactory.createConsumerException(data)); } // *** cse end *** // we actually upload to a file with a generated filename uploadCount.incrementAndGet(); String uploadedFileName = new File(uploadsDir, UUID.randomUUID().toString()).getPath(); upload.streamToFileSystem(uploadedFileName); FileUploadImpl fileUpload = new FileUploadImpl(uploadedFileName, upload); fileUploads.add(fileUpload); upload.exceptionHandler(context::fail); upload.endHandler(v -> uploadEnded()); }); } context.request().exceptionHandler(context::fail); }
@Override public void handle(final HttpServerFileUpload fileUpload) { if (request.files() == null) { request.setFiles(new HashMap<String, YokeFileUpload>()); } final YokeFileUpload upload = new YokeFileUpload(vertx(), fileUpload, uploadDir); // setup callbacks fileUpload.exceptionHandler(new Handler<Throwable>() { @Override public void handle(Throwable throwable) { next.handle(throwable); } }); // stream to the generated path fileUpload.streamToFileSystem(upload.path()); // store a reference in the request request.files().put(fileUpload.name(), upload); // set up a callback to remove the file from the file system when the request completes request.response().endHandler(new Handler<Void>() { @Override public void handle(Void event) { if (upload.isTransient()) { upload.delete(); } } }); } });
@Override public String fileName() { return upload.filename(); }
@Override public String name() { return upload.name(); }
@Override public String contentType() { return upload.contentType(); }
@Override public long size() { return upload.size(); }
public io.vertx.rxjava.core.http.HttpServerFileUpload endHandler(Handler<Void> endHandler) { delegate.endHandler(endHandler); return this; }
assertEquals(req.path(), "/form"); req.setExpectMultipart(true); req.uploadHandler(event -> event.handler(buffer -> { fail("Should not get here"); }));
/** * Stream the content of this upload to the given file on storage. * @param filename the name of the file * @return */ public io.vertx.rxjava.core.http.HttpServerFileUpload streamToFileSystem(String filename) { delegate.streamToFileSystem(filename); return this; }
public io.vertx.rxjava.core.http.HttpServerFileUpload exceptionHandler(Handler<Throwable> handler) { delegate.exceptionHandler(handler); return this; }
/** * @return true if the size of the upload can be retrieved via {@link io.vertx.rxjava.core.http.HttpServerFileUpload#size}. */ public boolean isSizeAvailable() { boolean ret = delegate.isSizeAvailable(); return ret; }
if (bodyLimit != -1 && upload.isSizeAvailable()) { long size = uploadSize + upload.size(); if (size > bodyLimit) { failed = true; upload.streamToFileSystem(uploadedFileName); FileUploadImpl fileUpload = new FileUploadImpl(uploadedFileName, upload); fileUploads.add(fileUpload); upload.exceptionHandler(t -> { deleteFileUploads(); context.fail(t); }); upload.endHandler(v -> uploadEnded()); });
/** * @return the filename which was used when upload the file. */ public String filename() { String ret = delegate.filename(); return ret; }