Skip to content

Commit 66d9e43

Browse files
committed
调整文件删除方式
1 parent bcf0be3 commit 66d9e43

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

main.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ async def delete_expire_files():
5151
async with AsyncSession(engine, expire_on_commit=False) as s:
5252
query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0))
5353
exps = (await s.execute(query)).scalars().all()
54-
files = [{'type': old.type, 'text': old.text} for old in exps]
54+
files = []
55+
exps_ids = []
56+
for exp in exps:
57+
if exp.type != "text":
58+
files.append(exp.text)
59+
exps_ids.append(exp.id)
5560
await storage.delete_files(files)
56-
exps_ids = [exp.id for exp in exps]
5761
query = delete(Codes).where(Codes.id.in_(exps_ids))
5862
await s.execute(query)
5963
await s.commit()
@@ -83,9 +87,11 @@ async def admin_post(s: AsyncSession = Depends(get_session)):
8387
async def admin_delete(code: str, s: AsyncSession = Depends(get_session)):
8488
query = select(Codes).where(Codes.code == code)
8589
file = (await s.execute(query)).scalars().first()
86-
await storage.delete_file({'type': file.type, 'text': file.text})
87-
await s.delete(file)
88-
await s.commit()
90+
if file:
91+
if file.type != 'text':
92+
await storage.delete_file(file.text)
93+
await s.delete(file)
94+
await s.commit()
8995
return {'detail': '删除成功'}
9096

9197

@@ -115,7 +121,8 @@ async def index(code: str, ip: str = Depends(ip_limit), s: AsyncSession = Depend
115121
error_count = settings.ERROR_COUNT - ip_limit.add_ip(ip)
116122
raise HTTPException(status_code=404, detail=f"取件码错误,错误{error_count}次将被禁止10分钟")
117123
if info.exp_time < datetime.datetime.now() or info.count == 0:
118-
await storage.delete_file({'type': info.type, 'text': info.text})
124+
if info.type != "text":
125+
await storage.delete_file(info.text)
119126
await s.delete(info)
120127
await s.commit()
121128
raise HTTPException(status_code=404, detail="取件码已过期,请联系寄件人")

storage.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,13 @@ async def save_file(self, file: UploadFile, text: str):
4646
filepath = await self.get_filepath(text)
4747
await asyncio.to_thread(self._save, filepath, file.file)
4848

49-
async def delete_file(self, file):
50-
# 是文件就删除
51-
if file['type'] != 'text':
52-
filepath = self.DATA_ROOT / file['text'].lstrip(self.STATIC_URL + '/')
53-
await asyncio.to_thread(os.remove, filepath)
54-
55-
async def delete_files(self, files):
56-
for file in files:
57-
if file['type'] != 'text':
58-
await self.delete_file(file)
49+
async def delete_file(self, text: str):
50+
filepath = await self.get_filepath(text)
51+
await asyncio.to_thread(os.remove, filepath)
52+
53+
async def delete_files(self, texts):
54+
tasks = [self.delete_file(text) for text in texts]
55+
await asyncio.gather(*tasks)
5956

6057

6158
STORAGE_ENGINE = {

0 commit comments

Comments
 (0)