Skip to content

Commit 003ff8a

Browse files
committed
Add feature to remove fingerpints after successful upload
See #27 Squashed commit of the following: commit b664f5b11b406d1ecf9b8fb5e2286de15641cc15 Author: Marius <[email protected]> Date: Thu Jun 13 12:16:27 2019 +0200 Move line endings back to Unix's LF commit b5807b8 Author: Juan Jose Rodriguez <[email protected]> Date: Tue Jun 4 17:16:59 2019 +0200 Change uploadFinished visibility to protected commit ebb5eab Author: Juan Jose Rodriguez <[email protected]> Date: Tue May 21 14:41:37 2019 +0200 #26 Add support for removeFingerprintOnSuccess config property
1 parent fc74dfc commit 003ff8a

File tree

5 files changed

+167
-19
lines changed

5 files changed

+167
-19
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies {
1818
compile 'org.jetbrains:annotations:13.0'
1919
testCompile 'junit:junit:4.12'
2020
testCompile 'org.mock-server:mockserver-netty:5.2.2'
21+
testCompile 'org.mockito:mockito-core:2.+'
2122
}
2223

2324
task sourcesJar(type: Jar, dependsOn: classes) {

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class TusClient {
2020

2121
private URL uploadCreationURL;
2222
private boolean resumingEnabled;
23+
private boolean removeFingerprintOnSuccessEnabled;
2324
private TusURLStore urlStore;
2425
private Map<String, String> headers;
2526
private int connectTimeout = 5000;
@@ -84,6 +85,37 @@ public boolean resumingEnabled() {
8485
return resumingEnabled;
8586
}
8687

88+
/**
89+
* Enable removing fingerprints after a successful upload.
90+
*
91+
* @see #disableRemoveFingerprintOnSuccess()
92+
*/
93+
public void enableRemoveFingerprintOnSuccess() {
94+
removeFingerprintOnSuccessEnabled = true;
95+
}
96+
97+
/**
98+
* Disable removing fingerprints after a successful upload.
99+
*
100+
* @see #enableRemoveFingerprintOnSuccess()
101+
*/
102+
public void disableRemoveFingerprintOnSuccess() {
103+
removeFingerprintOnSuccessEnabled = false;
104+
}
105+
106+
/**
107+
* Get the current status if removing fingerprints after a successful upload.
108+
*
109+
* @see #enableRemoveFingerprintOnSuccess()
110+
* @see #disableRemoveFingerprintOnSuccess()
111+
*
112+
* @return True if resuming has been enabled using {@link #enableResuming(TusURLStore)}
113+
*/
114+
public boolean removeFingerprintOnSuccessEnabled() {
115+
return removeFingerprintOnSuccessEnabled;
116+
}
117+
118+
87119
/**
88120
* Set headers which will be added to every HTTP requestes made by this TusClient instance.
89121
* These may to overwrite tus-specific headers, which can be identified by their Tus-*
@@ -165,7 +197,7 @@ public TusUploader createUpload(@NotNull TusUpload upload) throws ProtocolExcept
165197
urlStore.set(upload.getFingerprint(), uploadURL);
166198
}
167199

168-
return new TusUploader(this, uploadURL, upload.getTusInputStream(), 0);
200+
return new TusUploader(this, upload, uploadURL, upload.getTusInputStream(), 0);
169201
}
170202

171203
/**
@@ -233,7 +265,7 @@ public TusUploader beginOrResumeUploadFromURL(@NotNull TusUpload upload, @NotNul
233265
}
234266
long offset = Long.parseLong(offsetStr);
235267

236-
return new TusUploader(this, uploadURL, upload.getTusInputStream(), offset);
268+
return new TusUploader(this, upload, uploadURL, upload.getTusInputStream(), offset);
237269
}
238270

239271
/**
@@ -286,4 +318,16 @@ public void prepareConnection(@NotNull HttpURLConnection connection) {
286318
}
287319
}
288320
}
321+
322+
/**
323+
* Actions to be performed after a successful upload completion.
324+
* Manages URL removal from the URL store if remove fingerprint on success is enabled
325+
*
326+
* @param upload that has been finished
327+
*/
328+
protected void uploadFinished(@NotNull TusUpload upload) {
329+
if (resumingEnabled && removeFingerprintOnSuccessEnabled) {
330+
urlStore.remove(upload.getFingerprint());
331+
}
332+
}
289333
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class TusUploader {
2424
private TusInputStream input;
2525
private long offset;
2626
private TusClient client;
27+
private TusUpload upload;
2728
private byte[] buffer;
2829
private int requestPayloadSize = 10 * 1024 * 1024;
2930
private int bytesRemainingForRequest;
@@ -41,11 +42,12 @@ public class TusUploader {
4142
* @param offset Offset to read from
4243
* @throws IOException Thrown if an exception occurs while issuing the HTTP request.
4344
*/
44-
public TusUploader(TusClient client, URL uploadURL, TusInputStream input, long offset) throws IOException {
45+
public TusUploader(TusClient client, TusUpload upload, URL uploadURL, TusInputStream input, long offset) throws IOException {
4546
this.uploadURL = uploadURL;
4647
this.input = input;
4748
this.offset = offset;
4849
this.client = client;
50+
this.upload = upload;
4951

5052
input.seekTo(offset);
5153

@@ -270,6 +272,10 @@ public URL getUploadURL() {
270272
*/
271273
public void finish() throws ProtocolException, IOException {
272274
finishConnection();
275+
if (upload.getSize() == offset) {
276+
client.uploadFinished(upload);
277+
}
278+
273279
// Close the TusInputStream after checking the response and closing the connection to ensure
274280
// that we will not need to read from it again in the future.
275281
input.close();

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package io.tus.java.client;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
7+
38
import java.io.ByteArrayInputStream;
49
import java.io.IOException;
510
import java.net.HttpURLConnection;
@@ -13,8 +18,6 @@
1318
import org.mockserver.model.HttpRequest;
1419
import org.mockserver.model.HttpResponse;
1520

16-
import static org.junit.Assert.*;
17-
1821

1922
public class TestTusClient extends MockServerProvider {
2023
@Test
@@ -351,4 +354,47 @@ public void testFollowRedirects() throws Exception {
351354

352355
assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
353356
}
357+
358+
@Test
359+
public void testRemoveFingerprintOnSuccessDisabled() throws IOException, ProtocolException {
360+
361+
TusClient client = new TusClient();
362+
363+
TusURLStore store = new TusURLMemoryStore();
364+
URL dummyURL = new URL("http://dummy-url/files/dummy");
365+
store.set("fingerprint", dummyURL);
366+
client.enableResuming(store);
367+
368+
assertTrue(!client.removeFingerprintOnSuccessEnabled());
369+
370+
TusUpload upload = new TusUpload();
371+
upload.setFingerprint("fingerprint");
372+
373+
client.uploadFinished(upload);
374+
375+
assertTrue(dummyURL.equals(store.get("fingerprint")));
376+
377+
}
378+
379+
@Test
380+
public void testRemoveFingerprintOnSuccessEnabled() throws IOException, ProtocolException {
381+
382+
TusClient client = new TusClient();
383+
384+
TusURLStore store = new TusURLMemoryStore();
385+
URL dummyURL = new URL("http://dummy-url/files/dummy");
386+
store.set("fingerprint", dummyURL);
387+
client.enableResuming(store);
388+
client.enableRemoveFingerprintOnSuccess();
389+
390+
assertTrue(client.removeFingerprintOnSuccessEnabled());
391+
392+
TusUpload upload = new TusUpload();
393+
upload.setFingerprint("fingerprint");
394+
395+
client.uploadFinished(upload);
396+
397+
assertTrue(store.get("fingerprint") == null);
398+
399+
}
354400
}

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

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package io.tus.java.client;
22

3-
import org.junit.Assume;
4-
import org.junit.Test;
5-
import org.mockserver.model.HttpRequest;
6-
import org.mockserver.model.HttpResponse;
7-
import org.mockserver.socket.PortFactory;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.times;
88

99
import java.io.BufferedReader;
1010
import java.io.ByteArrayInputStream;
1111
import java.io.IOException;
12-
import java.io.InputStream;
1312
import java.io.InputStreamReader;
1413
import java.io.OutputStream;
1514
import java.net.MalformedURLException;
@@ -18,7 +17,11 @@
1817
import java.net.URL;
1918
import java.util.Arrays;
2019

21-
import static org.junit.Assert.*;
20+
import org.junit.Assume;
21+
import org.junit.Test;
22+
import org.mockserver.model.HttpRequest;
23+
import org.mockserver.model.HttpResponse;
24+
import org.mockserver.socket.PortFactory;
2225

2326
public class TestTusUploader extends MockServerProvider {
2427
private boolean isOpenJDK6 = System.getProperty("java.version").startsWith("1.6") &&
@@ -45,7 +48,9 @@ public void testTusUploader() throws IOException, ProtocolException {
4548
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
4649
long offset = 3;
4750

48-
TusUploader uploader = new TusUploader(client, uploadUrl, input, offset);
51+
TusUpload upload = new TusUpload();
52+
53+
TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, offset);
4954

5055
uploader.setChunkSize(5);
5156
assertEquals(uploader.getChunkSize(), 5);
@@ -58,6 +63,48 @@ public void testTusUploader() throws IOException, ProtocolException {
5863
uploader.finish();
5964
}
6065

66+
@Test
67+
public void testTusUploaderClientUploadFinishedCalled() throws IOException, ProtocolException {
68+
69+
TusClient client = mock(TusClient.class);
70+
71+
byte[] content = "hello world".getBytes();
72+
73+
URL uploadUrl = new URL("http://dummy-url/foo");
74+
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
75+
long offset = 10;
76+
77+
TusUpload upload = new TusUpload();
78+
upload.setSize(10);
79+
80+
TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, offset);
81+
uploader.finish();
82+
83+
// size and offset are the same, so uploadfinished() should be called
84+
verify(client).uploadFinished(upload);
85+
}
86+
87+
@Test
88+
public void testTusUploaderClientUploadFinishedNotCalled() throws IOException, ProtocolException {
89+
90+
TusClient client = mock(TusClient.class);
91+
92+
byte[] content = "hello world".getBytes();
93+
94+
URL uploadUrl = new URL("http://dummy-url/foo");
95+
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
96+
long offset = 0;
97+
98+
TusUpload upload = new TusUpload();
99+
upload.setSize(10);
100+
101+
TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, offset);
102+
uploader.finish();
103+
104+
// size is greater than offset, so uploadfinished() should not be called
105+
verify(client,times(0)).uploadFinished(upload);
106+
}
107+
61108
@Test
62109
public void testTusUploaderFailedExpectation() throws IOException, ProtocolException {
63110
Assume.assumeFalse(isOpenJDK6);
@@ -71,9 +118,9 @@ public void testTusUploaderFailedExpectation() throws IOException, ProtocolExcep
71118
URL uploadUrl = new URL(server.getURL() + "/expect");
72119
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
73120
long offset = 3;
74-
121+
TusUpload upload = new TusUpload();
75122
boolean exceptionThrown = false;
76-
TusUploader uploader = new TusUploader(client, uploadUrl, input, offset);
123+
TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, offset);
77124
try {
78125
uploader.uploadChunk();
79126
} catch(ProtocolException e) {
@@ -168,8 +215,9 @@ public void testSetRequestPayloadSize() throws Exception {
168215
TusClient client = new TusClient();
169216
URL uploadUrl = new URL(mockServerURL + "/payload");
170217
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
218+
TusUpload upload = new TusUpload();
171219

172-
TusUploader uploader = new TusUploader(client, uploadUrl, input, 0);
220+
TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, 0);
173221

174222
assertEquals(uploader.getRequestPayloadSize(), 10 * 1024 * 1024);
175223
uploader.setRequestPayloadSize(5);
@@ -197,8 +245,9 @@ public void testSetRequestPayloadSizeThrows() throws Exception {
197245
TusClient client = new TusClient();
198246
URL uploadUrl = new URL(mockServerURL + "/payloadException");
199247
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
248+
TusUpload upload = new TusUpload();
200249

201-
TusUploader uploader = new TusUploader(client, uploadUrl, input, 0);
250+
TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, 0);
202251

203252
uploader.setChunkSize(4);
204253
uploader.uploadChunk();
@@ -220,8 +269,9 @@ public void testMissingUploadOffsetHeader() throws Exception {
220269
TusClient client = new TusClient();
221270
URL uploadUrl = new URL(mockServerURL + "/missingHeader");
222271
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
272+
TusUpload upload = new TusUpload();
223273

224-
TusUploader uploader = new TusUploader(client, uploadUrl, input, 0);
274+
TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, 0);
225275

226276
boolean exceptionThrown = false;
227277
try {
@@ -249,8 +299,9 @@ public void testUnmatchingUploadOffsetHeader() throws Exception {
249299
TusClient client = new TusClient();
250300
URL uploadUrl = new URL(mockServerURL + "/unmatchingHeader");
251301
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
302+
TusUpload upload = new TusUpload();
252303

253-
TusUploader uploader = new TusUploader(client, uploadUrl, input, 0);
304+
TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, 0);
254305

255306
boolean exceptionThrown = false;
256307
try {

0 commit comments

Comments
 (0)