Skip to content

Commit

Permalink
update:自动删除过期文件
Browse files Browse the repository at this point in the history
  • Loading branch information
vastsa committed Aug 15, 2023
1 parent 824d6e7 commit d91d64c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
1 change: 0 additions & 1 deletion apps/base/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

class IPRateLimit:
def __init__(self, count, minutes):
print(count, minutes)
self.ips = {}
self.count = count
self.minutes = minutes
Expand Down
4 changes: 2 additions & 2 deletions apps/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
1 change: 1 addition & 0 deletions apps/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions core/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ async def get_file_url(self, file_code: FileCodes):
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
Expand Down
27 changes: 27 additions & 0 deletions core/tasks.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# @Author : Lan
# @File : main.py
# @Software: PyCharm
import asyncio
import os

from fastapi import FastAPI
Expand All @@ -13,6 +14,7 @@
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()

Expand Down Expand Up @@ -49,6 +51,12 @@
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(
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
- [x] 匿名分享:无需注册,无需登录
- [x] 管理面板:查看所有文件,删除文件
- [x] 一键部署:docker一键部署
- [x] 自由拓展:阿里云OSS、本地文件流,可根据需求在storage文件中新增存储引擎
- [x] 自由拓展:S3协议、本地文件流,可根据需求在storage文件中新增存储引擎
- [x] 简单明了:适合新手练手项目

## 部署方式
Expand Down Expand Up @@ -165,7 +165,7 @@ docker logs filecodebox
</tr>
</table>

## 配置文件
## 配置文件(1.7及以下版本才需要)

如果需要修改配置,可以将该文件放在`/opt/FileCodeBox/`目录下,并命名为`.env`,然后重启容器即可。
如果不是Docker,则需要在项目同目录下新建一个`data`文件夹,然后在创建`.env`文件
Expand Down

0 comments on commit d91d64c

Please sign in to comment.