Package me.desair.tus.server
Class TusFileUploadService
java.lang.Object
me.desair.tus.server.TusFileUploadService
Helper class that implements the server side tus v1.0.0 upload protocol
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddTusExtension
(TusExtension feature) Add a custom (application-specific) extension that implements theTusExtension
interface.void
cleanup()
This method should be invoked periodically.void
deleteUpload
(String uploadUri) Method to delete an upload associated with the given upload URL.void
deleteUpload
(String uploadUri, String ownerKey) Method to delete an upload associated with the given upload URL.disableTusExtension
(String extensionName) Disable the TusExtension for which the getName() method matches the provided string.protected void
executeProcessingByFeatures
(HttpMethod method, TusServletRequest servletRequest, TusServletResponse servletResponse, String ownerKey) Get the set of enabled Tus extensions.Get all HTTP methods that are supported by this TusUploadService based on the enabled and/or disabled tus extensions.getUploadedBytes
(String uploadUri) Method to retrieve the bytes that were uploaded to a specific upload URI.getUploadedBytes
(String uploadUri, String ownerKey) Method to retrieve the bytes that were uploaded to a specific upload URI.getUploadInfo
(String uploadUri) Get the information on the upload corresponding to the given upload URI.getUploadInfo
(String uploadUri, String ownerKey) Get the information on the upload corresponding to the given upload URI.protected void
void
process
(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse) Process a tus upload request.void
process
(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, String ownerKey) Process a tus upload request that belongs to a specific owner.protected void
processLockedRequest
(HttpMethod method, TusServletRequest request, TusServletResponse response, String ownerKey) protected void
processTusException
(HttpMethod method, TusServletRequest request, TusServletResponse response, String ownerKey, TusException exception) protected void
validateRequest
(HttpMethod method, jakarta.servlet.http.HttpServletRequest servletRequest, String ownerKey) withChunkedTransferDecoding
(boolean isEnabled) Instruct this service to (not) decode any requests with Transfer-Encoding value "chunked".Enable the unofficial `download` extension that also allows you to download uploaded bytes.withMaxUploadSize
(Long maxUploadSize) Specify the maximum number of bytes that can be uploaded per upload.withStoragePath
(String storagePath) If you're using the default file system-based storage service, you can use this method to specify the path where to store the uploaded bytes and upload information.withThreadLocalCache
(boolean isEnabled) Enable or disable a thread-local based cache of upload data.withUploadExpirationPeriod
(Long expirationPeriod) You can set the number of milliseconds after which an upload is considered as expired and available for cleanup.withUploadIdFactory
(UploadIdFactory uploadIdFactory) Provide a customUploadIdFactory
implementation that should be used to generate identifiers for the different uploads.withUploadLockingService
(UploadLockingService uploadLockingService) Provide a customUploadLockingService
implementation that should be used when processing uploads.withUploadStorageService
(UploadStorageService uploadStorageService) Provide a customUploadStorageService
implementation that should be used to store uploaded bytes and metadata (UploadInfo
).withUploadUri
(String uploadUri) Set the URI under which the main tus upload endpoint is hosted.
-
Field Details
-
TUS_API_VERSION
- See Also:
-
-
Constructor Details
-
TusFileUploadService
public TusFileUploadService()
-
-
Method Details
-
initFeatures
protected void initFeatures() -
withUploadUri
Set the URI under which the main tus upload endpoint is hosted. Optionally, this URI may contain regex parameters in order to support endpoints that contain URL parameters, for example /users/[0-9]+/files/upload- Parameters:
uploadUri
- The URI of the main tus upload endpoint- Returns:
- The current service
-
withMaxUploadSize
Specify the maximum number of bytes that can be uploaded per upload. If you don't call this method, the maximum number of bytes is Long.MAX_VALUE.- Parameters:
maxUploadSize
- The maximum upload length that is allowed- Returns:
- The current service
-
withUploadIdFactory
Provide a customUploadIdFactory
implementation that should be used to generate identifiers for the different uploads. Example implementation areUuidUploadIdFactory
andTimeBasedUploadIdFactory
.- Parameters:
uploadIdFactory
- The customUploadIdFactory
implementation- Returns:
- The current service
-
withUploadStorageService
Provide a customUploadStorageService
implementation that should be used to store uploaded bytes and metadata (UploadInfo
).- Parameters:
uploadStorageService
- The customUploadStorageService
implementation- Returns:
- The current service
-
withUploadLockingService
Provide a customUploadLockingService
implementation that should be used when processing uploads. The upload locking service is responsible for locking an upload that is being processed so that it cannot be corrupted by simultaneous or delayed requests.- Parameters:
uploadLockingService
- TheUploadLockingService
implementation to use- Returns:
- The current service
-
withStoragePath
If you're using the default file system-based storage service, you can use this method to specify the path where to store the uploaded bytes and upload information.- Parameters:
storagePath
- The file system path where uploads can be stored (temporarily)- Returns:
- The current service
-
withThreadLocalCache
Enable or disable a thread-local based cache of upload data. This can reduce the load on the storage backends. By default this cache is disabled.- Parameters:
isEnabled
- True if the cache should be enabled, false otherwise- Returns:
- The current service
-
withChunkedTransferDecoding
Instruct this service to (not) decode any requests with Transfer-Encoding value "chunked". Use this method in case the web container in which this service is running does not decode chunked transfers itself. By default, chunked decoding is disabled.- Parameters:
isEnabled
- True if chunked requests should be decoded, false otherwise.- Returns:
- The current service
-
withUploadExpirationPeriod
You can set the number of milliseconds after which an upload is considered as expired and available for cleanup.- Parameters:
expirationPeriod
- The number of milliseconds after which an upload expires and can be removed- Returns:
- The current service
-
withDownloadFeature
Enable the unofficial `download` extension that also allows you to download uploaded bytes. By default this feature is disabled.- Returns:
- The current service
-
addTusExtension
Add a custom (application-specific) extension that implements theTusExtension
interface. For example you can add your own extension that checks authentication and authorization policies within your application for the user doing the upload.- Parameters:
feature
- The custom extension implementation- Returns:
- The current service
-
disableTusExtension
Disable the TusExtension for which the getName() method matches the provided string. The default extensions have names "creation", "checksum", "expiration", "concatenation", "termination" and "download". You cannot disable the "core" feature.- Parameters:
extensionName
- The name of the extension to disable- Returns:
- The current service
-
getSupportedHttpMethods
Get all HTTP methods that are supported by this TusUploadService based on the enabled and/or disabled tus extensions.- Returns:
- The set of enabled HTTP methods
-
getEnabledFeatures
Get the set of enabled Tus extensions.- Returns:
- The set of active extensions
-
process
public void process(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse) throws IOException Process a tus upload request. Use this method to process any request made to the main and sub tus upload endpoints. This corresponds to the path specified in the withUploadUri() method and any sub-path of that URI.- Parameters:
servletRequest
- TheHttpServletRequest
of the requestservletResponse
- TheHttpServletResponse
of the request- Throws:
IOException
- When saving bytes or information of this requests fails
-
process
public void process(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, String ownerKey) throws IOException Process a tus upload request that belongs to a specific owner. Use this method to process any request made to the main and sub tus upload endpoints. This corresponds to the path specified in the withUploadUri() method and any sub-path of that URI.- Parameters:
servletRequest
- TheHttpServletRequest
of the requestservletResponse
- TheHttpServletResponse
of the requestownerKey
- A unique identifier of the owner (group) of this upload- Throws:
IOException
- When saving bytes or information of this requests fails
-
getUploadedBytes
Method to retrieve the bytes that were uploaded to a specific upload URI.- Parameters:
uploadUri
- The URI of the upload- Returns:
- An
InputStream
that will stream the uploaded bytes - Throws:
IOException
- When the retreiving the uploaded bytes failsTusException
- When the upload is still in progress or cannot be found
-
getUploadedBytes
public InputStream getUploadedBytes(String uploadUri, String ownerKey) throws IOException, TusException Method to retrieve the bytes that were uploaded to a specific upload URI.- Parameters:
uploadUri
- The URI of the uploadownerKey
- The key of the owner of this upload- Returns:
- An
InputStream
that will stream the uploaded bytes - Throws:
IOException
- When the retreiving the uploaded bytes failsTusException
- When the upload is still in progress or cannot be found
-
getUploadInfo
Get the information on the upload corresponding to the given upload URI.- Parameters:
uploadUri
- The URI of the upload- Returns:
- Information on the upload
- Throws:
IOException
- When retrieving the upload information failsTusException
- When the upload is still in progress or cannot be found
-
getUploadInfo
Get the information on the upload corresponding to the given upload URI.- Parameters:
uploadUri
- The URI of the uploadownerKey
- The key of the owner of this upload- Returns:
- Information on the upload
- Throws:
IOException
- When retrieving the upload information failsTusException
- When the upload is still in progress or cannot be found
-
deleteUpload
Method to delete an upload associated with the given upload URL. Invoke this method if you no longer need the upload.- Parameters:
uploadUri
- The upload URI- Throws:
IOException
TusException
-
deleteUpload
Method to delete an upload associated with the given upload URL. Invoke this method if you no longer need the upload.- Parameters:
uploadUri
- The upload URIownerKey
- The key of the owner of this upload- Throws:
IOException
TusException
-
cleanup
This method should be invoked periodically. It will cleanup any expired uploads and stale locks- Throws:
IOException
- When cleaning fails
-
processLockedRequest
protected void processLockedRequest(HttpMethod method, TusServletRequest request, TusServletResponse response, String ownerKey) throws IOException - Throws:
IOException
-
executeProcessingByFeatures
protected void executeProcessingByFeatures(HttpMethod method, TusServletRequest servletRequest, TusServletResponse servletResponse, String ownerKey) throws IOException, TusException - Throws:
IOException
TusException
-
validateRequest
protected void validateRequest(HttpMethod method, jakarta.servlet.http.HttpServletRequest servletRequest, String ownerKey) throws TusException, IOException - Throws:
TusException
IOException
-
processTusException
protected void processTusException(HttpMethod method, TusServletRequest request, TusServletResponse response, String ownerKey, TusException exception) throws IOException - Throws:
IOException
-