Tussle
An implementation of a tusserver in Elixir
Documentation: https://hexdocs.pm/tussle/
tus is a protocol based on HTTP for resumable file uploads. Resumable means that an upload can be interrupted at any moment and can be resumed without re-uploading the previous data again.
An interruption may happen willingly, if the user wants to pause, or by accident in case of an network issue or server outage.
Tussle is capable of accepting uploads with arbitrary sizes and storing them locally
on disk, or in Amazon S3 by installing the tus_storage_s3 hex package.
Due to its modularization and extensibility, support for any other cloud provider can be easily added.
Features
This library implements the core TUS API v1.0.0 protocol and the following extensions:
- Creation Protocol (http://tus.io/protocols/resumable-upload.html#creation). Deferring the upload's length is not possible.
- Termination Protocol (http://tus.io/protocols/resumable-upload.html#termination)
- Expiration Protocol (https://tus.io/protocols/resumable-upload.html#expiration)
Installation
Add this repo to your list of dependencies in mix.exs:
def deps do
[
{:tussle, "~> 0.2.0"},
]
endUsage
1. Add new controller(s)
defmodule DemoWeb.UploadController do
use DemoWeb, :controller
use Tussle.Controller
# Optional callback before upload starts
def on_begin_upload(file) do
...
:ok # or {:error, reason} to reject the upload
end
# Optional callback when upload completes
def on_complete_upload(file) do
...
end
end2. Add routes for each of your upload controllers
The simplest way is to use the Tussle.Routes macro:
defmodule DemoWeb.Router do
use DemoWeb, :router
import Tussle.Routes
scope "/files", DemoWeb do
pipe_through :api
add_tus_routes UploadController
end
endOr define routes manually:
scope "/files", DemoWeb do
options "/", UploadController, :options
post "/", UploadController, :post
match :head, "/:uid", UploadController, :head
get "/:uid", UploadController, :get # CloudFlare compatibility
patch "/:uid", UploadController, :patch
delete "/:uid", UploadController, :delete
end⚠️ CloudFlare Compatibility Note
CloudFlare's caching layer converts HEAD requests to GET requests. The TUS protocol specifies HEAD for metadata retrieval, so this conversion can cause requests to not match HEAD routes, resulting in 404 errors.
The
add_tus_routes/1macro includes a GET route that mirrors HEAD behavior automatically. If you define routes manually, you must add the GET route yourself to ensure resumable uploads work correctly behind CloudFlare or similar CDNs.
3. Add config for each controller (see next section)
Configuration
# List all of your upload controllers
config :tussle, controllers: [DemoWeb.UploadController]
# Configuration for the DemoWeb.UploadController
config :tussle, DemoWeb.UploadController,
storage: Tussle.Storage.Local,
base_path: "priv/static/files/",
# Optional: expire unfinished uploads after N seconds
expiration_period: 300,
cache: Tussle.Cache.Memory,
# max supported file size, in bytes (default 20 MB)
max_size: 1024 * 1024 * 20storage: module which handles file storage. This library includesTussle.Storage.Local. Install thetus_storage_s3hex package for Amazon S3 support.expiration_period: expire unfinished uploads after a specified number of seconds.cache: module for handling temporary upload metadata. This library includesTussle.Cache.Memory. Install thetus_cache_redishex package for Redis support.max_size: hard limit on the maximum size an uploaded file can have.
Options for Tussle.Storage.Local
base_path: where in the filesystem the uploaded files will be stored
About This Fork
This is a maintained fork of the original tus package. The package was renamed to Tussle to allow publishing updated versions to Hex without conflicting with the original (now unmaintained) package.
Acknowledgments
Thank you to the original author of this library and all the people who graciously published their improvements that I have integrated into this fork.
- Juan-Pablo Scaletti (jpscaletti) -- original author
- Pierre-Louis Gottfrois (gottfrois) -- maintained the primary fork, merged community PRs
- Marcin Koziej (marcinkoziej) -- init_file callback, missing config handling, empty metadata fix
- Davide Colombo (davec82) -- expiration protocol, on_complete_upload result checking, empty metadata values
- Zachary Kessin (zkessin) -- location prefix support
- Stephen Solka (Clause-Logic) -- storage provider offset control
- Kevin Pan (feng19) -- source_url fix
- Ringo De Smet (ringods) -- Storage behaviour, File typespecs, metadata as map
- Alexander Buch (bucha) -- multiple cache support
- Joel Jucá (joeljuca) -- mix.exs formatting fix
Contributors
Jayson Vantuyl | Juan-Pablo Scaletti | Pierre-Louis Gottfrois | Marcin Koziej | Ringo De Smet | Joel Jucá |
Kevin Pan | Zachary Kessin | github-actions[bot] |
License
BSD-3-Clause. See LICENSE for details.