Files
FileCodeBox/apps/base/views.py
T

154 lines
5.0 KiB
Python
Raw Permalink Normal View History

2023-08-15 20:40:25 +08:00
from fastapi import APIRouter, Form, UploadFile, File, Depends, HTTPException
2024-10-06 17:14:16 +08:00
from apps.admin.dependencies import admin_required
2023-08-14 05:03:07 +08:00
from apps.base.models import FileCodes
2024-10-06 17:14:16 +08:00
from apps.base.schemas import SelectFileModel
2024-07-28 19:03:37 +08:00
from apps.base.utils import get_expire_info, get_file_path_name, ip_limit
2023-08-14 13:30:10 +08:00
from core.response import APIResponse
2023-08-15 20:40:25 +08:00
from core.settings import settings
from core.storage import storages, FileStorageInterface
2023-08-24 14:04:00 +08:00
from core.utils import get_select_token
2023-08-14 05:03:07 +08:00
2025-02-15 22:00:53 +08:00
share_api = APIRouter(prefix="/share", tags=["分享"])
2024-10-06 17:14:16 +08:00
async def validate_file_size(file: UploadFile, max_size: int):
if file.size > max_size:
max_size_mb = max_size / (1024 * 1024)
2025-02-15 22:00:53 +08:00
raise HTTPException(
status_code=403, detail=f"大小超过限制,最大为{max_size_mb:.2f} MB"
)
2024-10-06 17:14:16 +08:00
async def create_file_code(code, **kwargs):
return await FileCodes.create(code=code, **kwargs)
2023-08-14 05:03:07 +08:00
2025-02-15 22:00:53 +08:00
@share_api.post("/text/", dependencies=[Depends(admin_required)])
2024-10-06 17:14:16 +08:00
async def share_text(
2025-02-15 22:00:53 +08:00
text: str = Form(...),
expire_value: int = Form(default=1, gt=0),
expire_style: str = Form(default="day"),
ip: str = Depends(ip_limit["upload"]),
2024-10-06 17:14:16 +08:00
):
2025-02-15 22:00:53 +08:00
text_size = len(text.encode("utf-8"))
2024-10-06 17:14:16 +08:00
max_txt_size = 222 * 1024
if text_size > max_txt_size:
2025-02-15 22:00:53 +08:00
raise HTTPException(status_code=403, detail="内容过多,建议采用文件形式")
2024-10-06 17:14:16 +08:00
2025-02-15 22:00:53 +08:00
expired_at, expired_count, used_count, code = await get_expire_info(
expire_value, expire_style
)
2024-10-06 17:14:16 +08:00
await create_file_code(
2023-08-14 05:03:07 +08:00
code=code,
text=text,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
size=len(text),
2025-02-15 22:00:53 +08:00
prefix="Text",
2023-08-14 05:03:07 +08:00
)
2025-02-15 22:00:53 +08:00
ip_limit["upload"].add_ip(ip)
return APIResponse(detail={"code": code})
2023-08-14 05:03:07 +08:00
2025-02-15 22:00:53 +08:00
@share_api.post("/file/", dependencies=[Depends(admin_required)])
2024-10-06 17:14:16 +08:00
async def share_file(
2025-02-15 22:00:53 +08:00
expire_value: int = Form(default=1, gt=0),
expire_style: str = Form(default="day"),
file: UploadFile = File(...),
ip: str = Depends(ip_limit["upload"]),
2024-10-06 17:14:16 +08:00
):
await validate_file_size(file, settings.uploadSize)
if expire_style not in settings.expireStyle:
2025-02-15 22:00:53 +08:00
raise HTTPException(status_code=400, detail="过期时间类型错误")
2024-10-06 17:14:16 +08:00
2025-02-15 22:00:53 +08:00
expired_at, expired_count, used_count, code = await get_expire_info(
expire_value, expire_style
)
2023-08-14 05:03:07 +08:00
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(file)
2024-10-06 17:14:16 +08:00
file_storage: FileStorageInterface = storages[settings.file_storage]()
2023-08-14 05:03:07 +08:00
await file_storage.save_file(file, save_path)
2024-10-06 17:14:16 +08:00
await create_file_code(
2023-08-14 05:03:07 +08:00
code=code,
prefix=prefix,
suffix=suffix,
uuid_file_name=uuid_file_name,
file_path=path,
size=file.size,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
)
2025-02-15 22:00:53 +08:00
ip_limit["upload"].add_ip(ip)
return APIResponse(detail={"code": code, "name": file.filename})
2023-08-14 05:03:07 +08:00
2024-01-11 17:51:05 +08:00
async def get_code_file_by_code(code, check=True):
file_code = await FileCodes.filter(code=code).first()
if not file_code:
2025-02-15 22:00:53 +08:00
return False, "文件不存在"
2024-01-11 17:51:05 +08:00
if await file_code.is_expired() and check:
2025-02-15 22:00:53 +08:00
return False, "文件已过期"
return True, file_code
2024-10-06 17:14:16 +08:00
async def update_file_usage(file_code):
file_code.used_count += 1
if file_code.expired_count > 0:
file_code.expired_count -= 1
await file_code.save()
2025-02-15 22:00:53 +08:00
@share_api.get("/select/")
async def get_code_file(code: str, ip: str = Depends(ip_limit["error"])):
file_storage: FileStorageInterface = storages[settings.file_storage]()
has, file_code = await get_code_file_by_code(code)
if not has:
2025-02-15 22:00:53 +08:00
ip_limit["error"].add_ip(ip)
return APIResponse(code=404, detail=file_code)
2024-10-06 17:14:16 +08:00
await update_file_usage(file_code)
return await file_storage.get_file_response(file_code)
2025-02-15 22:00:53 +08:00
@share_api.post("/select/")
async def select_file(data: SelectFileModel, ip: str = Depends(ip_limit["error"])):
file_storage: FileStorageInterface = storages[settings.file_storage]()
has, file_code = await get_code_file_by_code(data.code)
if not has:
2025-02-15 22:00:53 +08:00
ip_limit["error"].add_ip(ip)
return APIResponse(code=404, detail=file_code)
2024-10-06 17:14:16 +08:00
await update_file_usage(file_code)
2025-02-15 22:00:53 +08:00
return APIResponse(
detail={
"code": file_code.code,
"name": file_code.prefix + file_code.suffix,
"size": file_code.size,
"text": (
file_code.text
if file_code.text is not None
else await file_storage.get_file_url(file_code)
),
}
)
2023-08-14 14:22:10 +08:00
2025-02-15 22:00:53 +08:00
@share_api.get("/download")
async def download_file(key: str, code: str, ip: str = Depends(ip_limit["error"])):
file_storage: FileStorageInterface = storages[settings.file_storage]()
2024-10-06 17:14:16 +08:00
if await get_select_token(code) != key:
2025-02-15 22:00:53 +08:00
ip_limit["error"].add_ip(ip)
2024-10-06 17:14:16 +08:00
2024-01-11 17:51:05 +08:00
has, file_code = await get_code_file_by_code(code, False)
if not has:
2025-02-15 22:00:53 +08:00
return APIResponse(code=404, detail="文件不存在")
2024-10-06 17:14:16 +08:00
2025-02-09 20:25:23 +08:00
return (
APIResponse(detail=file_code.text)
if file_code.text
else await file_storage.get_file_response(file_code)
)