You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iTi-Flask/iti/applications/routes/sys/file.py

125 lines
3.3 KiB
Python

from __future__ import annotations
from apiflask import APIBlueprint
from flask import request
from flask_jwt_extended import jwt_required, get_jwt_identity
from iti.applications.common.utils import success
from iti.applications.models import SysFileSchema
from iti.applications.service.sys.sys_file import SysFileService
bp = APIBlueprint("sys_file", __name__, url_prefix="/file", tag="系统.文件管理")
# ---------------------------------------------------------------------------
# 文件信息查询
# ---------------------------------------------------------------------------
@bp.get("/<string:file_id>")
@jwt_required(optional=True)
@bp.output(SysFileSchema)
def get_file(file_id: str):
"""获取文件详情"""
file_obj = SysFileService.get_file_by_id(file_id)
return success(file_obj)
# ---------------------------------------------------------------------------
# 回收站功能
# ---------------------------------------------------------------------------
@bp.delete("/<string:file_id>")
@jwt_required()
def delete_file(file_id: str):
"""
删除文件(移动到回收站)
如果回收站功能启用,文件将被移动到回收站;否则直接物理删除
"""
user_id = get_jwt_identity()
SysFileService.move_to_recycle(file_id, user_id)
return success(message="文件已删除")
@bp.post("/<string:file_id>/restore")
@jwt_required()
def restore_file(file_id: str):
"""从回收站恢复文件"""
SysFileService.restore_from_recycle(file_id)
return success(message="文件已恢复")
@bp.delete("/<string:file_id>/permanent")
@jwt_required()
def delete_file_permanent(file_id: str):
"""永久删除文件(物理删除)"""
SysFileService.delete_file_permanently(file_id)
return success(message="文件已永久删除")
@bp.post("/recycle/clear")
@jwt_required()
def clear_recycle_bin():
"""
清空回收站
删除30天前的回收站文件
"""
count = SysFileService.clear_recycle_bin(days=30)
return success(message=f"已清理 {count} 个文件")
# ---------------------------------------------------------------------------
# 分享管理功能
# ---------------------------------------------------------------------------
@bp.post("/<string:file_id>/share")
@jwt_required()
def create_share(file_id: str):
"""
创建文件分享
请求示例:
```json
{
"password": "1234", // 可选
"expireHours": 24 // 可选,不传表示永久
}
```
响应示例:
```json
{
"success": true,
"data": {
"shareCode": "abc123",
"shareUrl": "http://example.com/share/abc123",
"password": "1234",
"expireAt": "2024-01-01 12:00:00"
}
}
```
"""
data = request.get_json() or {}
password = data.get("password")
expire_hours = data.get("expireHours") or data.get("expire_hours")
result = SysFileService.create_share(
file_id=file_id,
password=password,
expire_hours=expire_hours,
)
return success(result)
@bp.delete("/<string:file_id>/share")
@jwt_required()
def cancel_share(file_id: str):
"""取消文件分享"""
SysFileService.cancel_share(file_id)
return success(message="分享已取消")