Skip to content

Commit a3ef5ec

Browse files
committed
Allow beginning an upload from URL without creation
This is necessary to allow people to upload to Vimeo, see tus/tus-android-client#19
1 parent 795b04a commit a3ef5ec

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

example/src/main/java/io/tus/java/example/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static void main(String[] args) {
2525

2626
// Configure tus HTTP endpoint. This URL will be used for creating new uploads
2727
// using the Creation extension
28-
client.setUploadCreationURL(new URL("http://master.tus.io/files/"));
28+
client.setUploadCreationURL(new URL("https://master.tus.io/files/"));
2929

3030
// Enable resumable uploads by storing the upload URL in memory
3131
client.enableResuming(new TusURLMemoryStore());

src/main/java/io/tus/java/client/TusClient.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,36 @@ public TusUploader createUpload(@NotNull TusUpload upload) throws ProtocolExcept
186186
* @throws IOException Thrown if an exception occurs while issuing the HTTP request.
187187
*/
188188
public TusUploader resumeUpload(@NotNull TusUpload upload) throws FingerprintNotFoundException, ResumingNotEnabledException, ProtocolException, IOException {
189-
if(!resumingEnabled) {
189+
if (!resumingEnabled) {
190190
throw new ResumingNotEnabledException();
191191
}
192192

193193
URL uploadURL = urlStore.get(upload.getFingerprint());
194-
if(uploadURL == null) {
194+
if (uploadURL == null) {
195195
throw new FingerprintNotFoundException(upload.getFingerprint());
196196
}
197197

198+
return beginOrResumeUploadFromURL(upload, uploadURL);
199+
}
200+
201+
/**
202+
* Begin an upload or alternatively resume it if the upload has already been started before. In contrast to
203+
* {@link #createUpload(TusUpload)} and {@link #resumeOrCreateUpload(TusUpload)} this method will not create a new
204+
* upload. The user must obtain the upload location URL on their own as this method will not send the POST request
205+
* which is normally used to create a new upload.
206+
* Therefore, this method is only useful if you are uploading to a service which takes care of creating the tus
207+
* upload for yourself. One example of such a service is the Vimeo API.
208+
* When called a HEAD request will be issued to find the current offset without uploading the file, yet.
209+
* The uploading can be started by using the returned {@link TusUploader} object.
210+
*
211+
* @param upload The file for which an upload will be resumed
212+
* @param uploadURL The upload location URL at which has already been created and this file should be uploaded to.
213+
* @return Use {@link TusUploader} to upload the remaining file's chunks.
214+
* @throws ProtocolException Thrown if the remote server sent an unexpected response, e.g.
215+
* wrong status codes or missing/invalid headers.
216+
* @throws IOException Thrown if an exception occurs while issuing the HTTP request.
217+
*/
218+
public TusUploader beginOrResumeUploadFromURL(@NotNull TusUpload upload, @NotNull URL uploadURL) throws ProtocolException, IOException {
198219
HttpURLConnection connection = (HttpURLConnection) uploadURL.openConnection();
199220
connection.setRequestMethod("HEAD");
200221
prepareConnection(connection);

src/test/java/io/tus/java/client/TestTusClient.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void testCreateUploadWithMissingLocationHeader() throws IOException, Exce
103103
}
104104

105105
@Test
106-
public void testCreateUplaodWithRelativeLocation() throws Exception {
106+
public void testCreateUploadWithRelativeLocation() throws Exception {
107107
// We need to enable strict following for POST requests first
108108
System.setProperty("http.strictPostRedirect", "true");
109109

@@ -242,6 +242,30 @@ public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolExcep
242242
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
243243
}
244244

245+
@Test
246+
public void testBeginOrResumeUploadFromURL() throws IOException, ProtocolException {
247+
mockServer.when(new HttpRequest()
248+
.withMethod("HEAD")
249+
.withPath("/files/fooFromURL")
250+
.withHeader("Tus-Resumable", TusClient.TUS_VERSION))
251+
.respond(new HttpResponse()
252+
.withStatusCode(204)
253+
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
254+
.withHeader("Upload-Offset", "3"));
255+
256+
TusClient client = new TusClient();
257+
URL uploadURL = new URL(mockServerURL.toString() + "/fooFromURL");
258+
259+
TusUpload upload = new TusUpload();
260+
upload.setSize(10);
261+
upload.setInputStream(new ByteArrayInputStream(new byte[10]));
262+
263+
TusUploader uploader = client.beginOrResumeUploadFromURL(upload, uploadURL);
264+
265+
assertEquals(uploader.getUploadURL(), uploadURL);
266+
assertEquals(uploader.getOffset(), 3);
267+
}
268+
245269
@Test
246270
public void testPrepareConnection() throws IOException {
247271
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();

0 commit comments

Comments
 (0)