From 13d38291bd07401bd16b6e6805edc72bf0029b2f Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 17 Aug 2019 15:17:02 +0200 Subject: Fetch and store thumbnails via storage modules This will allow us to remotely store thumbnails in case of S3. For S3 the thumb bucket is configurable to allow these to be stored separately. The S3 key for thumbnails does not conflict with files, so these can be stored in the same bucket if needed. --- fbin/file_storage/filesystem.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'fbin/file_storage/filesystem.py') diff --git a/fbin/file_storage/filesystem.py b/fbin/file_storage/filesystem.py index 3b46e34..1259002 100644 --- a/fbin/file_storage/filesystem.py +++ b/fbin/file_storage/filesystem.py @@ -8,6 +8,7 @@ class Storage(BaseStorage): def __init__(self, app): super().__init__(app) os.makedirs(self.app.config['FILE_DIRECTORY'], exist_ok=True) + os.makedirs(self.app.config['THUMB_DIRECTORY'], exist_ok=True) def store_file(self, uploaded_file, file_hash, user, ip): size = uploaded_file.content_length @@ -45,3 +46,17 @@ class Storage(BaseStorage): def temp_file(self, f): with open(f.get_path(), 'rb') as f: yield f + + def get_thumbnail(self, f): + path = f.get_thumb_path() + if not os.path.exists(path): + return + return path + + def store_thumbnail(self, f, stream): + path = f.get_thumb_path() + with open(path, 'wb') as f: + buf = stream.read(1024*10) + while buf: + f.write(buf) + buf = stream.read(1024*10) -- cgit v1.2.3