🐛 fix(upload): 定时清理保护已发布文件

This commit is contained in:
orion committed 2026-09-18 16:57:59 +08:00
1 parent dfb392545a
commit 1023292e71
2 files changed
+32 -23

No files matched your search

+19 -23
View File
@@ -103,35 +103,31 @@ async def clean_incomplete_uploads():
await asyncio.sleep(3600)
async def clean_expired_presign_session(upload_id, now):
async with upload_lock(upload_id):
session = await PresignUploadSession.filter(upload_id=upload_id, expires_at__lt=now).first()
if session is None:
return
published = await FileCodes.filter(upload_id=upload_id).exists()
if not published:
storage = storages[settings.file_storage]()
if await storage.file_exists(session.save_path):
await storage.delete_file(FileCodes(file_path=os.path.dirname(session.save_path),uuid_file_name=os.path.basename(session.save_path)))
await StorageReservation.filter(token=f"presign:{upload_id}").delete()
await session.delete()
async def clean_expired_presign_sessions():
while True:
try:
await refresh_settings()
storage: FileStorageInterface = storages[settings.file_storage]()
now = await get_now()
expired_sessions = await PresignUploadSession.filter(
expires_at__lt=now
).all()
expired_sessions = await PresignUploadSession.filter(expires_at__lt=now).all()
for session in expired_sessions:
if session.mode in {"direct", "cancelled"}:
try:
if await storage.file_exists(session.save_path):
await storage.delete_file(
FileCodes(
file_path=os.path.dirname(session.save_path),
uuid_file_name=os.path.basename(session.save_path),
)
)
except Exception as e:
logger.error(
"清理过期直传文件失败 "
f"upload_id={session.upload_id}: {e}"
)
continue
await StorageReservation.filter(
token=f"presign:{session.upload_id}"
).delete()
await session.delete()
try:
await clean_expired_presign_session(session.upload_id, now)
except Exception as e:
logger.error(f"清理上传失败,保留会话等待重试 upload_id={session.upload_id}: {e}")
except Exception as e:
logger.error(f"清理过期预签名会话异常: {e}")
finally: