From c9ec40803d2bddb4077bc0a1a573f6c9f019a656 Mon Sep 17 00:00:00 2001 From: lan Date: Tue, 13 Dec 2022 17:50:22 +0800 Subject: [PATCH] update:adjust file structure --- main.py | 50 +++++++------------------------------------------- storage.py | 9 +++++---- utils.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 47 deletions(-) create mode 100644 utils.py diff --git a/main.py b/main.py index 5ba5525..0b692b5 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,5 @@ import datetime import uuid -import random import asyncio 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.staticfiles import StaticFiles -from sqlalchemy import or_, select, update, delete +from sqlalchemy import select, update from sqlalchemy.ext.asyncio.session import AsyncSession import settings -from database import get_session, Codes, init_models, engine -from storage import STORAGE_ENGINE +from utils import delete_expire_files, storage, get_code +from database import get_session, Codes, init_models from depends import admin_required, IPRateLimit app = FastAPI(debug=settings.DEBUG) @@ -25,8 +24,6 @@ if not DATA_ROOT.exists(): STATIC_URL = settings.STATIC_URL app.mount(STATIC_URL, StaticFiles(directory=DATA_ROOT), name="static") -storage = STORAGE_ENGINE[settings.STORAGE_ENGINE]() - @app.on_event('startup') async def startup(): @@ -46,31 +43,6 @@ admin_html = open('templates/admin.html', 'r', encoding='utf-8').read() \ 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}') async def admin(): return HTMLResponse(admin_html) @@ -138,7 +110,8 @@ async def index(code: str, ip: str = Depends(ip_limit), s: AsyncSession = Depend @app.post('/share') 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) if style == '2': 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) else: size, _text, _type, name = len(text), text, 'text', '文本分享' - info = Codes( - code=code, - text=_text, - size=size, - type=_type, - name=name, - count=exp_count, - exp_time=exp_time, - key=key - ) + info = Codes(code=code, text=_text, size=size, type=_type, name=name, count=exp_count, exp_time=exp_time, key=key) s.add(info) await s.commit() return { - 'detail': '分享成功,请点击文件箱查看取件码', + 'detail': '分享成功,请点击取件码按钮查看上传列表', 'data': {'code': code, 'key': key, 'name': name, 'text': _text} } diff --git a/storage.py b/storage.py index b992a9a..0059aed 100644 --- a/storage.py +++ b/storage.py @@ -23,18 +23,19 @@ class FileSystemStorage: path = self.DATA_ROOT / f"upload/{now.year}/{now.month}/{now.day}/" if not path.exists(): path.mkdir(parents=True) - filepath = path / f'{key}.{ext}' - text = f"{self.STATIC_URL}/{filepath.relative_to(self.DATA_ROOT)}" + text = f"{self.STATIC_URL}/{(path / f'{key}.{ext}').relative_to(self.DATA_ROOT).relative_to(self.DATA_ROOT)}" return text - async def get_size(self, file: UploadFile): + @staticmethod + async def get_size(file: UploadFile): f = file.file f.seek(0, os.SEEK_END) size = f.tell() f.seek(0, os.SEEK_SET) return size - def _save(self, filepath, file: BinaryIO): + @staticmethod + def _save(filepath, file: BinaryIO): with open(filepath, 'wb') as f: chunk_size = 256 * 1024 chunk = file.read(chunk_size) diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..0b297f3 --- /dev/null +++ b/utils.py @@ -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)