/** * Creates a temporary file. * * @return a new Temp File from getDiskFilename(), default prefix, postfix and baseDirectory */ static synchronized File tempFile(HttpServerFileUpload upload) { String newpostfix; String diskFilename = new File(upload.filename()).getName(); newpostfix = '_' + diskFilename; File tmpFile; try { if (baseDirectory == null) { // create a temporary file tmpFile = File.createTempFile(prefix, newpostfix); } else { tmpFile = File.createTempFile(prefix, newpostfix, new File( baseDirectory)); } if (deleteOnExitTemporaryFile) { tmpFile.deleteOnExit(); } return tmpFile; } catch (IOException e) { // Really bad, can't create the tmp file. throw new IllegalStateException(e); } }
/** * Uploads the file contents to S3. * * @param aBucket An S3 bucket * @param aKey An S3 key * @param aUpload An HttpServerFileUpload * @param aHandler An upload response handler */ public void put(final String aBucket, final String aKey, final HttpServerFileUpload aUpload, final Handler<HttpClientResponse> aHandler) { final S3ClientRequest request = createPutRequest(aBucket, aKey, aHandler); final Buffer buffer = Buffer.buffer(); aUpload.endHandler(event -> { request.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(buffer.length())); request.end(buffer); }); aUpload.handler(data -> { buffer.appendBuffer(data); }); }
/** * Creates an instance of {@link org.wisdom.framework.vertx.file.VertxFileUpload}. * * @param vertx the Vert.X instance * @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); } } } } ); }
public FileUploadImpl(String uploadedFileName, HttpServerFileUpload upload) { this.uploadedFileName = uploadedFileName; this.upload = upload; }
/** * Checks whether we exceed the max allowed file size. * * @param newSize the expected size once the current chunk is consumed * @param maxSize the max allowed size. * @param upload the upload */ private void checkSize(long newSize, long maxSize, HttpServerFileUpload upload) { if (maxSize >= 0 && newSize > maxSize) { upload.handler(null); report(new IllegalStateException("Size exceed allowed maximum capacity")); } }
/** * Creates an instance of {@link org.wisdom.framework.vertx.file.DiskFileUpload}. * * @param vertx the Vert.X instance * @param upload the Vert.X file upload object * @param errorHandler the error handler */ public DiskFileUpload(Vertx vertx, HttpServerFileUpload upload, Handler<Result> errorHandler) { super(upload, errorHandler); this.file = tempFile(upload); this.vertx = vertx; }
/** * Creates an instance of {@link org.wisdom.framework.vertx.file.MemoryFileUpload}. * * @param upload the Vert.X file upload object * @param errorHandler the error handler */ public MemoryFileUpload(HttpServerFileUpload upload, Handler<Result> errorHandler) { super(upload, errorHandler); }
/** * Creates the {@link org.wisdom.framework.vertx.file.VertxFileUpload}. * * @param upload the {@link HttpServerFileUpload} that is uploaded. * @param errorHandler the error handler. */ protected VertxFileUpload(HttpServerFileUpload upload, Handler<Result> errorHandler) { this.upload = upload; this.errorHandler = errorHandler; }