11import datetime
22import uuid
3- import random
43import asyncio
54from pathlib import Path
65
76from fastapi import FastAPI , Depends , UploadFile , Form , File , HTTPException , BackgroundTasks
87from starlette .responses import HTMLResponse , FileResponse
98from starlette .staticfiles import StaticFiles
109
11- from sqlalchemy import or_ , select , update , delete
10+ from sqlalchemy import select , update
1211from sqlalchemy .ext .asyncio .session import AsyncSession
1312
1413import settings
15- from database import get_session , Codes , init_models , engine
16- from storage import STORAGE_ENGINE
14+ from utils import delete_expire_files , storage , get_code
15+ from database import get_session , Codes , init_models
1716from depends import admin_required , IPRateLimit
1817
1918app = FastAPI (debug = settings .DEBUG )
2524STATIC_URL = settings .STATIC_URL
2625app .mount (STATIC_URL , StaticFiles (directory = DATA_ROOT ), name = "static" )
2726
28- storage = STORAGE_ENGINE [settings .STORAGE_ENGINE ]()
29-
3027
3128@app .on_event ('startup' )
3229async def startup ():
@@ -46,31 +43,6 @@ async def startup():
4643ip_limit = IPRateLimit ()
4744
4845
49- async def delete_expire_files ():
50- while True :
51- async with AsyncSession (engine , expire_on_commit = False ) as s :
52- query = select (Codes ).where (or_ (Codes .exp_time < datetime .datetime .now (), Codes .count == 0 ))
53- exps = (await s .execute (query )).scalars ().all ()
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 )
60- await storage .delete_files (files )
61- query = delete (Codes ).where (Codes .id .in_ (exps_ids ))
62- await s .execute (query )
63- await s .commit ()
64- await asyncio .sleep (random .randint (60 , 300 ))
65-
66-
67- async def get_code (s : AsyncSession ):
68- code = random .randint (10000 , 99999 )
69- while (await s .execute (select (Codes .id ).where (Codes .code == code ))).scalar ():
70- code = random .randint (10000 , 99999 )
71- return str (code )
72-
73-
7446@app .get (f'/{ settings .ADMIN_ADDRESS } ' )
7547async def admin ():
7648 return HTMLResponse (admin_html )
@@ -138,7 +110,8 @@ async def index(code: str, ip: str = Depends(ip_limit), s: AsyncSession = Depend
138110
139111@app .post ('/share' )
140112async 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 )):
113+ value : int = Form (default = 1 ), file : UploadFile = File (default = None ),
114+ s : AsyncSession = Depends (get_session )):
142115 code = await get_code (s )
143116 if style == '2' :
144117 if value > 7 :
@@ -162,20 +135,11 @@ async def share(background_tasks: BackgroundTasks, text: str = Form(default=None
162135 background_tasks .add_task (storage .save_file , file , _text )
163136 else :
164137 size , _text , _type , name = len (text ), text , 'text' , '文本分享'
165- info = Codes (
166- code = code ,
167- text = _text ,
168- size = size ,
169- type = _type ,
170- name = name ,
171- count = exp_count ,
172- exp_time = exp_time ,
173- key = key
174- )
138+ info = Codes (code = code , text = _text , size = size , type = _type , name = name , count = exp_count , exp_time = exp_time , key = key )
175139 s .add (info )
176140 await s .commit ()
177141 return {
178- 'detail' : '分享成功,请点击文件箱查看取件码 ' ,
142+ 'detail' : '分享成功,请点击取件码按钮查看上传列表 ' ,
179143 'data' : {'code' : code , 'key' : key , 'name' : name , 'text' : _text }
180144 }
181145
0 commit comments