|
3 | 3 | import asyncio |
4 | 4 | from pathlib import Path |
5 | 5 | import os |
| 6 | + |
6 | 7 | try: |
7 | 8 | import chardet |
8 | 9 | from fastapi import FastAPI, Depends, UploadFile, Form, File, HTTPException, BackgroundTasks |
@@ -116,6 +117,25 @@ async def index(): |
116 | 117 | return HTMLResponse(index_html) |
117 | 118 |
|
118 | 119 |
|
| 120 | +@app.post('/') |
| 121 | +async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession = Depends(get_session)): |
| 122 | + query = select(Codes).where(Codes.code == code) |
| 123 | + info = (await s.execute(query)).scalars().first() |
| 124 | + if not info: |
| 125 | + error_count = settings.ERROR_COUNT - error_ip_limit.add_ip(ip) |
| 126 | + raise HTTPException(status_code=404, detail=f"取件码错误,{error_count}次后将被禁止{settings.ERROR_MINUTE}分钟") |
| 127 | + if info.exp_time < datetime.datetime.now() or info.count == 0: |
| 128 | + raise HTTPException(status_code=404, detail="取件码已失效,请联系寄件人") |
| 129 | + await s.execute(update(Codes).where(Codes.id == info.id).values(count=info.count - 1)) |
| 130 | + await s.commit() |
| 131 | + if info.type != 'text': |
| 132 | + info.text = f'/select?code={code}' |
| 133 | + return { |
| 134 | + 'detail': f'取件成功,文件将在{settings.DELETE_EXPIRE_FILES_INTERVAL}分钟后删除', |
| 135 | + 'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code} |
| 136 | + } |
| 137 | + |
| 138 | + |
119 | 139 | @app.get('/banner') |
120 | 140 | async def banner(request: Request, s: AsyncSession = Depends(get_session)): |
121 | 141 | # 数据库查询config |
@@ -161,25 +181,6 @@ async def get_file(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession |
161 | 181 | return FileResponse(filepath, filename=info.name) |
162 | 182 |
|
163 | 183 |
|
164 | | -@app.post('/') |
165 | | -async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession = Depends(get_session)): |
166 | | - query = select(Codes).where(Codes.code == code) |
167 | | - info = (await s.execute(query)).scalars().first() |
168 | | - if not info: |
169 | | - error_count = settings.ERROR_COUNT - error_ip_limit.add_ip(ip) |
170 | | - raise HTTPException(status_code=404, detail=f"取件码错误,{error_count}次后将被禁止{settings.ERROR_MINUTE}分钟") |
171 | | - if info.exp_time < datetime.datetime.now() or info.count == 0: |
172 | | - raise HTTPException(status_code=404, detail="取件码已失效,请联系寄件人") |
173 | | - await s.execute(update(Codes).where(Codes.id == info.id).values(count=info.count - 1)) |
174 | | - await s.commit() |
175 | | - if info.type != 'text': |
176 | | - info.text = f'/select?code={code}' |
177 | | - return { |
178 | | - 'detail': f'取件成功,文件将在{settings.DELETE_EXPIRE_FILES_INTERVAL}分钟后删除', |
179 | | - 'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code} |
180 | | - } |
181 | | - |
182 | | - |
183 | 184 | @app.post('/share', dependencies=[Depends(admin_required)], description='分享文件') |
184 | 185 | async def share(background_tasks: BackgroundTasks, text: str = Form(default=None), |
185 | 186 | style: str = Form(default='2'), value: int = Form(default=1), file: UploadFile = File(default=None), |
|
0 commit comments