tus server implementation for Flask.
Following software stack is required to run this project:
If Docker is not used, a Mongoengine database is required for testing and running the example. Furthermore, the host has to be changed in tests/config.py
and example/app.py
respectively.
$ git clone https://github.com/volesen/flask-tus.git
$ cd flask-tus
# pipenv --python $(which python3) install
$ pipenv --python /path/to/python3 install
$ pipenv shell
& pip install -e .
$ git clone https://github.com/volesen/flask-tus.git
$ cd flask-tus
$ virtualenv -p python3 venv
$ source venv/bin/activate
& pip install -e .
$ git clone https://github.com/volesen/flask-tus.git
$ cd flask-tus
$ docker-compose -f docker-compose.yml up
$ python demo/app.py
$ docker-compose up
Go to 127.0.0.1:5000 and upload a file. The uploaded file will end in demo/uploads/
$ pytest tests
$ docker-compose run app pytest tests
The flask_tus extension, need to be instatiated with an app and a model and optionally a database/session object, in the case of SQLAlchemy-models, as in the following example:
from flask import Flask
from flask_sqlachemy import SQLAlchemy
from flask_tus import FlaskTus
from flask_tus.models import SQLAlchemyModel
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
db = SQLAlchemy(app)
flask_tus = FlaskTus(app, model=SQLAlchemyModel, db=db)
The flask_tus extensions supports the applicaiton facotry pattern, with the flask_tus.init_app
method.
Settings are added in app.config:
TUS_UPLOAD_URL
- Url of endpointTUS_UPLOAD_DIR
- Path to upload directory. Can be changed with custom models (eg. for saving in AWS S3)TUS_MAX_SIZE
- Max size of a file-uploadTUS_EXPIRATION
- Time allowed to complete upload (Must be insatce of datetime.timedelta)TUS_CHUNK_SIZE
- Chunk size used in calculation of MD5
The callbacks are as follows:
FlaskTus.on_create
- Callback for creation of uploadFlaskTus.pre_save
- Callback for pre-save on each chunkFlaskTus.post_save
- Callback for post-save on each chunkFlaskTus.on_complete
- Callback for completion of uploadFlaskTus.pre_delete
- Callback for pre-delete of uploadFlaskTus.post_delete
- Callback for post-delete of upload
The extension can be extended in terms of callbacks, custom methods etc. as in the following example
from flask_tus import FlaskTus
class FlaskTusExtended(FlaskTus):
def on_complete(self):
print('Succesful upload')
Custom models and features can be added by suppling a custom repository in the instantiation of the extensions object.
For the use-case based model, "MongoengineUpload", MD5 can be calculated and set for the upload by the metohod update_md5
which could be done on the flask_tus.on_completion
method, but should be done on by a task in production.
The extensions supports deletion of expired downsload by the CLI-command
$ tus delete_expired
Which can be scheduled with eg. cron
.