-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from tomdesair/feature/generic-upload-id-factory
Support custom UploadIdFactory implementations that are not UUID-based
- Loading branch information
Showing
43 changed files
with
914 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/me/desair/tus/server/upload/TimeBasedUploadIdFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package me.desair.tus.server.upload; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* Alternative {@link UploadIdFactory} implementation that uses the current system time to generate ID's. | ||
* Since time is not unique, this upload ID factory should not be used in busy, clustered production systems. | ||
*/ | ||
public class TimeBasedUploadIdFactory extends UploadIdFactory { | ||
|
||
@Override | ||
protected Serializable getIdValueIfValid(String extractedUrlId) { | ||
Long id = null; | ||
|
||
if (StringUtils.isNotBlank(extractedUrlId)) { | ||
try { | ||
id = Long.parseLong(extractedUrlId); | ||
} catch (NumberFormatException ex) { | ||
id = null; | ||
} | ||
} | ||
|
||
return id; | ||
} | ||
|
||
@Override | ||
public synchronized UploadId createId() { | ||
return new UploadId(System.currentTimeMillis()); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/me/desair/tus/server/upload/UUIDUploadIdFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package me.desair.tus.server.upload; | ||
|
||
import java.io.Serializable; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Factory to create unique upload IDs. This factory can also parse the upload identifier | ||
* from a given upload URL. | ||
*/ | ||
public class UUIDUploadIdFactory extends UploadIdFactory { | ||
|
||
@Override | ||
protected Serializable getIdValueIfValid(String extractedUrlId) { | ||
UUID id = null; | ||
try { | ||
id = UUID.fromString(extractedUrlId); | ||
} catch (IllegalArgumentException ex) { | ||
id = null; | ||
} | ||
|
||
return id; | ||
} | ||
|
||
@Override | ||
public synchronized UploadId createId() { | ||
return new UploadId(UUID.randomUUID()); | ||
} | ||
|
||
} |
Oops, something went wrong.