From 1023292e7113c5532e60adb0656ec9db139828f8 Mon Sep 17 00:00:00 2001 From: Orion Date: Fri, 18 Sep 2026 16:57:59 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(upload):=20=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E6=B8=85=E7=90=86=E4=BF=9D=E6=8A=A4=E5=B7=B2=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/tasks.py | 42 ++++++++++++++---------------- tests/test_webdav_stream_upload.py | 13 +++++++++ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/core/tasks.py b/core/tasks.py index add243f..3ae15b3 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -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: diff --git a/tests/test_webdav_stream_upload.py b/tests/test_webdav_stream_upload.py index 844e995..6152eab 100644 --- a/tests/test_webdav_stream_upload.py +++ b/tests/test_webdav_stream_upload.py @@ -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())