调整文件删除方式

This commit is contained in:
veoco
2022-12-13 14:18:02 +08:00
parent dc353d624f
commit 9a190484bf
2 changed files with 19 additions and 15 deletions
+13 -6
View File
@@ -51,9 +51,13 @@ async def delete_expire_files():
async with AsyncSession(engine, expire_on_commit=False) as s: async with AsyncSession(engine, expire_on_commit=False) as s:
query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0)) query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0))
exps = (await s.execute(query)).scalars().all() exps = (await s.execute(query)).scalars().all()
files = [{'type': old.type, 'text': old.text} for old in exps] files = []
exps_ids = []
for exp in exps:
if exp.type != "text":
files.append(exp.text)
exps_ids.append(exp.id)
await storage.delete_files(files) await storage.delete_files(files)
exps_ids = [exp.id for exp in exps]
query = delete(Codes).where(Codes.id.in_(exps_ids)) query = delete(Codes).where(Codes.id.in_(exps_ids))
await s.execute(query) await s.execute(query)
await s.commit() await s.commit()
@@ -83,9 +87,11 @@ async def admin_post(s: AsyncSession = Depends(get_session)):
async def admin_delete(code: str, s: AsyncSession = Depends(get_session)): async def admin_delete(code: str, s: AsyncSession = Depends(get_session)):
query = select(Codes).where(Codes.code == code) query = select(Codes).where(Codes.code == code)
file = (await s.execute(query)).scalars().first() file = (await s.execute(query)).scalars().first()
await storage.delete_file({'type': file.type, 'text': file.text}) if file:
await s.delete(file) if file.type != 'text':
await s.commit() await storage.delete_file(file.text)
await s.delete(file)
await s.commit()
return {'detail': '删除成功'} return {'detail': '删除成功'}
@@ -115,7 +121,8 @@ async def index(code: str, ip: str = Depends(ip_limit), s: AsyncSession = Depend
error_count = settings.ERROR_COUNT - ip_limit.add_ip(ip) error_count = settings.ERROR_COUNT - ip_limit.add_ip(ip)
raise HTTPException(status_code=404, detail=f"取件码错误,错误{error_count}次将被禁止10分钟") raise HTTPException(status_code=404, detail=f"取件码错误,错误{error_count}次将被禁止10分钟")
if info.exp_time < datetime.datetime.now() or info.count == 0: if info.exp_time < datetime.datetime.now() or info.count == 0:
await storage.delete_file({'type': info.type, 'text': info.text}) if info.type != "text":
await storage.delete_file(info.text)
await s.delete(info) await s.delete(info)
await s.commit() await s.commit()
raise HTTPException(status_code=404, detail="取件码已过期,请联系寄件人") raise HTTPException(status_code=404, detail="取件码已过期,请联系寄件人")
+6 -9
View File
@@ -46,16 +46,13 @@ class FileSystemStorage:
filepath = await self.get_filepath(text) filepath = await self.get_filepath(text)
await asyncio.to_thread(self._save, filepath, file.file) await asyncio.to_thread(self._save, filepath, file.file)
async def delete_file(self, file): async def delete_file(self, text: str):
# 是文件就删除 filepath = await self.get_filepath(text)
if file['type'] != 'text': await asyncio.to_thread(os.remove, filepath)
filepath = self.DATA_ROOT / file['text'].lstrip(self.STATIC_URL + '/')
await asyncio.to_thread(os.remove, filepath)
async def delete_files(self, files): async def delete_files(self, texts):
for file in files: tasks = [self.delete_file(text) for text in texts]
if file['type'] != 'text': await asyncio.gather(*tasks)
await self.delete_file(file)
STORAGE_ENGINE = { STORAGE_ENGINE = {