2026-04-13 09:43:24 +08:00
|
|
|
# api/v1/history_router.py
|
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
|
import urllib.parse
|
|
|
|
|
|
2026-09-17 16:15:49 +08:00
|
|
|
from moldinsight.services.file_history_service import FileHistoryService
|
2026-05-29 18:10:08 +08:00
|
|
|
from shared.database.database import get_db_session
|
2026-08-31 18:01:34 +08:00
|
|
|
from shared.services.auth_service import get_current_active_user
|
2026-09-17 16:15:49 +08:00
|
|
|
from shared.models.identity import User
|
2026-08-31 18:01:34 +08:00
|
|
|
from shared.utils.logger import get_logger
|
2026-04-13 09:43:24 +08:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
2026-08-31 18:01:34 +08:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
2026-04-13 09:43:24 +08:00
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/history")
|
|
|
|
|
@router.post("/history")
|
2026-08-31 18:01:34 +08:00
|
|
|
async def get_file_history(
|
|
|
|
|
db_session: AsyncSession = Depends(get_db_session),
|
|
|
|
|
current_user: User = Depends(get_current_active_user),
|
|
|
|
|
):
|
|
|
|
|
"""获取当前用户按文件名分组的文件历史记录(支持多上传)"""
|
2026-09-17 16:15:49 +08:00
|
|
|
storage_service = FileHistoryService()
|
2026-08-31 18:01:34 +08:00
|
|
|
file_groups = await storage_service.get_all_file_groups(
|
|
|
|
|
db_session, user_id=current_user.id
|
|
|
|
|
)
|
2026-04-13 09:43:24 +08:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"total_files": len(file_groups),
|
|
|
|
|
"files": file_groups
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/history/{filename}")
|
|
|
|
|
@router.post("/history/{filename}")
|
2026-08-31 18:01:34 +08:00
|
|
|
async def get_file_records(
|
|
|
|
|
filename: str,
|
|
|
|
|
db_session: AsyncSession = Depends(get_db_session),
|
|
|
|
|
current_user: User = Depends(get_current_active_user),
|
|
|
|
|
):
|
|
|
|
|
"""获取当前用户指定文件名的所有上传记录(支持多上传历史)"""
|
2026-04-13 09:43:24 +08:00
|
|
|
decoded_filename = urllib.parse.unquote(filename)
|
|
|
|
|
|
2026-09-17 16:15:49 +08:00
|
|
|
storage_service = FileHistoryService()
|
2026-04-13 09:43:24 +08:00
|
|
|
file_records = await storage_service.get_file_history_by_filename(
|
|
|
|
|
db_session,
|
2026-08-31 18:01:34 +08:00
|
|
|
decoded_filename,
|
|
|
|
|
user_id=current_user.id,
|
2026-04-13 09:43:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return file_records
|