SolidTUS 0.0.19
See the version list below for details.
dotnet add package SolidTUS --version 0.0.19
NuGet\Install-Package SolidTUS -Version 0.0.19
<PackageReference Include="SolidTUS" Version="0.0.19" />
paket add SolidTUS --version 0.0.19
#r "nuget: SolidTUS, 0.0.19"
// Install SolidTUS as a Cake Addin #addin nuget:?package=SolidTUS&version=0.0.19 // Install SolidTUS as a Cake Tool #tool nuget:?package=SolidTUS&version=0.0.19
What is it?
A C# dotnet 7 implementation of the TUS-protocol for resumable file uploads for an ASP.NET Core application.
Why create another TUS library?
This library's purpose is to be simple and give the consumer more options on how to authorize and authenticate requests.
Which I felt that other libraries did not provide.
SolidTUS is a more Controller/Action oriented and integrates well with a Web API or an MVC project.
If you have any suggestions or improvements please do not hesitate to contribute.
Current TUS features
SolidTUS currently implements the following.
Basic features:
- Core-protocol v.1.0.0 (stop and resume uploads)
- Max-size (define a hard limit for upload size)
- Tus-metadata validation
- Options (server feature announcements)
Extensions:
Notes:
* Checksum feature does not implement the trailing header feature, i.e. A checksum value must be provided upon sending the http request.
** Termination must be implemented by yourself. See examples and documentation on how to and why.
Future goals is to implement all the extensions:
Other TUS libraries for C#
Quickstart
Add the package to your project:
$ dotnet add package SolidTUS
Register the service in the startup process:
// Register TUS services
builder.Services.AddTUS();
In your Controller
add the TusCreation
-attribute to the action method endpoint and the TusCreationContext
as parameter.
This will not upload any file (unless the client explicitly uses the TUS-extension Creation-With-Upload
feature).
This only sets the ground work for getting information such as file size, and where to upload the data.
Next the actual upload.
Set the TusUpload
-attribute and add the TusUploadContext
as a parameter
And done...
Congratulations you now have a very basic upload / pause / resume functionality. If you want to add TUS-termination then you can add the TusDelete
attribute to an action. The only requirement is that you ensure the route to the upload endpoint matches the route to the termination endpoint. To see how to implement Tus-Termination
endpoint see the wiki.
Extra options
To see all the configurations go to the wiki.
Configurations
SolidTUS can be configured through the TusOptions
object, either on startup or using environment variables.
// Custom metadata provider or set maximum TUS protocol file size
builder.Services
.AddTUS()
.Configuration(options =>
{
// A Func<string, bool> that validates the given TUS-metadata upon resource creation
options.MetadataValidator = (metadata) => metadata.ContainsKey("filename");
// This max size is different than the ASP.NET specified max size. To change the request size limit do it per Action with an attribute (recommended).
options.MaxSize = 5_000_000_000;
});
All options are mentioned in the wiki/tus-options
Note: to change request size limits see: Microsoft documentation
If you don't want to use the default FileUploadStorageHandler you can provide your own, maybe you want to save the files to a database?
// Add custom storage handler
builder.Services
.AddTUS()
.AddStorageHandler<MyStorageHandler>(); // <-- must implement IUploadStorageHandler interface
If you use the default FileUploadStorageHandler you can configure the directory where to store files:
// Add custom checksum validator
builder.Services
.AddTUS()
.FileStorageConfiguration(options =>
{
options.DirectoryPath = "path/to/where/save/upload/files";
});
another option is to determine where each file should be uploaded on per upload basis. In the Action
you can specify the file path:
[TusUpload]
public async Task<ActionResult> Upload(string fileId, [FromServices] TusUploadContext context)
{
// ... omitted
await context.StartAppendDataAsync(fileId, "determine/path/per/upload");
// ... omitted
}
Configuration from appsettings.json or environment variables
You can configure the Tus-Max-Size
parameter and the default file storage upload folder from the appsettings.json configuration:
{
"SolidTUS": {
"DirectoryPath": "/path/to/my/uploads",
"MaxSize": "3000000"
}
}
Environment variables are named as SolidTUS__DirectoryPath
with a double underscore (so they also can be read from a linux environment). See Microsofts documentation for naming
Contexts
The injected context classes are excluded from ModelBinding but do show up in Swagger SwashBuckle. To exclude from Swagger SwashBuckle you can annotate the contexts with the FromServices
-attribute.
IMPORTANT: Callbacks must be defined before upload starts.
The StartCreationAsync
and StartAppendDataAsync
starts upload.
TusUploadContext
Is responsible for starting the upload.
The class contains the following members:
OnUploadFinished
- A method that takes an awaitable callback. When the whole file has been completely uploaded the callback is invoked.StartAppendDataAsync
- Starts accepting the upload stream from the clientUploadFileInfo
- Contains the SolidTUS metadata about the current upload.SetExpirationStrategy
- Defines the expiration strategy for this upload. See wiki/ExpirationStrategy section.
Can only call StartAppendDataAsync
once - subsequent calls will be ignored.
The TusUploadContext
is injected from the TusUpload
-attribute.
TusUploadAttribute
Is responsible for marking the TUS-protocol upload endpoint. And needs information about 2 things:
- The file ID parameter name of type
string
- The context parameter name of type
TusUploadContext
These parameters can be tuned:
[TusUpload(FileIdParameterName = "Id", ContextParameterName = "tus")]
public async Task<ActionResult> UploadEndPoint(string Id, TusUploadContext tus)
{
/* Logic omitted ... */
}
To see all parameters see wiki/TusUploadAttribute section.
TusCreationContext
Is responsible for creating the resource metadata UploadFileInfo
. Defining the file ID and eventual any TUS-metadata.
SolidTUS implements the TUS-protocol extension creation-with-upload
thus a resource creation can contain some upload data.
Before reaching the actual Action
method the metadata validator defined in the Configuration
will run; If metadata is invalid an automatic response of 400 Bad Request will be returned, as specified in the TUS-protocol.
The class contains the following members:
StartCreationAsync
- Starts resource creationOnResourceCreated
- A method that takes a callback function, which is invoked when the resource has been successfully createdOnUploadFinished
- A method that takes a callback function, which is invoked when the partial file or whole file has finished uploading. It could be the client has sent a partial upload or the whole file.Metadata
- ADictionary<string, string>
property of the parsed TUS-metadata
The TusCreationContext
is injected from the TusCreation
-attribute.
TusCreationAttribute
Is responsible for marking the TUS-creation endpoint and needs information about the context parameter name.
[TusCreation(ContextParameterName = "creationContext")]
public async Task<ActionResult> CreationEndPoint(TusUploadContext creationContext)
{
/* Logic omitted ... */
}
The TUS protocol with SolidTUS simplified
In essence the client sends a request to an endpoint as marked by the TusCreation
attribute:
POST /files HTTP/1.1
Host: tus.example.org
Content-Length: 0
Upload-Length: 100
Tus-Resumable: 1.0.0
The server then knows to expect an upload file with a total size of 100 bytes and at which point SolidTUS creates an UploadFileInfo
which contains this metadata.
The server responds on success where the file can be uploaded:
HTTP/1.1 201 Created
Location: https://tus.example.org/files/24e533e02ec3bc40c387f1a0e460e216
Tus-Resumable: 1.0.0
In this example the file has an ID of 24e533e02ec3bc40c387f1a0e460e216.
The client then uploads the data to that location as marked by the TusUpload
attribute:
PATCH /files/24e533e02ec3bc40c387f1a0e460e216 HTTP/1.1
Host: tus.example.org
Content-Type: application/offset+octet-stream
Content-Length: 30
Upload-Offset: 70
Tus-Resumable: 1.0.0
[remaining 30 bytes]
After some while the upload starts from byte 70 and has a total size of 100, the missing 30 bytes are in the PATCH body. This data is directed to the OnPartialUploadAsync
method in the IUploadStorageHandler
.
When finished successfully the server responds:
HTTP/1.1 204 No Content
Tus-Resumable: 1.0.0
Upload-Offset: 100
Test methodology
Using unit tests and manually making TUS-request with the official javascript client in the examples folder.
TODO
- Create wiki pages for all the configuration options
- Create wiki pages for library design, and how to extend
- Implement all TUS extension features
- Add section in readme for examples
References
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.0.34 | 149 | 2/2/2024 |
0.0.33 | 123 | 2/1/2024 |
0.0.32 | 144 | 1/12/2024 |
0.0.31 | 149 | 1/4/2024 |
0.0.30 | 134 | 1/4/2024 |
0.0.29 | 223 | 1/3/2024 |
0.0.28 | 137 | 1/2/2024 |
0.0.27 | 129 | 12/21/2023 |
0.0.26 | 130 | 12/18/2023 |
0.0.25 | 114 | 12/11/2023 |
0.0.24 | 108 | 12/11/2023 |
0.0.23 | 113 | 12/11/2023 |
0.0.22 | 126 | 12/7/2023 |
0.0.21 | 126 | 12/5/2023 |
0.0.20 | 144 | 10/9/2023 |
0.0.19 | 139 | 9/25/2023 |
0.0.18 | 147 | 9/23/2023 |
0.0.17 | 171 | 4/27/2023 |
0.0.16 | 422 | 8/8/2022 |
0.0.15 | 399 | 8/6/2022 |
0.0.14 | 411 | 8/5/2022 |
0.0.13 | 413 | 7/21/2022 |
0.0.12 | 403 | 7/21/2022 |
0.0.11 | 408 | 7/21/2022 |
0.0.10 | 431 | 7/13/2022 |
0.0.9 | 452 | 7/13/2022 |
0.0.8 | 439 | 7/13/2022 |
0.0.7 | 425 | 7/12/2022 |