From 2498f142782e26681c542dfca4b3f9fc4ff64808 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 29 Oct 2020 19:45:05 +0100 Subject: Clean up leftover file path references This change removes and direct references to the filesystem from the pyfbin code other that file_storage.filesystem. This should hopyfully be the last changes needed for pyfbin to be successfully run using the file_storage.s3 module without any issues. --- fbin/file_storage/filesystem.py | 46 +++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 11 deletions(-) (limited to 'fbin/file_storage') diff --git a/fbin/file_storage/filesystem.py b/fbin/file_storage/filesystem.py index 7951d88..87d908e 100644 --- a/fbin/file_storage/filesystem.py +++ b/fbin/file_storage/filesystem.py @@ -10,6 +10,24 @@ class Storage(BaseStorage): os.makedirs(self.app.config['FILE_DIRECTORY'], exist_ok=True) os.makedirs(self.app.config['THUMB_DIRECTORY'], exist_ok=True) + def _get_legacy_path(self, file_hash, filename): + return os.path.join(self.app.config['FILE_DIRECTORY'], file_hash + os.path.splitext(filename)[1]) + + def _get_path(self, file_hash): + return os.path.join(self.app.config['FILE_DIRECTORY'], file_hash) + + def _find_path(self, f): + path = self._get_legacy_path(f.hash, f.filename) + if path and os.path.exists(path): + return path + path = self._get_path(f.hash) + if path and os.path.exists(path): + return path + return None + + def get_thumb_path(self, f): + return os.path.join(self.app.config['THUMB_DIRECTORY'], f.hash + '.jpg') + def upload_file(self, uploaded_file, file_hash, user): size = uploaded_file.content_length if hasattr(uploaded_file.stream, 'file'): @@ -20,7 +38,7 @@ class Storage(BaseStorage): uploaded_file.save(temp.file) temp_path = temp.name size = os.path.getsize(temp_path) - new_path = os.path.join(self.app.config['FILE_DIRECTORY'], file_hash + os.path.splitext(uploaded_file.filename)[1]) + new_path = self._get_path(file_hash) os.rename(temp_path, new_path) if self.app.config.get('DESTINATION_MODE'): os.chmod(new_path, self.app.config.get('DESTINATION_MODE')) @@ -36,33 +54,39 @@ class Storage(BaseStorage): raise def file_exists(self, f): - path = f.get_path() - return os.path.exists(path) + path = self._find_path(f) + return path and os.path.exists(path) def get_file(self, f): - path = f.get_path() - if not os.path.exists(path): + path = self._find_path(f) + if not path or not os.path.exists(path): return return path def delete_file(self, f): - path = f.get_path() - if os.path.exists(path): + path = self._find_path(f) + if path and os.path.exists(path): os.unlink(path) + thumb_path = self.get_thumb_path(f) + if thumb_path and os.path.exists(thumb_path): + os.unlink(thumb_path) @contextlib.contextmanager def temp_file(self, f): - with open(f.get_path(), 'rb') as f: + path = self._find_path(f) + if not path or not os.path.exists(path): + raise FileNotFoundError(path) + with open(path, 'rb') as f: yield f def get_thumbnail(self, f): - path = f.get_thumb_path() - if not os.path.exists(path): + path = self.get_thumb_path(f) + if not path or not os.path.exists(path): return return path def store_thumbnail(self, f, stream): - path = f.get_thumb_path() + path = self.get_thumb_path(f) with open(path, 'wb') as f: buf = stream.read(1024*10) while buf: -- cgit v1.2.3