summaryrefslogtreecommitdiff
path: root/fbin/file_storage/base.py
blob: 6f39665574191dc3575038bf1ce54708a2d0f5b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import datetime

from .. import db

class BaseStorage:
    def __init__(self, app):
        self.app = app

    def add_file(self, file_hash, filename, size, user=None, ip=None):
        '''Adds the file to the database.

        Call from store_file after the file is successfully stored.'''
        with db.session_scope() as sess:
            f = db.File(file_hash, filename, size, datetime.datetime.utcnow(), user.id if user else None, ip)
            sess.add(f)
            sess.commit()
            sess.refresh(f)
        return f

    def store_file(self, uploaded_file, file_hash, filename, user, ip):
        '''Store uploaded_file.'''
        raise NotImplementedError()

    def get_file(self, f):
        '''Return a file object for the specified file.

        Subclasses can also return a flask.Response instance if required.'''
        raise NotImplementedError()

    def delete_file(self, f):
        '''Delete the specified file.'''
        raise NotImplementedError()

    def temp_file(self, f):
        '''Context manager which returns a temporary file for reading.

        This is used internally for eg. thumbnails.'''
        raise NotImplementedError()