(self, file_id: int, max_chars: int = 4000)
| 1436 | return await self.file_storage.get_file_response(file_code) |
| 1437 | |
| 1438 | async def preview_file(self, file_id: int, max_chars: int = 4000): |
| 1439 | max_chars = min(max(max_chars, 1), 20000) |
| 1440 | file_code = await FileCodes.filter(id=file_id).first() |
| 1441 | if not file_code: |
| 1442 | raise HTTPException(status_code=404, detail="文件不存在") |
| 1443 | if file_code.text is None: |
| 1444 | raise HTTPException(status_code=400, detail="仅文本分享支持预览") |
| 1445 | |
| 1446 | content = file_code.text |
| 1447 | preview = content[:max_chars] |
| 1448 | return { |
| 1449 | "id": file_code.id, |
| 1450 | "code": file_code.code, |
| 1451 | "name": f"{file_code.prefix}{file_code.suffix}", |
| 1452 | "type": "text", |
| 1453 | "content": preview, |
| 1454 | "length": len(content), |
| 1455 | "previewLength": len(preview), |
| 1456 | "preview_length": len(preview), |
| 1457 | "truncated": len(content) > max_chars, |
| 1458 | "maxChars": max_chars, |
| 1459 | "max_chars": max_chars, |
| 1460 | "createdAt": file_code.created_at, |
| 1461 | "created_at": file_code.created_at, |
| 1462 | "expiredAt": file_code.expired_at, |
| 1463 | "expired_at": file_code.expired_at, |
| 1464 | } |
| 1465 | |
| 1466 | async def share_local_file(self, item): |
| 1467 | local_file = LocalFileClass(item.filename) |
no test coverage detected