Skip to content

Commit e048ac5

Browse files
committed
Attempt creation if original upload can not be found anymore
Fixes #3
1 parent f3d7a35 commit e048ac5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ public TusUploader resumeOrCreateUpload(@NotNull TusUpload upload) throws Protoc
229229
return createUpload(upload);
230230
} catch(ResumingNotEnabledException e) {
231231
return createUpload(upload);
232+
} catch(ProtocolException e) {
233+
// If the attempt to resume returned a 404 Not Found, we immediately try to create a new
234+
// one since TusExectuor would not retry this operation.
235+
HttpURLConnection connection = e.getCausingConnection();
236+
if(connection != null && connection.getResponseCode() == 404) {
237+
return createUpload(upload);
238+
}
239+
240+
throw e;
232241
}
233242
}
234243

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,41 @@ public void testResumeOrCreateUpload() throws IOException, ProtocolException {
169169
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
170170
}
171171

172+
@Test
173+
public void testResumeOrCreateUploadNotFound() throws IOException, ProtocolException {
174+
mockServer.when(new HttpRequest()
175+
.withMethod("HEAD")
176+
.withPath("/files/not_found")
177+
.withHeader("Tus-Resumable", TusClient.TUS_VERSION))
178+
.respond(new HttpResponse()
179+
.withStatusCode(404));
180+
181+
mockServer.when(new HttpRequest()
182+
.withMethod("POST")
183+
.withPath("/files")
184+
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
185+
.withHeader("Upload-Length", "10"))
186+
.respond(new HttpResponse()
187+
.withStatusCode(201)
188+
.withHeader("Tus-Resumable", TusClient.TUS_VERSION)
189+
.withHeader("Location", mockServerURL + "/foo"));
190+
191+
TusClient client = new TusClient();
192+
client.setUploadCreationURL(mockServerURL);
193+
194+
TusURLStore store = new TusURLMemoryStore();
195+
store.set("fingerprint", new URL(mockServerURL + "/not_found"));
196+
client.enableResuming(store);
197+
198+
TusUpload upload = new TusUpload();
199+
upload.setSize(10);
200+
upload.setInputStream(new ByteArrayInputStream(new byte[10]));
201+
upload.setFingerprint("fingerprint");
202+
TusUploader uploader = client.resumeOrCreateUpload(upload);
203+
204+
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
205+
}
206+
172207
@Test
173208
public void testPrepareConnection() throws IOException {
174209
HttpURLConnection connection = (HttpURLConnection) mockServerURL.openConnection();

0 commit comments

Comments
 (0)