From 315cedd0c43f96910fccfaebbf54c96b3a361b66 Mon Sep 17 00:00:00 2001 From: lan Date: Mon, 16 Jan 2023 18:50:46 +0800 Subject: [PATCH] =?UTF-8?q?add:=E5=90=8E=E5=8F=B0=E5=88=86=E4=BA=AB?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 12 ++++++++---- settings.py | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 137f9ab..bf21e65 100644 --- a/main.py +++ b/main.py @@ -18,9 +18,13 @@ from core.database import init_models, Options, Codes, get_session app = FastAPI(debug=settings.DEBUG, docs_url=None, redoc_url=None) # 数据存储文件夹 -DATA_ROOT = Path(settings.DATA_ROOT + '/files') +DATA_ROOT = Path(settings.DATA_ROOT) if not DATA_ROOT.exists(): DATA_ROOT.mkdir(parents=True) +# 本地文件文件夹 +LOCAL_ROOT = Path(settings.LOCAL_ROOT) +if not LOCAL_ROOT.exists(): + LOCAL_ROOT.mkdir(parents=True) # 静态文件夹,这个固定就行了,静态资源都放在这里 app.mount('/static', StaticFiles(directory='./static'), name="static") @@ -57,7 +61,7 @@ async def admin(): @app.get(f'/{settings.ADMIN_ADDRESS}/files', dependencies=[Depends(admin_required)]) async def get_files(): files = [] - for i, j, k in os.walk(f'{DATA_ROOT}'): + for i, j, k in os.walk(f'{LOCAL_ROOT}'): files.extend([{'name': _.split('/')[-1], 'path': i} for _ in k]) return {'data': files} @@ -68,12 +72,12 @@ async def share_file(name=Form(...), path=File(...), s: AsyncSession = Depends(g key = uuid.uuid4().hex code = await get_code(s) now = datetime.datetime.now() - path = f"{settings.DATA_ROOT}/upload/{now.year}/{now.month}/{now.day}/" + path = f"{settings.LOCAL_ROOT}/upload/{now.year}/{now.month}/{now.day}/" if not os.path.exists(path): os.makedirs(path) text = f"{path}/{f'{key}{name}'}" exp_time = datetime.datetime.now() + datetime.timedelta(days=1) - s.add(Codes(code=code, text=text.replace(f'{settings.DATA_ROOT}/', ''), type='file', name=name, count=-1, + s.add(Codes(code=code, text=text.replace(f'{settings.LOCAL_ROOT}/', ''), type='file', name=name, count=-1, exp_time=exp_time, key=key)) await s.commit() shutil.move(file_path, text) diff --git a/settings.py b/settings.py index adcf15d..fab7b00 100644 --- a/settings.py +++ b/settings.py @@ -19,6 +19,8 @@ class Settings: DATABASE_URL = config('DATABASE_URL', cast=str, default=f"sqlite+aiosqlite:///{DATABASE_FILE}") # 数据存储文件夹,文件就不暴露在静态资源里面了 DATA_ROOT = './data/' + config('DATA_ROOT', cast=str, default=f"static") + # 本地文件夹 + LOCAL_ROOT = './data/' + config('LOCAL_ROOT', cast=str, default=f"local") # 静态文件夹URL STATIC_URL = config('STATIC_URL', cast=str, default="/static") # 开启上传