Compare commits
3 Commits
39d24e56c3
...
19d989b696
Author | SHA1 | Date |
---|---|---|
Emi Simpson | 19d989b696 | |
Emi Simpson | 74b6f986ee | |
Emi Simpson | 64ddfdb702 |
|
@ -1,4 +1,4 @@
|
|||
"""add file expirations [creates legacy files]
|
||||
"""add file expirations
|
||||
|
||||
Revision ID: 939a08e1d6e5
|
||||
Revises: 7e246705da6a
|
||||
|
@ -61,26 +61,29 @@ def upgrade():
|
|||
|
||||
# List of file hashes which have not expired yet
|
||||
# This could get really big for some servers
|
||||
unexpired_files = set(os.listdir(storage))
|
||||
try:
|
||||
unexpired_files = os.listdir(storage)
|
||||
except FileNotFoundError:
|
||||
return # There are no currently unexpired files
|
||||
|
||||
# Calculate an expiration date for all existing files
|
||||
files = session.scalars(
|
||||
sa.select(File)
|
||||
.where(
|
||||
sa.not_(File.removed)
|
||||
sa.not_(File.removed),
|
||||
File.sha256.in_(unexpired_files)
|
||||
)
|
||||
)
|
||||
for file in files:
|
||||
if file.sha256 in unexpired_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
|
||||
op.execute(
|
||||
sa.update(UpdatedFile)
|
||||
.where(UpdatedFile.c.id == file.id)
|
||||
.values({'expiration': int(file_birth + max_age)})
|
||||
)
|
||||
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
|
||||
op.execute(
|
||||
sa.update(UpdatedFile)
|
||||
.where(UpdatedFile.c.id == file.id)
|
||||
.values({'expiration': int(file_birth + max_age)})
|
||||
)
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('file', 'expiration')
|
||||
|
|
Loading…
Reference in New Issue