summaryrefslogtreecommitdiff
path: root/fbin/file_storage/filesystem.py
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2020-10-28 20:14:05 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2020-10-28 20:14:05 +0100
commitb1ed551c3125278d14a69750fea2bfe39cf68530 (patch)
treefe7a89bd5bea3d53b18009a55e29b9f4181ca600 /fbin/file_storage/filesystem.py
parent0faa732c9a3e1ffced2b26bee682f513b0e5f0ae (diff)
Add fbin-backup.py
This is a backup script which will copy files to a target storage, and optionally add database entries as well. It works by specifying a source and a target config file. Both should have separate storage configured, and files will be copied from the source storage to the target storage. The list of files to copy is read from the source database, which means this must be accessible from where the script is run, not just the storage. If updating databse entries the target database must also be accessible. The target database is also check for any existing file hashes, and any files that would cause a collision will be skipped.
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):