diff --git a/history.md b/history.md deleted file mode 100644 index 504fd03..0000000 --- a/history.md +++ /dev/null @@ -1,33 +0,0 @@ -## 更新记录 - -### 2022年12月13日 - -- [x] 完善手机版上传文本 -- [x] 优化Index.html首页 -- [x] 填补了由于上面修改带来的坑 -- [x] 更新了 - -### 2022年12月12日 - -- [x] 重写用户登录和 IP 检查并重定义状态码 -- [x] 新增存储引擎统一文件读写 -- [x] 设置PORT - -### 2022年12月11日 - -1. 修复取件不显示码的问题 -2. 修复文件次数为1时,文件被删除的问题 -3. 使用 aiosqlite 驱动异步化数据库操作 -4. 增加定时清理过期文件 -5. 优化部署方式,Docker映射,后续更新直接覆盖代码重启 -6. 优化配置文件,增加配置项 -7. 发布V1.4.5稳定版 - -### 2022年12月10日 - -1. 管理面板已新增,一如既往的极简,只有删除 -2. 二维码图片(调用的网络接口,如果离线环境将无法使用,一切为了极简) -3. 取件码有效期,取件码使用次数 -4. 优化代码逻辑 -5. 限制上传文件大小 -6. 完善配置参数 diff --git a/main.py b/main.py index 4e920f4..c14cd35 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import uuid import asyncio from pathlib import Path import os + try: import chardet from fastapi import FastAPI, Depends, UploadFile, Form, File, HTTPException, BackgroundTasks @@ -116,6 +117,25 @@ async def index(): return HTMLResponse(index_html) +@app.post('/') +async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession = Depends(get_session)): + query = select(Codes).where(Codes.code == code) + info = (await s.execute(query)).scalars().first() + if not info: + error_count = settings.ERROR_COUNT - error_ip_limit.add_ip(ip) + raise HTTPException(status_code=404, detail=f"取件码错误,{error_count}次后将被禁止{settings.ERROR_MINUTE}分钟") + if info.exp_time < datetime.datetime.now() or info.count == 0: + raise HTTPException(status_code=404, detail="取件码已失效,请联系寄件人") + await s.execute(update(Codes).where(Codes.id == info.id).values(count=info.count - 1)) + await s.commit() + if info.type != 'text': + info.text = f'/select?code={code}' + return { + 'detail': f'取件成功,文件将在{settings.DELETE_EXPIRE_FILES_INTERVAL}分钟后删除', + 'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code} + } + + @app.get('/banner') async def banner(request: Request, s: AsyncSession = Depends(get_session)): # 数据库查询config @@ -161,25 +181,6 @@ async def get_file(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession return FileResponse(filepath, filename=info.name) -@app.post('/') -async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession = Depends(get_session)): - query = select(Codes).where(Codes.code == code) - info = (await s.execute(query)).scalars().first() - if not info: - error_count = settings.ERROR_COUNT - error_ip_limit.add_ip(ip) - raise HTTPException(status_code=404, detail=f"取件码错误,{error_count}次后将被禁止{settings.ERROR_MINUTE}分钟") - if info.exp_time < datetime.datetime.now() or info.count == 0: - raise HTTPException(status_code=404, detail="取件码已失效,请联系寄件人") - await s.execute(update(Codes).where(Codes.id == info.id).values(count=info.count - 1)) - await s.commit() - if info.type != 'text': - info.text = f'/select?code={code}' - return { - 'detail': f'取件成功,文件将在{settings.DELETE_EXPIRE_FILES_INTERVAL}分钟后删除', - 'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code} - } - - @app.post('/share', dependencies=[Depends(admin_required)], description='分享文件') 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), diff --git a/readme.md b/readme.md index af6e06d..878a549 100644 --- a/readme.md +++ b/readme.md @@ -52,15 +52,9 @@ docker run -d --restart=always -p 12345:12345 -v /opt/FileCodeBox/:/app/data --n ## 项目规划 2022年12月14日 - 这个项目的灵感来源于丁丁快传,然后写了这么一个基于本机存储的快传系统,本系统主要是以轻量,单用户,离线环境(`私有化` )为主,因此也不需要加太多东西,所以其实这个项目到这基本功能已经完成了,剩下的就是维护和完善现有功能。 - -也不会再加入新的大功能了,如果有新的功能的话,那就是我们的Pro版本了,当然也是继续开源的,能和@veoco一起开源挺荣幸的,在他的代码中我学到了许多,此前我基本上是使用Django那一套,对Fastapi仅限于使用,他的许多写法让我受益匪浅,也让我对Fastapi有了更深的了解,所以我也会在Pro版本中使用Fastapi。 - -根据目前一些使用反馈来说,希望加入登录功能,还有多存储引擎等,欢迎各位继续提意见,加入我们共同开发。 - -如果你有更好的想法和建议欢迎提issue。 +也不会再加入新的大功能了,如果你有更好的想法和建议欢迎提issue。 ## 预览 @@ -166,6 +160,68 @@ KEYWORDS=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文 STORAGE_ENGINE=filesystem ``` +## 接口文档 + +前端比较简陋,可以使用接口进行二次开发 + +### 取件 + +#### PATH + +`/` + +#### METHOD + +`POST` + +#### PARAMS + +code: 取件码 + +#### Response + +```json +{ + "detail": "msg", + "data": { + "type": "类型", + "text": "文本", + "name": "名称", + "code": "取件码" + } +} +``` + +### 寄件 + +#### PATH + +`/share` + +#### METHOD + +`POST` + +#### PARAMS + +style: 1为次数,2为时间 +value: 次数或时间 +text: 取件码 +file: 文件 + +#### Response + +```json +{ + "detail": "msg", + "data": { + "code": "类型", + "key": "唯一ID", + "name": "名称" + } +} +``` + ## 状态 ![Alt](https://repobeats.axiom.co/api/embed/7a6c92f1d96ee57e6fb67f0df371528397b0c9ac.svg "Repobeats analytics image") diff --git a/settings.py b/settings.py index 7f1739a..91445c1 100644 --- a/settings.py +++ b/settings.py @@ -1,7 +1,7 @@ from starlette.config import Config import os import shutil -import encoding_convert +# import encoding_convert # 配置文件.env # 请将.env移动至data目录,方便docker部署 @@ -9,9 +9,9 @@ import encoding_convert if os.path.exists('.env'): # 将文件复制到data目录 shutil.copy('.env', 'data/.env') -if os.path.exists('data/.env'): - # 获取data目录下的.env文件编码,如果为utf-8编码,就转换为gbk编码 - encoding_convert.convert_encoding('./data/.env') +# if os.path.exists('data/.env'): +# # 获取data目录下的.env文件编码,如果为utf-8编码,就转换为gbk编码 +# encoding_convert.convert_encoding('./data/.env') config = Config("data/.env") # 是否开启DEBUG模式