output.close(); TusUpload upload = new TusUpload(file); metadata.putAll(upload.getMetadata()); upload.setMetadata(metadata); assertEquals(upload.getMetadata(), metadata); assertEquals( upload.getEncodedMetadata(), "foo aGVsbG8=,bar d29ybGQ=,filename " + TusUpload.base64Encode(file.getName().getBytes())); assertEquals(upload.getSize(), content.length()); assertNotSame(upload.getFingerprint(), ""); byte[] readContent = new byte[content.length()]; assertEquals(upload.getInputStream().read(readContent), content.length()); assertEquals(new String(readContent), content);
/** * Create a new TusUpload object using the supplied File object. The corresponding {@link * InputStream}, size and fingerprint will be automatically set. * * @param file The file whose content should be later uploaded. * @throws FileNotFoundException Thrown if the file cannot be found. */ public TusUpload(@NotNull File file) throws FileNotFoundException { size = file.length(); setInputStream(new FileInputStream(file)); fingerprint = String.format("%s-%d", file.getAbsolutePath(), size); metadata = new HashMap<String, String>(); metadata.put("filename", file.getName()); }
prepareConnection(connection); String encodedMetadata = upload.getEncodedMetadata(); if(encodedMetadata.length() > 0) { connection.setRequestProperty("Upload-Metadata", encodedMetadata); connection.addRequestProperty("Upload-Length", Long.toString(upload.getSize())); connection.connect(); urlStore.set(upload.getFingerprint(), uploadURL); return new TusUploader(this, uploadURL, upload.getTusInputStream(), 0);
@Test public void testBeginOrResumeUploadFromURL() throws IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("HEAD") .withPath("/files/fooFromURL") .withHeader("Tus-Resumable", TusClient.TUS_VERSION)) .respond(new HttpResponse() .withStatusCode(204) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Offset", "3")); TusClient client = new TusClient(); URL uploadURL = new URL(mockServerURL.toString() + "/fooFromURL"); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); TusUploader uploader = client.beginOrResumeUploadFromURL(upload, uploadURL); assertEquals(uploader.getUploadURL(), uploadURL); assertEquals(uploader.getOffset(), 3); }
@Test public void testResumeUpload() throws ResumingNotEnabledException, FingerprintNotFoundException, IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("HEAD") .withPath("/files/foo") .withHeader("Tus-Resumable", TusClient.TUS_VERSION)) .respond(new HttpResponse() .withStatusCode(204) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Offset", "3")); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); client.enableResuming(new TestResumeUploadStore()); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); upload.setFingerprint("test-fingerprint"); TusUploader uploader = client.resumeUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL.toString() + "/foo")); assertEquals(uploader.getOffset(), 3); }
@Test public void testCreateUpload() throws IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Metadata", "foo aGVsbG8=,bar d29ybGQ=") .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Location", mockServerURL + "/foo")); Map<String, String> metadata = new LinkedHashMap<String, String>(); metadata.put("foo", "hello"); metadata.put("bar", "world"); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); upload.setMetadata(metadata); TusUploader uploader = client.createUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo")); }
@Override protected void makeAttempt() throws ProtocolException, IOException { // First try to resume an upload. If that's not possible we will create a new // upload and get a TusUploader in return. This class is responsible for opening // a connection to the remote server and doing the uploading. TusUploader uploader = client.resumeOrCreateUpload(upload); // Upload the file in chunks of 1KB sizes. uploader.setChunkSize(1024); // Upload the file as long as data is available. Once the // file has been fully uploaded the method will return -1 do { // Calculate the progress using the total size of the uploading file and // the current offset. long totalBytes = upload.getSize(); long bytesUploaded = uploader.getOffset(); double progress = (double) bytesUploaded / totalBytes * 100; System.out.printf("Upload at %06.2f%%.\n", progress); } while(uploader.uploadChunk() > -1); // Allow the HTTP connection to be closed and cleaned up uploader.finish(); System.out.println("Upload finished."); System.out.format("Upload available at: %s", uploader.getUploadURL().toString()); } };
/** * Encode the metadata into a string according to the specification, so it can be * used as the value for the Upload-Metadata header. * * @return Encoded metadata */ public String getEncodedMetadata() { if(metadata == null || metadata.size() == 0) { return ""; } String encoded = ""; boolean firstElement = true; for(Map.Entry<String, String> entry : metadata.entrySet()) { if(!firstElement) { encoded += ","; } encoded += entry.getKey() + " " + base64Encode(entry.getValue().getBytes()); firstElement = false; } return encoded; }
/** * Try to resume an already started upload. Before call this function, resuming must be * enabled using {@link #enableResuming(TusURLStore)}. This method will look up the URL for this * upload in the {@link TusURLStore} using the upload's fingerprint (see * {@link TusUpload#getFingerprint()}). After a successful lookup a HEAD request will be issued * to find the current offset without uploading the file, yet. * * @param upload The file for which an upload will be resumed * @return Use {@link TusUploader} to upload the remaining file's chunks. * @throws FingerprintNotFoundException Thrown if no matching fingerprint has been found in * {@link TusURLStore}. Use {@link #createUpload(TusUpload)} to create a new upload. * @throws ResumingNotEnabledException Throw if resuming has not been enabled using {@link * #enableResuming(TusURLStore)}. * @throws ProtocolException Thrown if the remote server sent an unexpected response, e.g. * wrong status codes or missing/invalid headers. * @throws IOException Thrown if an exception occurs while issuing the HTTP request. */ public TusUploader resumeUpload(@NotNull TusUpload upload) throws FingerprintNotFoundException, ResumingNotEnabledException, ProtocolException, IOException { if (!resumingEnabled) { throw new ResumingNotEnabledException(); } URL uploadURL = urlStore.get(upload.getFingerprint()); if (uploadURL == null) { throw new FingerprintNotFoundException(upload.getFingerprint()); } return beginOrResumeUploadFromURL(upload, uploadURL); }
final TusUpload upload = new TusUpload(file);
return new TusUploader(this, uploadURL, upload.getTusInputStream(), offset);
@Test public void testResumeOrCreateUpload() throws IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Location", mockServerURL + "/foo")); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); TusUploader uploader = client.resumeOrCreateUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo")); }
@Test public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolException { mockServer.when(new HttpRequest() .withMethod("HEAD") .withPath("/files/not_found") .withHeader("Tus-Resumable", TusClient.TUS_VERSION)) .respond(new HttpResponse() .withStatusCode(404)); mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Location", mockServerURL + "/foo")); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusURLStore store = new TusURLMemoryStore(); store.set("fingerprint", new URL(mockServerURL + "/not_found")); client.enableResuming(store); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); upload.setFingerprint("fingerprint"); TusUploader uploader = client.resumeOrCreateUpload(upload); assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo")); }
@Test public void testCreateUploadWithMissingLocationHeader() throws IOException, Exception { mockServer.when(new HttpRequest() .withMethod("POST") .withPath("/files") .withHeader("Tus-Resumable", TusClient.TUS_VERSION) .withHeader("Upload-Length", "10")) .respond(new HttpResponse() .withStatusCode(201) .withHeader("Tus-Resumable", TusClient.TUS_VERSION)); TusClient client = new TusClient(); client.setUploadCreationURL(mockServerURL); TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); try { TusUploader uploader = client.createUpload(upload); throw new Exception("unreachable code reached"); } catch(ProtocolException e) { assertEquals(e.getMessage(), "missing upload URL in response for creating upload"); } }
TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); TusUploader uploader = client.createUpload(upload);
TusUpload upload = new TusUpload(); upload.setSize(10); upload.setInputStream(new ByteArrayInputStream(new byte[10])); TusUploader uploader = client.createUpload(upload);