44import asyncio
55from pathlib import Path
66
7- from fastapi import FastAPI , Depends , UploadFile , Form , File , HTTPException
7+ from fastapi import FastAPI , Depends , UploadFile , Form , File , HTTPException , BackgroundTasks
88from starlette .responses import HTMLResponse , FileResponse
99from starlette .staticfiles import StaticFiles
1010
@@ -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)):
8387async 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 = "取件码已过期,请联系寄件人" )
@@ -130,8 +137,8 @@ async def index(code: str, ip: str = Depends(ip_limit), s: AsyncSession = Depend
130137
131138
132139@app .post ('/share' )
133- async def share (text : str = Form (default = None ), style : str = Form (default = '2' ), value : int = Form ( default = 1 ),
134- file : UploadFile = File (default = None ), s : AsyncSession = Depends (get_session )):
140+ async def share (background_tasks : BackgroundTasks , text : str = Form (default = None ), style : str = Form (default = '2' ),
141+ value : int = Form ( default = 1 ), file : UploadFile = File (default = None ), s : AsyncSession = Depends (get_session )):
135142 code = await get_code (s )
136143 if style == '2' :
137144 if value > 7 :
@@ -148,11 +155,11 @@ async def share(text: str = Form(default=None), style: str = Form(default='2'),
148155 exp_count = - 1
149156 key = uuid .uuid4 ().hex
150157 if file :
151- file_bytes = await file .read ()
152- size = len (file_bytes )
158+ size = await storage .get_size (file )
153159 if size > settings .FILE_SIZE_LIMIT :
154160 raise HTTPException (status_code = 400 , detail = "文件过大" )
155- _text , _type , name = await storage .save_file (file , file_bytes , key ), file .content_type , file .filename
161+ _text , _type , name = await storage .get_text (file , key ), file .content_type , file .filename
162+ background_tasks .add_task (storage .save_file , file , _text )
156163 else :
157164 size , _text , _type , name = len (text ), text , 'text' , '文本分享'
158165 info = Codes (
0 commit comments