update:自动删除过期文件
This commit is contained in:
@@ -12,7 +12,6 @@ from core.response import APIResponse
|
||||
|
||||
class IPRateLimit:
|
||||
def __init__(self, count, minutes):
|
||||
print(count, minutes)
|
||||
self.ips = {}
|
||||
self.count = count
|
||||
self.minutes = minutes
|
||||
|
||||
+2
-2
@@ -28,10 +28,10 @@ class FileCodes(Model):
|
||||
created_at: Optional[datetime] = fields.DatetimeField(auto_now_add=True, description='创建时间')
|
||||
|
||||
async def is_expired(self):
|
||||
if self.expired_at and (self.expired_count == -1 or self.used_count < self.expired_count):
|
||||
if self.expired_at and (self.expired_count == -1 or self.expired_count > 0):
|
||||
return self.expired_at < await get_now()
|
||||
else:
|
||||
return self.expired_count != -1 and self.used_count >= self.expired_count
|
||||
return self.expired_count != -1 and self.expired_count == 0
|
||||
|
||||
async def get_file_path(self):
|
||||
return f"{self.file_path}/{self.uuid_file_name}"
|
||||
|
||||
@@ -71,6 +71,7 @@ async def select_file(data: SelectFileModel, ip: str = Depends(error_ip_limit)):
|
||||
if await file_code.is_expired():
|
||||
return APIResponse(code=403, detail='文件已过期')
|
||||
file_code.used_count += 1
|
||||
file_code.expired_count -= 1
|
||||
await file_code.save()
|
||||
return APIResponse(detail={
|
||||
'code': file_code.code,
|
||||
|
||||
@@ -69,6 +69,20 @@ class S3FileStorage:
|
||||
return result
|
||||
|
||||
|
||||
class FileStorageTemplate:
|
||||
def __init__(self):
|
||||
...
|
||||
|
||||
async def save_file(self, file: UploadFile, save_path: str):
|
||||
...
|
||||
|
||||
async def delete_file(self, file_code: FileCodes):
|
||||
...
|
||||
|
||||
async def get_file_url(self, file_code: FileCodes):
|
||||
...
|
||||
|
||||
|
||||
storages = {
|
||||
'local': SystemFileStorage,
|
||||
's3': S3FileStorage
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
# @Time : 2023/8/15 22:00
|
||||
# @Author : Lan
|
||||
# @File : tasks.py
|
||||
# @Software: PyCharm
|
||||
import asyncio
|
||||
|
||||
from tortoise.expressions import Q
|
||||
|
||||
from apps.base.models import FileCodes
|
||||
from apps.base.utils import error_ip_limit, upload_ip_limit
|
||||
from core.storage import file_storage
|
||||
from core.utils import get_now
|
||||
|
||||
|
||||
async def delete_expire_files():
|
||||
while True:
|
||||
try:
|
||||
await error_ip_limit.remove_expired_ip()
|
||||
await upload_ip_limit.remove_expired_ip()
|
||||
expire_data = await FileCodes.filter(Q(expired_at__lt=await get_now()) | Q(expired_count=0)).all()
|
||||
for exp in expire_data:
|
||||
await file_storage.delete_file(exp)
|
||||
await exp.delete()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
finally:
|
||||
await asyncio.sleep(600)
|
||||
@@ -2,6 +2,7 @@
|
||||
# @Author : Lan
|
||||
# @File : main.py
|
||||
# @Software: PyCharm
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from fastapi import FastAPI
|
||||
@@ -13,6 +14,7 @@ from tortoise.contrib.fastapi import register_tortoise
|
||||
from apps.base.views import share_api
|
||||
from apps.admin.views import admin_api
|
||||
from core.settings import data_root, settings
|
||||
from core.tasks import delete_expire_files
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@@ -49,6 +51,12 @@ app.include_router(share_api)
|
||||
app.include_router(admin_api)
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
# 启动后台任务,不定时删除过期文件
|
||||
asyncio.create_task(delete_expire_files())
|
||||
|
||||
|
||||
@app.get('/')
|
||||
async def index():
|
||||
return HTMLResponse(
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
- [x] 匿名分享:无需注册,无需登录
|
||||
- [x] 管理面板:查看所有文件,删除文件
|
||||
- [x] 一键部署:docker一键部署
|
||||
- [x] 自由拓展:阿里云OSS、本地文件流,可根据需求在storage文件中新增存储引擎
|
||||
- [x] 自由拓展:S3协议、本地文件流,可根据需求在storage文件中新增存储引擎
|
||||
- [x] 简单明了:适合新手练手项目
|
||||
|
||||
## 部署方式
|
||||
@@ -165,7 +165,7 @@ docker logs filecodebox
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
## 配置文件
|
||||
## 配置文件(1.7及以下版本才需要)
|
||||
|
||||
如果需要修改配置,可以将该文件放在`/opt/FileCodeBox/`目录下,并命名为`.env`,然后重启容器即可。
|
||||
如果不是Docker,则需要在项目同目录下新建一个`data`文件夹,然后在创建`.env`文件
|
||||
|
||||
Reference in New Issue
Block a user