public Long handleUpload(String uploadId, Representation entity, Long startPosition) { ResumableUploadResource resource = getResource(uploadId); Long writtenBytes = 0L; try { final ReadableByteChannel source = entity.getChannel(); RandomAccessFile raf = null; FileChannel outputChannel = null; try { raf = new RandomAccessFile(resource.getFile(), "rw"); outputChannel = raf.getChannel(); writtenBytes = IOUtils.copyToFileChannel(256 * 1024, source, outputChannel, startPosition); } finally { try { if (raf != null) { raf.close(); } } finally { IOUtils.closeQuietly(source); IOUtils.closeQuietly(outputChannel); } } } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); } finally { } return resource.getFile().length(); }
// Return relative path of uploaded file private String getDestinationPath(String uploadId) throws IOException { ResumableUploadResource resource = getResource(uploadId); String fileName = resource .getFile() .getCanonicalPath() .replaceAll(tmpUploadFolder.dir().getCanonicalPath(), ""); fileName = fileName.replaceAll("_" + uploadId, ""); fileName = fileName.replaceAll("^/", ""); return fileName; }
/** * Executes validations on resume parameters to check if successive start position index matches * actual partial file length */ public Boolean validateUpload( String uploadId, Long totalByteToUpload, Long startPosition, Long endPosition, Long totalFileSize) { Boolean validated = false; ResumableUploadResource uploadResource = getResource(uploadId); if (uploadResource != null && uploadResource.getFile().exists()) { if (uploadResource.getFile().length() == startPosition) { validated = true; } } return validated; }
/** * Executes the mapping to move uploaded file from temporary folder to REST upload root Creates * the sidecar file */ public String uploadDone(String uploadId) throws IOException { ResumableUploadResource resource = getResource(uploadId); Map<String, String> storeParams = new HashMap<String, String>(); String destinationPath = getDestinationPath(uploadId); StringBuilder remappingPath = new StringBuilder(destinationPath); String tempFile = resource.getFile().getCanonicalPath(); RESTUtils.remapping( null, FilenameUtils.getBaseName(destinationPath), remappingPath, tempFile, storeParams); // Move file to remapped path Resource destinationFile = Resources.fromPath(remappingPath.toString()); // Fill file IOUtils.copyStream(new FileInputStream(resource.getFile()), destinationFile.out(), true, true); resource.delete(); // Add temporary sidecar file to mark upload completion, it will be cleared after // expirationThreshold getSideCarFile(uploadId).createNewFile(); return destinationPath.toString(); }
/** Create new temporary file for this uploadId */ public void clearUpload(String uploadId) { ResumableUploadResource resource = getResource(uploadId); if (resource != null) { resource.clear(); } }