summaryrefslogtreecommitdiff
path: root/migrations/versions/939a08e1d6e5_.py
diff options
context:
space:
mode:
authorMia Herkt <mia@0x0.st>2024-09-27 17:39:18 +0200
committerMia Herkt <mia@0x0.st>2024-09-27 18:30:33 +0200
commitde19212a719b0bc6ea5455b3e32072a76d021df6 (patch)
tree6f95f343bbb3587f50739c97019f3159b4b8a095 /migrations/versions/939a08e1d6e5_.py
parenta2147cc964d385a59f077add9d7e5ba009bf9b2e (diff)
PEP8 compliance
Diffstat (limited to 'migrations/versions/939a08e1d6e5_.py')
-rw-r--r--migrations/versions/939a08e1d6e5_.py38
1 files changed, 24 insertions, 14 deletions
diff --git a/migrations/versions/939a08e1d6e5_.py b/migrations/versions/939a08e1d6e5_.py
index e389b41..9e69758 100644
--- a/migrations/versions/939a08e1d6e5_.py
+++ b/migrations/versions/939a08e1d6e5_.py
@@ -21,24 +21,29 @@ from sqlalchemy.orm import Session
import os
import time
-""" For a file of a given size, determine the largest allowed lifespan of that file
-Based on the current app's configuration: Specifically, the MAX_CONTENT_LENGTH, as well
-as FHOST_{MIN,MAX}_EXPIRATION.
+"""
+For a file of a given size, determine the largest allowed lifespan of that file
+
+Based on the current app's configuration:
+Specifically, the MAX_CONTENT_LENGTH, as well as FHOST_{MIN,MAX}_EXPIRATION.
-This lifespan may be shortened by a user's request, but no files should be allowed to
-expire at a point after this number.
+This lifespan may be shortened by a user's request, but no files should be
+allowed to expire at a point after this number.
Value returned is a duration in milliseconds.
"""
def get_max_lifespan(filesize: int) -> int:
- min_exp = current_app.config.get("FHOST_MIN_EXPIRATION", 30 * 24 * 60 * 60 * 1000)
- max_exp = current_app.config.get("FHOST_MAX_EXPIRATION", 365 * 24 * 60 * 60 * 1000)
- max_size = current_app.config.get("MAX_CONTENT_LENGTH", 256 * 1024 * 1024)
+ cfg = current_app.config
+ min_exp = cfg.get("FHOST_MIN_EXPIRATION", 30 * 24 * 60 * 60 * 1000)
+ max_exp = cfg.get("FHOST_MAX_EXPIRATION", 365 * 24 * 60 * 60 * 1000)
+ max_size = cfg.get("MAX_CONTENT_LENGTH", 256 * 1024 * 1024)
return min_exp + int((-max_exp + min_exp) * (filesize / max_size - 1) ** 3)
+
Base = automap_base()
+
def upgrade():
op.add_column('file', sa.Column('expiration', sa.BigInteger()))
@@ -48,14 +53,14 @@ def upgrade():
session = Session(bind=bind)
storage = Path(current_app.config["FHOST_STORAGE_PATH"])
- current_time = time.time() * 1000;
+ current_time = time.time() * 1000
# List of file hashes which have not expired yet
# This could get really big for some servers
try:
unexpired_files = os.listdir(storage)
except FileNotFoundError:
- return # There are no currently unexpired files
+ return # There are no currently unexpired files
# Calculate an expiration date for all existing files
@@ -65,7 +70,7 @@ def upgrade():
sa.not_(File.removed)
)
)
- updates = [] # We coalesce updates to the database here
+ updates = [] # We coalesce updates to the database here
# SQLite has a hard limit on the number of variables so we
# need to do this the slow way
@@ -74,13 +79,18 @@ def upgrade():
for file in files:
file_path = storage / file.sha256
stat = os.stat(file_path)
- max_age = get_max_lifespan(stat.st_size) # How long the file is allowed to live, in ms
- file_birth = stat.st_mtime * 1000 # When the file was created, in ms
- updates.append({'id': file.id, 'expiration': int(file_birth + max_age)})
+ # How long the file is allowed to live, in ms
+ max_age = get_max_lifespan(stat.st_size)
+ # When the file was created, in ms
+ file_birth = stat.st_mtime * 1000
+ updates.append({
+ 'id': file.id,
+ 'expiration': int(file_birth + max_age)})
# Apply coalesced updates
session.bulk_update_mappings(File, updates)
session.commit()
+
def downgrade():
op.drop_column('file', 'expiration')