update:adjust file structure
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import uuid
|
import uuid
|
||||||
import random
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -8,12 +7,12 @@ from fastapi import FastAPI, Depends, UploadFile, Form, File, HTTPException, Bac
|
|||||||
from starlette.responses import HTMLResponse, FileResponse
|
from starlette.responses import HTMLResponse, FileResponse
|
||||||
from starlette.staticfiles import StaticFiles
|
from starlette.staticfiles import StaticFiles
|
||||||
|
|
||||||
from sqlalchemy import or_, select, update, delete
|
from sqlalchemy import select, update
|
||||||
from sqlalchemy.ext.asyncio.session import AsyncSession
|
from sqlalchemy.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
import settings
|
import settings
|
||||||
from database import get_session, Codes, init_models, engine
|
from utils import delete_expire_files, storage, get_code
|
||||||
from storage import STORAGE_ENGINE
|
from database import get_session, Codes, init_models
|
||||||
from depends import admin_required, IPRateLimit
|
from depends import admin_required, IPRateLimit
|
||||||
|
|
||||||
app = FastAPI(debug=settings.DEBUG)
|
app = FastAPI(debug=settings.DEBUG)
|
||||||
@@ -25,8 +24,6 @@ if not DATA_ROOT.exists():
|
|||||||
STATIC_URL = settings.STATIC_URL
|
STATIC_URL = settings.STATIC_URL
|
||||||
app.mount(STATIC_URL, StaticFiles(directory=DATA_ROOT), name="static")
|
app.mount(STATIC_URL, StaticFiles(directory=DATA_ROOT), name="static")
|
||||||
|
|
||||||
storage = STORAGE_ENGINE[settings.STORAGE_ENGINE]()
|
|
||||||
|
|
||||||
|
|
||||||
@app.on_event('startup')
|
@app.on_event('startup')
|
||||||
async def startup():
|
async def startup():
|
||||||
@@ -46,31 +43,6 @@ admin_html = open('templates/admin.html', 'r', encoding='utf-8').read() \
|
|||||||
ip_limit = IPRateLimit()
|
ip_limit = IPRateLimit()
|
||||||
|
|
||||||
|
|
||||||
async def delete_expire_files():
|
|
||||||
while True:
|
|
||||||
async with AsyncSession(engine, expire_on_commit=False) as s:
|
|
||||||
query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0))
|
|
||||||
exps = (await s.execute(query)).scalars().all()
|
|
||||||
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)
|
|
||||||
query = delete(Codes).where(Codes.id.in_(exps_ids))
|
|
||||||
await s.execute(query)
|
|
||||||
await s.commit()
|
|
||||||
await asyncio.sleep(random.randint(60, 300))
|
|
||||||
|
|
||||||
|
|
||||||
async def get_code(s: AsyncSession):
|
|
||||||
code = random.randint(10000, 99999)
|
|
||||||
while (await s.execute(select(Codes.id).where(Codes.code == code))).scalar():
|
|
||||||
code = random.randint(10000, 99999)
|
|
||||||
return str(code)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get(f'/{settings.ADMIN_ADDRESS}')
|
@app.get(f'/{settings.ADMIN_ADDRESS}')
|
||||||
async def admin():
|
async def admin():
|
||||||
return HTMLResponse(admin_html)
|
return HTMLResponse(admin_html)
|
||||||
@@ -138,7 +110,8 @@ async def index(code: str, ip: str = Depends(ip_limit), s: AsyncSession = Depend
|
|||||||
|
|
||||||
@app.post('/share')
|
@app.post('/share')
|
||||||
async def share(background_tasks: BackgroundTasks, text: str = Form(default=None), style: str = Form(default='2'),
|
async def share(background_tasks: BackgroundTasks, text: str = Form(default=None), style: str = Form(default='2'),
|
||||||
value: int = Form(default=1), file: UploadFile = File(default=None), s: AsyncSession = Depends(get_session)):
|
value: int = Form(default=1), file: UploadFile = File(default=None),
|
||||||
|
s: AsyncSession = Depends(get_session)):
|
||||||
code = await get_code(s)
|
code = await get_code(s)
|
||||||
if style == '2':
|
if style == '2':
|
||||||
if value > 7:
|
if value > 7:
|
||||||
@@ -162,20 +135,11 @@ async def share(background_tasks: BackgroundTasks, text: str = Form(default=None
|
|||||||
background_tasks.add_task(storage.save_file, file, _text)
|
background_tasks.add_task(storage.save_file, file, _text)
|
||||||
else:
|
else:
|
||||||
size, _text, _type, name = len(text), text, 'text', '文本分享'
|
size, _text, _type, name = len(text), text, 'text', '文本分享'
|
||||||
info = Codes(
|
info = Codes(code=code, text=_text, size=size, type=_type, name=name, count=exp_count, exp_time=exp_time, key=key)
|
||||||
code=code,
|
|
||||||
text=_text,
|
|
||||||
size=size,
|
|
||||||
type=_type,
|
|
||||||
name=name,
|
|
||||||
count=exp_count,
|
|
||||||
exp_time=exp_time,
|
|
||||||
key=key
|
|
||||||
)
|
|
||||||
s.add(info)
|
s.add(info)
|
||||||
await s.commit()
|
await s.commit()
|
||||||
return {
|
return {
|
||||||
'detail': '分享成功,请点击文件箱查看取件码',
|
'detail': '分享成功,请点击取件码按钮查看上传列表',
|
||||||
'data': {'code': code, 'key': key, 'name': name, 'text': _text}
|
'data': {'code': code, 'key': key, 'name': name, 'text': _text}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-4
@@ -23,18 +23,19 @@ class FileSystemStorage:
|
|||||||
path = self.DATA_ROOT / f"upload/{now.year}/{now.month}/{now.day}/"
|
path = self.DATA_ROOT / f"upload/{now.year}/{now.month}/{now.day}/"
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
path.mkdir(parents=True)
|
path.mkdir(parents=True)
|
||||||
filepath = path / f'{key}.{ext}'
|
text = f"{self.STATIC_URL}/{(path / f'{key}.{ext}').relative_to(self.DATA_ROOT).relative_to(self.DATA_ROOT)}"
|
||||||
text = f"{self.STATIC_URL}/{filepath.relative_to(self.DATA_ROOT)}"
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
async def get_size(self, file: UploadFile):
|
@staticmethod
|
||||||
|
async def get_size(file: UploadFile):
|
||||||
f = file.file
|
f = file.file
|
||||||
f.seek(0, os.SEEK_END)
|
f.seek(0, os.SEEK_END)
|
||||||
size = f.tell()
|
size = f.tell()
|
||||||
f.seek(0, os.SEEK_SET)
|
f.seek(0, os.SEEK_SET)
|
||||||
return size
|
return size
|
||||||
|
|
||||||
def _save(self, filepath, file: BinaryIO):
|
@staticmethod
|
||||||
|
def _save(filepath, file: BinaryIO):
|
||||||
with open(filepath, 'wb') as f:
|
with open(filepath, 'wb') as f:
|
||||||
chunk_size = 256 * 1024
|
chunk_size = 256 * 1024
|
||||||
chunk = file.read(chunk_size)
|
chunk = file.read(chunk_size)
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import datetime
|
||||||
|
import random
|
||||||
|
import asyncio
|
||||||
|
from sqlalchemy import or_, select, delete
|
||||||
|
from sqlalchemy.ext.asyncio.session import AsyncSession
|
||||||
|
import settings
|
||||||
|
from database import Codes, engine
|
||||||
|
from storage import STORAGE_ENGINE
|
||||||
|
|
||||||
|
storage = STORAGE_ENGINE[settings.STORAGE_ENGINE]()
|
||||||
|
|
||||||
|
|
||||||
|
async def delete_expire_files():
|
||||||
|
while True:
|
||||||
|
async with AsyncSession(engine, expire_on_commit=False) as s:
|
||||||
|
query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0))
|
||||||
|
exps = (await s.execute(query)).scalars().all()
|
||||||
|
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)
|
||||||
|
query = delete(Codes).where(Codes.id.in_(exps_ids))
|
||||||
|
await s.execute(query)
|
||||||
|
await s.commit()
|
||||||
|
await asyncio.sleep(random.randint(60, 300))
|
||||||
|
|
||||||
|
|
||||||
|
async def get_code(s: AsyncSession):
|
||||||
|
code = random.randint(10000, 99999)
|
||||||
|
while (await s.execute(select(Codes.id).where(Codes.code == code))).scalar():
|
||||||
|
code = random.randint(10000, 99999)
|
||||||
|
return str(code)
|
||||||
Reference in New Issue
Block a user