summaryrefslogtreecommitdiff
path: root/fbin/file_storage/filesystem.py
diff options
context:
space:
mode:
Diffstat (limited to 'fbin/file_storage/filesystem.py')
-rw-r--r--fbin/file_storage/filesystem.py24
1 files changed, 16 insertions, 8 deletions
diff --git a/fbin/file_storage/filesystem.py b/fbin/file_storage/filesystem.py
index 3a640bb..7951d88 100644
--- a/fbin/file_storage/filesystem.py
+++ b/fbin/file_storage/filesystem.py
@@ -10,7 +10,7 @@ class Storage(BaseStorage):
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):
+ def upload_file(self, uploaded_file, file_hash, user):
size = uploaded_file.content_length
if hasattr(uploaded_file.stream, 'file'):
temp = None
@@ -20,17 +20,25 @@ 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])
+ os.rename(temp_path, new_path)
+ if self.app.config.get('DESTINATION_MODE'):
+ os.chmod(new_path, self.app.config.get('DESTINATION_MODE'))
+ return new_path, size
+
+ def store_file(self, uploaded_file, file_hash, user, ip):
+ file_path, size = self.upload_file(uploaded_file, file_hash, user)
try:
- new_file = self.add_file(file_hash, uploaded_file.filename, size, user, ip)
- if new_file:
- os.rename(temp_path, new_file.get_path())
- if self.app.config.get('DESTINATION_MODE'):
- os.chmod(new_file.get_path(), self.app.config.get('DESTINATION_MODE'))
- return new_file
+ return self.add_file(file_hash, uploaded_file.filename, size, user, ip)
except:
- os.unlink(temp.name)
+ if os.path.exists(file_path):
+ os.unlink(file_path)
raise
+ def file_exists(self, f):
+ path = f.get_path()
+ return os.path.exists(path)
+
def get_file(self, f):
path = f.get_path()
if not os.path.exists(path):