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
@@ -130,8 +130,8 @@ async def index(code: str, ip: str = Depends(ip_limit), s: AsyncSession = Depend
130130
131131
132132@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 )):
133+ async def share (background_tasks : BackgroundTasks , text : str = Form (default = None ), style : str = Form (default = '2' ),
134+ value : int = Form ( default = 1 ), file : UploadFile = File (default = None ), s : AsyncSession = Depends (get_session )):
135135 code = await get_code (s )
136136 if style == '2' :
137137 if value > 7 :
@@ -148,11 +148,11 @@ async def share(text: str = Form(default=None), style: str = Form(default='2'),
148148 exp_count = - 1
149149 key = uuid .uuid4 ().hex
150150 if file :
151- file_bytes = await file .read ()
152- size = len (file_bytes )
151+ size = await storage .get_size (file )
153152 if size > settings .FILE_SIZE_LIMIT :
154153 raise HTTPException (status_code = 400 , detail = "文件过大" )
155- _text , _type , name = await storage .save_file (file , file_bytes , key ), file .content_type , file .filename
154+ _text , _type , name = await storage .get_text (file , key ), file .content_type , file .filename
155+ background_tasks .add_task (storage .save_file , file , _text )
156156 else :
157157 size , _text , _type , name = len (text ), text , 'text' , '文本分享'
158158 info = Codes (
0 commit comments