volesen / flask-tus

MIT License
6 stars 2 forks source link

flask-tus

tus server implementation for Flask.

Requirements

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.

Installation

pipenv

$ 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 .

virtualenviroment

$ git clone https://github.com/volesen/flask-tus.git
$ cd flask-tus
$ virtualenv -p python3 venv
$ source venv/bin/activate
& pip install -e .

docker

$ git clone https://github.com/volesen/flask-tus.git
$ cd flask-tus
$ docker-compose -f docker-compose.yml up

Running example

Python

$ python demo/app.py

Docker

$ docker-compose  up

Go to 127.0.0.1:5000 and upload a file. The uploaded file will end in demo/uploads/

Testing

Python

$ pytest tests

Docker

$ docker-compose run app pytest tests

Usage

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

Settings are added in app.config:

Callbacks

The callbacks are as follows:

Extending

Extension

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

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.

CLI

The extensions supports deletion of expired downsload by the CLI-command

$ tus delete_expired

Which can be scheduled with eg. cron.