From 257cbf6685d99cc2f831fa34171405642c6116a7 Mon Sep 17 00:00:00 2001 From: lan-air Date: Sun, 11 Dec 2022 22:35:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=96=87=E6=A1=A3=EF=BC=8C?= =?UTF-8?q?=E4=BB=A5=E5=8F=8Adocker=E6=96=B0=E5=A2=9E=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E6=98=A0=E5=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database.py | 1 - main.py | 21 ++++++++------------- readme.md | 34 ++++++++++++++++++++++++++++++++-- settings.py | 7 +++---- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/database.py b/database.py index b27f9e1..fb06c42 100644 --- a/database.py +++ b/database.py @@ -1,4 +1,3 @@ -import asyncio import datetime from sqlalchemy import Boolean, Column, Integer, String, DateTime diff --git a/main.py b/main.py index 6c73777..3214062 100644 --- a/main.py +++ b/main.py @@ -30,9 +30,9 @@ app.mount(STATIC_URL, StaticFiles(directory=DATA_ROOT), name="static") @app.on_event('startup') async def startup(): await init_models() - asyncio.create_task(delete_expire_files()) + index_html = open('templates/index.html', 'r', encoding='utf-8').read() \ .replace('{{title}}', settings.TITLE) \ .replace('{{description}}', settings.DESCRIPTION) \ @@ -48,7 +48,7 @@ error_ip_count = {} def delete_file(files): for file in files: if file['type'] != 'text': - os.remove(DATA_ROOT / file['text'].lstrip(STATIC_URL+'/')) + os.remove(DATA_ROOT / file['text'].lstrip(STATIC_URL + '/')) async def delete_expire_files(): @@ -57,12 +57,10 @@ async def delete_expire_files(): query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0)) exps = (await s.execute(query)).scalars().all() await asyncio.to_thread(delete_file, [{'type': old.type, 'text': old.text} for old in exps]) - exps_ids = [exp.id for exp in exps] query = delete(Codes).where(Codes.id.in_(exps_ids)) await s.execute(query) await s.commit() - await asyncio.sleep(random.randint(60, 300)) @@ -73,12 +71,8 @@ async def get_code(s: AsyncSession): return str(code) -def get_file_name(key, ext, file): +def get_file_name(key, ext, file, file_bytes): now = datetime.datetime.now() - file_bytes = file.file.read() - size = len(file_bytes) - if size > settings.FILE_SIZE_LIMIT: - return size, '', '', '' path = DATA_ROOT / f"upload/{now.year}/{now.month}/{now.day}/" name = f'{key}.{ext}' if not path.exists(): @@ -86,7 +80,7 @@ def get_file_name(key, ext, file): filepath = path / name with open(filepath, 'wb') as f: f.write(file_bytes) - return size, f"{STATIC_URL}/{filepath.relative_to(DATA_ROOT)}", file.content_type, file.filename + return f"{STATIC_URL}/{filepath.relative_to(DATA_ROOT)}", file.content_type, file.filename @app.get(f'/{settings.ADMIN_ADDRESS}') @@ -148,7 +142,7 @@ async def get_file(code: str, s: AsyncSession = Depends(get_session)): if info.type == 'text': return {'code': code, 'msg': '查询成功', 'data': info.text} else: - return FileResponse(DATA_ROOT / info.text.lstrip(STATIC_URL+'/'), filename=info.name) + return FileResponse(DATA_ROOT / info.text.lstrip(STATIC_URL + '/'), filename=info.name) else: return {'code': 404, 'msg': '口令不存在'} @@ -173,7 +167,6 @@ async def index(request: Request, code: str, s: AsyncSession = Depends(get_sessi await s.commit() if info.type != 'text': info.text = f'/select?code={code}' - return { 'code': 200, 'msg': '取件成功,请点击"取"查看', @@ -200,9 +193,11 @@ async def share(text: str = Form(default=None), style: str = Form(default='2'), exp_count = -1 key = uuid.uuid4().hex if file: - size, _text, _type, name = get_file_name(key, file.filename.split('.')[-1], file) + file_bytes = file.file.read() + size = len(file_bytes) if size > settings.FILE_SIZE_LIMIT: return {'code': 404, 'msg': '文件过大'} + _text, _type, name = get_file_name(key, file.filename.split('.')[-1], file, file_bytes) else: size, _text, _type, name = len(text), text, 'text', '文本分享' info = Codes( diff --git a/readme.md b/readme.md index d34989e..c953e9d 100644 --- a/readme.md +++ b/readme.md @@ -65,7 +65,33 @@ https://www.yuque.com/lxyo/work/zd0kvzy7fofx6w7v ## 部署方式 -先拉取代码,然后修改main.py文件,将里面写了注释的可以根据需求改一下 +为持久化,不管怎么样,先第一步,建一个文件夹,然后再下载代码 + +```bash +mkdir /opt/FileCodeBox +cd /opt/FileCodeBox +``` + +新建一个`.env`文件 + +```bash +vi .env +``` +将下列字段内容替换成你自己的 + +```dotenv +DEBUG=False +DATABASE_URL=sqlite+aiosqlite:///database.db +DATA_ROOT=./static +STATIC_URL=/static +ERROR_COUNT=5 +ERROR_MINUTE=10 +ADMIN_ADDRESS=admin +FILE_SIZE_LIMIT=10 +TITLE=文件快递柜 +DESCRIPTION=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件 +KEYWORDS=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件 +``` ### 服务端部署 @@ -83,8 +109,12 @@ https://www.yuque.com/lxyo/work/zd0kvzy7fofx6w7v ### Docker部署 ```bash +mkdir "/opt/FileCodeBox" +cd "/opt/FileCodeBox" +wget https://github.com/vastsa/FileCodeBox/releases/download/Main/code.zip +unzip code.zip docker build --file Dockerfile --tag filecodebox . -docker run -d -p 12345:12345 --name filecodebox filecodebox +docker run -d -p 12345:12345 --name filecodebox --volume /opt/FileCodeBox:/app filecodebox ``` ## 状态 diff --git a/settings.py b/settings.py index ed9b9ad..682185d 100644 --- a/settings.py +++ b/settings.py @@ -1,6 +1,5 @@ from starlette.config import Config - config = Config(".env") DEBUG = config('DEBUG', cast=bool, default=False) @@ -19,10 +18,10 @@ ADMIN_ADDRESS = config('ADMIN_ADDRESS', cast=str, default="admin") ADMIN_PASSWORD = config('ADMIN_ADDRESS', cast=str, default="admin") -FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=1024 * 1024 * 10) +FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=10) * 1024 * 1024 TITLE = config('TITLE', cast=str, default="文件快递柜") -DESCRIPTION = config('DESCRIPTION', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件") +DESCRIPTION = config('DESCRIPTION', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件等文件") -KEYWORDS = config('TITLE', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件") +KEYWORDS = config('KEYWORDS', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件等文件")