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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import datetime
from ..db import db, File
from .exceptions import FileSizeError
class BaseStorage:
def __init__(self, app):
self.app = app
def verify_file(self, file):
user = file.user_id is not None
size_limit = self.app.config.get('USER_FILE_SIZE_LIMIT' if user else 'ANONYMOUS_FILE_SIZE_LIMIT')
if size_limit is not None and file.size > size_limit:
raise FileSizeError('The file size is too large (max {})'.format(File.pretty_size(size_limit)))
def upload_file(self, uploaded_file, file_hash, user):
'''Upload data from uploaded_file.'''
raise NotImplementedError()
def add_file(self, file_hash, filename, size, user=None, ip=None, verify=True):
'''Adds the file to the database.
Call from store_file after the file is successfully stored.'''
f = File(file_hash, filename, size, datetime.datetime.utcnow(), user.id if user else None, ip)
# Raises on invalid files
self.verify_file(f)
db.session.add(f)
db.session.commit()
db.session.refresh(f)
return f
def store_file(self, uploaded_file, file_hash, filename, user, ip):
'''Store uploaded_file.'''
raise NotImplementedError()
def file_exists(self, f):
'''Return True if the specified file exists. '''
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()
def get_thumbnail(self, f):
'''Return a file object for the specified file's thumbnail.
Subclasses can also return a flask.Response instance if required.'''
raise NotImplementedError()
def store_thumbnail(self, f, stream):
'''Store thumbnail for the specified file.'''
raise NotImplementedError()
|