🐛 fix(upload): 定时清理保护已发布文件
This commit is contained in:
1 parent
dfb392545a
commit
1023292e71
2 files changed
+32
-23
No files matched your search
+19
-23
@@ -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:
|
||||
|
||||
@@ -93,3 +93,16 @@ class WebDAVStreamTests(WebDAVFixture):
|
||||
self.assertEqual((await PresignUploadSession.get(upload_id='direct')).mode,'cancelled')
|
||||
self.assertTrue(await StorageReservation.filter(token='presign:direct').exists())
|
||||
with self.assertRaises(HTTPException):await _get_valid_session('direct')
|
||||
|
||||
async def test_expired_presign_cleanup_preserves_published_file_with_residual_session(self):
|
||||
from apps.base.models import FileCodes,PresignUploadSession
|
||||
from core.tasks import clean_expired_presign_session
|
||||
from core.utils import get_now
|
||||
import datetime
|
||||
now=await get_now()
|
||||
await PresignUploadSession.create(upload_id='published-stale',file_name='old.txt',file_size=4,save_path='old.txt',mode='direct',expire_value=1,expire_style='day',expires_at=now-datetime.timedelta(minutes=1))
|
||||
await FileCodes.create(code='OLDKEEP1',upload_id='published-stale',prefix='old',suffix='.txt',file_path='',uuid_file_name='old.txt',expired_count=-1)
|
||||
self.files['/dav/old.txt']=b'keep'
|
||||
await clean_expired_presign_session('published-stale',now)
|
||||
self.assertEqual(self.files['/dav/old.txt'],b'keep')
|
||||
self.assertFalse(await PresignUploadSession.filter(upload_id='published-stale').exists())
|
||||
Reference in new issue
Block a user