39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
from datetime import datetime, timedelta
|
|
from models import db, RoomFile
|
|
import os
|
|
|
|
def cleanup_trash():
|
|
"""
|
|
Permanently deletes files that have been in trash for more than 30 days.
|
|
This function should be called by a scheduler (e.g., cron job) daily.
|
|
"""
|
|
# Calculate the cutoff date (30 days ago)
|
|
cutoff_date = datetime.utcnow() - timedelta(days=30)
|
|
|
|
# Find all files that were deleted before the cutoff date
|
|
files_to_delete = RoomFile.query.filter(
|
|
RoomFile.deleted == True,
|
|
RoomFile.deleted_at <= cutoff_date
|
|
).all()
|
|
|
|
for file in files_to_delete:
|
|
try:
|
|
# Delete the file from storage if it's a file
|
|
if file.type == 'file':
|
|
file_path = os.path.join('/data/rooms', str(file.room_id), file.path, file.name)
|
|
if os.path.exists(file_path):
|
|
os.remove(file_path)
|
|
|
|
# Delete the database record
|
|
db.session.delete(file)
|
|
|
|
except Exception as e:
|
|
print(f"Error deleting file {file.name}: {str(e)}")
|
|
continue
|
|
|
|
# Commit all changes
|
|
try:
|
|
db.session.commit()
|
|
except Exception as e:
|
|
print(f"Error committing changes: {str(e)}")
|
|
db.session.rollback() |