feat: add admin text preview endpoint

This commit is contained in:
Lan
2026-06-03 04:05:52 +08:00
parent 71fc1ad8ea
commit 1eb69e4cc5
2 changed files with 38 additions and 0 deletions
+28
View File
@@ -190,6 +190,34 @@ class FileService:
else: else:
return await self.file_storage.get_file_response(file_code) return await self.file_storage.get_file_response(file_code)
async def preview_file(self, file_id: int, max_chars: int = 4000):
max_chars = min(max(max_chars, 1), 20000)
file_code = await FileCodes.filter(id=file_id).first()
if not file_code:
raise HTTPException(status_code=404, detail="文件不存在")
if file_code.text is None:
raise HTTPException(status_code=400, detail="仅文本分享支持预览")
content = file_code.text
preview = content[:max_chars]
return {
"id": file_code.id,
"code": file_code.code,
"name": f"{file_code.prefix}{file_code.suffix}",
"type": "text",
"content": preview,
"length": len(content),
"previewLength": len(preview),
"preview_length": len(preview),
"truncated": len(content) > max_chars,
"maxChars": max_chars,
"max_chars": max_chars,
"createdAt": file_code.created_at,
"created_at": file_code.created_at,
"expiredAt": file_code.expired_at,
"expired_at": file_code.expired_at,
}
async def share_local_file(self, item): async def share_local_file(self, item):
local_file = LocalFileClass(item.filename) local_file = LocalFileClass(item.filename)
if not await local_file.exists(): if not await local_file.exists():
+10
View File
@@ -196,6 +196,16 @@ async def file_download(
return file_content return file_content
@admin_api.get("/file/preview")
async def file_preview(
id: int,
maxChars: int = 4000,
file_service: FileService = Depends(get_file_service),
):
preview = await file_service.preview_file(id, maxChars)
return APIResponse(detail=preview)
@admin_api.get("/local/lists") @admin_api.get("/local/lists")
async def get_local_lists( async def get_local_lists(
local_file_service: LocalFileService = Depends(get_local_file_service), local_file_service: LocalFileService = Depends(get_local_file_service),