Skip to content

Commit

Permalink
#26 Add support for removeFingerprintOnSuccess config property
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Jose Rodriguez committed May 21, 2019
1 parent fc74dfc commit ebb5eab
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 23 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
compile 'org.jetbrains:annotations:13.0'
testCompile 'junit:junit:4.12'
testCompile 'org.mock-server:mockserver-netty:5.2.2'
testCompile 'org.mockito:mockito-core:2.+'
}

task sourcesJar(type: Jar, dependsOn: classes) {
Expand Down
48 changes: 46 additions & 2 deletions src/main/java/io/tus/java/client/TusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TusClient {

private URL uploadCreationURL;
private boolean resumingEnabled;
private boolean removeFingerprintOnSuccessEnabled;
private TusURLStore urlStore;
private Map<String, String> headers;
private int connectTimeout = 5000;
Expand Down Expand Up @@ -83,6 +84,37 @@ public void disableResuming() {
public boolean resumingEnabled() {
return resumingEnabled;
}

/**
* Enable removing fingerprints on success.
*
* @see #disableRemoveFingerprintOnSuccess()
*/
public void enableRemoveFingerprintOnSuccess() {
removeFingerprintOnSuccessEnabled = true;
}

/**
* Disable removing fingerprints on success.
*
* @see #enableRemoveFingerprintOnSuccess()
*/
public void disableRemoveFingerprintOnSuccess() {
removeFingerprintOnSuccessEnabled = false;
}

/**
* Get the current status if removing fingerprints on success.
*
* @see #enableRemoveFingerprintOnSuccess()
* @see #disableRemoveFingerprintOnSuccess()
*
* @return True if resuming has been enabled using {@link #enableResuming(TusURLStore)}
*/
public boolean removeFingerprintOnSuccessEnabled() {
return removeFingerprintOnSuccessEnabled;
}


/**
* Set headers which will be added to every HTTP requestes made by this TusClient instance.
Expand Down Expand Up @@ -165,7 +197,7 @@ public TusUploader createUpload(@NotNull TusUpload upload) throws ProtocolExcept
urlStore.set(upload.getFingerprint(), uploadURL);
}

return new TusUploader(this, uploadURL, upload.getTusInputStream(), 0);
return new TusUploader(this, upload, uploadURL, upload.getTusInputStream(), 0);
}

/**
Expand Down Expand Up @@ -233,7 +265,7 @@ public TusUploader beginOrResumeUploadFromURL(@NotNull TusUpload upload, @NotNul
}
long offset = Long.parseLong(offsetStr);

return new TusUploader(this, uploadURL, upload.getTusInputStream(), offset);
return new TusUploader(this, upload, uploadURL, upload.getTusInputStream(), offset);
}

/**
Expand Down Expand Up @@ -286,4 +318,16 @@ public void prepareConnection(@NotNull HttpURLConnection connection) {
}
}
}

/**
* Actions to be performed after a successful upload completion.
* Manages URL removal from the URL store if remove fingerprint on success is enabled
*
* @param upload that has been finished
*/
public void uploadFinished(@NotNull TusUpload upload) {
if (resumingEnabled && removeFingerprintOnSuccessEnabled) {
urlStore.remove(upload.getFingerprint());
}
}
}
8 changes: 7 additions & 1 deletion src/main/java/io/tus/java/client/TusUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class TusUploader {
private TusInputStream input;
private long offset;
private TusClient client;
private TusUpload upload;
private byte[] buffer;
private int requestPayloadSize = 10 * 1024 * 1024;
private int bytesRemainingForRequest;
Expand All @@ -41,11 +42,12 @@ public class TusUploader {
* @param offset Offset to read from
* @throws IOException Thrown if an exception occurs while issuing the HTTP request.
*/
public TusUploader(TusClient client, URL uploadURL, TusInputStream input, long offset) throws IOException {
public TusUploader(TusClient client, TusUpload upload, URL uploadURL, TusInputStream input, long offset) throws IOException {
this.uploadURL = uploadURL;
this.input = input;
this.offset = offset;
this.client = client;
this.upload = upload;

input.seekTo(offset);

Expand Down Expand Up @@ -270,6 +272,10 @@ public URL getUploadURL() {
*/
public void finish() throws ProtocolException, IOException {
finishConnection();
if (upload.getSize() == offset) {
client.uploadFinished(upload);
}

// Close the TusInputStream after checking the response and closing the connection to ensure
// that we will not need to read from it again in the future.
input.close();
Expand Down
50 changes: 48 additions & 2 deletions src/test/java/io/tus/java/client/TestTusClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package io.tus.java.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
Expand All @@ -13,8 +18,6 @@
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;

import static org.junit.Assert.*;


public class TestTusClient extends MockServerProvider {
@Test
Expand Down Expand Up @@ -351,4 +354,47 @@ public void testFollowRedirects() throws Exception {

assertEquals(uploader.getUploadURL(), new URL(mockServerURL + "/foo"));
}

@Test
public void testRemoveFingerprintOnSuccessDisabled() throws IOException, ProtocolException {

TusClient client = new TusClient();

TusURLStore store = new TusURLMemoryStore();
URL dummyURL = new URL("http://dummy-url/files/dummy");
store.set("fingerprint", dummyURL);
client.enableResuming(store);

assertTrue(!client.removeFingerprintOnSuccessEnabled());

TusUpload upload = new TusUpload();
upload.setFingerprint("fingerprint");

client.uploadFinished(upload);

assertTrue(dummyURL.equals(store.get("fingerprint")));

}

@Test
public void testRemoveFingerprintOnSuccessEnabled() throws IOException, ProtocolException {

TusClient client = new TusClient();

TusURLStore store = new TusURLMemoryStore();
URL dummyURL = new URL("http://dummy-url/files/dummy");
store.set("fingerprint", dummyURL);
client.enableResuming(store);
client.enableRemoveFingerprintOnSuccess();

assertTrue(client.removeFingerprintOnSuccessEnabled());

TusUpload upload = new TusUpload();
upload.setFingerprint("fingerprint");

client.uploadFinished(upload);

assertTrue(store.get("fingerprint") == null);

}
}
87 changes: 69 additions & 18 deletions src/test/java/io/tus/java/client/TestTusUploader.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.tus.java.client;

import org.junit.Assume;
import org.junit.Test;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.socket.PortFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
Expand All @@ -18,7 +17,11 @@
import java.net.URL;
import java.util.Arrays;

import static org.junit.Assert.*;
import org.junit.Assume;
import org.junit.Test;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.socket.PortFactory;

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

TusUploader uploader = new TusUploader(client, uploadUrl, input, offset);
TusUpload upload = new TusUpload();

TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, offset);

uploader.setChunkSize(5);
assertEquals(uploader.getChunkSize(), 5);
Expand All @@ -57,6 +62,48 @@ public void testTusUploader() throws IOException, ProtocolException {
assertEquals(11, uploader.getOffset());
uploader.finish();
}

@Test
public void testTusUploaderClientUploadFinishedCalled() throws IOException, ProtocolException {

TusClient client = mock(TusClient.class);

byte[] content = "hello world".getBytes();

URL uploadUrl = new URL("http://dummy-url/foo");
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
long offset = 10;

TusUpload upload = new TusUpload();
upload.setSize(10);

TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, offset);
uploader.finish();

// size and offset are the same, so uploadfinished() should be called
verify(client).uploadFinished(upload);
}

@Test
public void testTusUploaderClientUploadFinishedNotCalled() throws IOException, ProtocolException {

TusClient client = mock(TusClient.class);

byte[] content = "hello world".getBytes();

URL uploadUrl = new URL("http://dummy-url/foo");
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
long offset = 0;

TusUpload upload = new TusUpload();
upload.setSize(10);

TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, offset);
uploader.finish();

// size is greater than offset, so uploadfinished() should not be called
verify(client,times(0)).uploadFinished(upload);
}

@Test
public void testTusUploaderFailedExpectation() throws IOException, ProtocolException {
Expand All @@ -71,9 +118,9 @@ public void testTusUploaderFailedExpectation() throws IOException, ProtocolExcep
URL uploadUrl = new URL(server.getURL() + "/expect");
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));
long offset = 3;

TusUpload upload = new TusUpload();
boolean exceptionThrown = false;
TusUploader uploader = new TusUploader(client, uploadUrl, input, offset);
TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, offset);
try {
uploader.uploadChunk();
} catch(ProtocolException e) {
Expand Down Expand Up @@ -168,8 +215,9 @@ public void testSetRequestPayloadSize() throws Exception {
TusClient client = new TusClient();
URL uploadUrl = new URL(mockServerURL + "/payload");
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));

TusUploader uploader = new TusUploader(client, uploadUrl, input, 0);
TusUpload upload = new TusUpload();

TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, 0);

assertEquals(uploader.getRequestPayloadSize(), 10 * 1024 * 1024);
uploader.setRequestPayloadSize(5);
Expand Down Expand Up @@ -197,8 +245,9 @@ public void testSetRequestPayloadSizeThrows() throws Exception {
TusClient client = new TusClient();
URL uploadUrl = new URL(mockServerURL + "/payloadException");
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));

TusUploader uploader = new TusUploader(client, uploadUrl, input, 0);
TusUpload upload = new TusUpload();

TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, 0);

uploader.setChunkSize(4);
uploader.uploadChunk();
Expand All @@ -220,8 +269,9 @@ public void testMissingUploadOffsetHeader() throws Exception {
TusClient client = new TusClient();
URL uploadUrl = new URL(mockServerURL + "/missingHeader");
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));

TusUploader uploader = new TusUploader(client, uploadUrl, input, 0);
TusUpload upload = new TusUpload();

TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, 0);

boolean exceptionThrown = false;
try {
Expand Down Expand Up @@ -249,8 +299,9 @@ public void testUnmatchingUploadOffsetHeader() throws Exception {
TusClient client = new TusClient();
URL uploadUrl = new URL(mockServerURL + "/unmatchingHeader");
TusInputStream input = new TusInputStream(new ByteArrayInputStream(content));

TusUploader uploader = new TusUploader(client, uploadUrl, input, 0);
TusUpload upload = new TusUpload();

TusUploader uploader = new TusUploader(client, upload, uploadUrl, input, 0);

boolean exceptionThrown = false;
try {
Expand Down

0 comments on commit ebb5eab

Please sign in to comment.