Files
FileCodeBox/apps/base/views.py
T

159 lines
5.5 KiB
Python
Raw Permalink Normal View History

2023-08-14 05:03:07 +08:00
# @Time : 2023/8/14 03:59
# @Author : Lan
# @File : views.py
# @Software: PyCharm
2023-12-06 08:37:33 +08:00
# 导入所需的库和模块
2023-08-15 20:40:25 +08:00
from fastapi import APIRouter, Form, UploadFile, File, Depends, HTTPException
from apps.admin.depends import admin_required
2023-08-14 05:03:07 +08:00
from apps.base.models import FileCodes
2023-08-14 13:30:10 +08:00
from apps.base.pydantics import SelectFileModel
2023-08-15 21:33:58 +08:00
from apps.base.utils import get_expire_info, get_file_path_name, error_ip_limit, upload_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
2023-08-14 05:03:07 +08:00
from core.storage import file_storage
2023-08-24 14:04:00 +08:00
from core.utils import get_select_token
2023-08-14 05:03:07 +08:00
2023-12-06 08:37:33 +08:00
# 创建一个API路由
2023-08-14 05:03:07 +08:00
share_api = APIRouter(
2023-12-06 08:37:33 +08:00
prefix='/share', # 路由前缀
tags=['分享'], # 标签
2023-08-14 05:03:07 +08:00
)
2023-12-06 08:37:33 +08:00
# 分享文本的API
2023-08-15 20:40:25 +08:00
@share_api.post('/text/', dependencies=[Depends(admin_required)])
2023-08-15 21:33:58 +08:00
async def share_text(text: str = Form(...), expire_value: int = Form(default=1, gt=0), expire_style: str = Form(default='day'), ip: str = Depends(upload_ip_limit)):
2023-12-06 08:37:33 +08:00
# 获取过期信息
2023-08-14 05:03:07 +08:00
expired_at, expired_count, used_count, code = await get_expire_info(expire_value, expire_style)
2023-12-06 08:37:33 +08:00
# 创建一个新的FileCodes实例
2023-08-14 05:03:07 +08:00
await FileCodes.create(
code=code,
text=text,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
size=len(text),
prefix='文本分享'
)
2023-12-06 08:37:33 +08:00
# 添加IP到限制列表
2023-08-15 21:33:58 +08:00
upload_ip_limit.add_ip(ip)
2023-12-06 08:37:33 +08:00
# 返回API响应
2023-08-14 13:30:10 +08:00
return APIResponse(detail={
'code': code,
})
2023-08-14 05:03:07 +08:00
2023-12-06 08:37:33 +08:00
# 分享文件的API
2023-08-15 20:40:25 +08:00
@share_api.post('/file/', dependencies=[Depends(admin_required)])
2023-08-15 21:33:58 +08:00
async def share_file(expire_value: int = Form(default=1, gt=0), expire_style: str = Form(default='day'), file: UploadFile = File(...), ip: str = Depends(upload_ip_limit)):
2023-12-06 08:37:33 +08:00
# 检查文件大小是否超过限制
2023-08-15 20:40:25 +08:00
if file.size > settings.uploadSize:
raise HTTPException(status_code=403, detail=f'文件大小超过限制,最大为{settings.uploadSize}字节')
2023-12-06 08:37:33 +08:00
# 获取过期信息
if expire_style not in settings.expireStyle:
raise HTTPException(status_code=400, detail='过期时间类型错误')
2023-08-14 05:03:07 +08:00
expired_at, expired_count, used_count, code = await get_expire_info(expire_value, expire_style)
2023-12-06 08:37:33 +08:00
# 获取文件路径和名称
2023-08-14 05:03:07 +08:00
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(file)
2023-12-06 08:37:33 +08:00
# 保存文件
2023-08-14 05:03:07 +08:00
await file_storage.save_file(file, save_path)
2023-12-06 08:37:33 +08:00
# 创建一个新的FileCodes实例
2023-08-14 05:03:07 +08:00
await FileCodes.create(
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,
)
2023-12-06 08:37:33 +08:00
# 添加IP到限制列表
2023-08-15 21:33:58 +08:00
upload_ip_limit.add_ip(ip)
2023-12-06 08:37:33 +08:00
# 返回API响应
2023-08-14 13:30:10 +08:00
return APIResponse(detail={
'code': code,
'name': file.filename,
})
2023-08-14 05:03:07 +08:00
2023-12-06 08:37:33 +08:00
# 根据code获取文件
2024-01-11 17:51:05 +08:00
async def get_code_file_by_code(code, check=True):
2023-12-06 08:37:33 +08:00
# 查询文件
file_code = await FileCodes.filter(code=code).first()
2023-12-06 08:37:33 +08:00
# 检查文件是否存在
if not file_code:
return False, '文件不存在'
2023-12-06 08:37:33 +08:00
# 检查文件是否过期
2024-01-11 17:51:05 +08:00
if await file_code.is_expired() and check:
return False, '文件已过期',
return True, file_code
2023-12-06 08:37:33 +08:00
# 获取文件的API
@share_api.get('/select/')
async def get_code_file(code: str, ip: str = Depends(error_ip_limit)):
2023-12-06 08:37:33 +08:00
# 获取文件
has, file_code = await get_code_file_by_code(code)
2023-12-06 08:37:33 +08:00
# 检查文件是否存在
if not has:
2023-12-06 08:37:33 +08:00
# 添加IP到限制列表
error_ip_limit.add_ip(ip)
2023-12-06 08:37:33 +08:00
# 返回API响应
return APIResponse(code=404, detail=file_code)
2023-12-06 08:37:33 +08:00
# 更新文件的使用次数和过期次数
file_code.used_count += 1
if file_code.expired_count > 0:
file_code.expired_count -= 1
2023-12-06 08:37:33 +08:00
# 保存文件
await file_code.save()
2023-12-06 08:37:33 +08:00
# 返回文件响应
return await file_storage.get_file_response(file_code)
2023-12-06 08:37:33 +08:00
# 选择文件的API
2023-08-14 05:03:07 +08:00
@share_api.post('/select/')
2023-08-14 13:30:10 +08:00
async def select_file(data: SelectFileModel, ip: str = Depends(error_ip_limit)):
2023-12-06 08:37:33 +08:00
# 获取文件
has, file_code = await get_code_file_by_code(data.code)
2023-12-06 08:37:33 +08:00
# 检查文件是否存在
if not has:
2023-12-06 08:37:33 +08:00
# 添加IP到限制列表
2023-08-14 13:30:10 +08:00
error_ip_limit.add_ip(ip)
2023-12-06 08:37:33 +08:00
# 返回API响应
return APIResponse(code=404, detail=file_code)
2023-12-06 08:37:33 +08:00
# 更新文件的使用次数和过期次数
2023-08-14 13:30:10 +08:00
file_code.used_count += 1
if file_code.expired_count > 0:
file_code.expired_count -= 1
2023-12-06 08:37:33 +08:00
# 保存文件
2023-08-14 13:30:10 +08:00
await file_code.save()
2023-12-06 08:37:33 +08:00
# 返回API响应
2023-08-14 13:30:10 +08:00
return APIResponse(detail={
'code': file_code.code,
'name': file_code.prefix + file_code.suffix,
'size': file_code.size,
2023-08-14 13:45:58 +08:00
'text': file_code.text if file_code.text is not None else await file_storage.get_file_url(file_code),
2023-08-14 13:30:10 +08:00
})
2023-08-14 14:22:10 +08:00
2023-12-06 08:37:33 +08:00
# 下载文件的API
2023-08-14 14:22:10 +08:00
@share_api.get('/download')
async def download_file(key: str, code: str, ip: str = Depends(error_ip_limit)):
2023-12-06 08:37:33 +08:00
# 检查token是否有效
2023-08-24 14:04:00 +08:00
is_valid = await get_select_token(code) == key
2023-08-14 14:22:10 +08:00
if not is_valid:
2023-12-06 08:37:33 +08:00
# 添加IP到限制列表
2023-08-14 14:22:10 +08:00
error_ip_limit.add_ip(ip)
2023-12-06 08:37:33 +08:00
# 获取文件
2024-01-11 17:51:05 +08:00
has, file_code = await get_code_file_by_code(code, False)
2023-12-06 08:37:33 +08:00
# 检查文件是否存在
2024-01-11 17:51:05 +08:00
if not has:
2023-12-06 08:37:33 +08:00
# 返回API响应
2023-08-14 14:22:10 +08:00
return APIResponse(code=404, detail='文件不存在')
2023-12-06 08:37:33 +08:00
# 如果文件是文本,返回文本内容,否则返回文件响应
2023-08-14 14:22:10 +08:00
if file_code.text:
return APIResponse(detail=file_code.text)
else:
2023-08-24 14:04:00 +08:00
return await file_storage.get_file_response(file_code)