feat: add file serving endpoint GET /api/documents/{id}/file
Returns original document file from NAS. Fixes 404 on PDF/image viewer in frontend. Updated frontend iframe/img src to match. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ from pathlib import Path
|
|||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile, status
|
from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile, status
|
||||||
|
from fastapi.responses import FileResponse
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from sqlalchemy import func, select
|
from sqlalchemy import func, select
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
@@ -160,6 +161,28 @@ async def get_document(
|
|||||||
return DocumentResponse.model_validate(doc)
|
return DocumentResponse.model_validate(doc)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{doc_id}/file")
|
||||||
|
async def get_document_file(
|
||||||
|
doc_id: int,
|
||||||
|
user: Annotated[User, Depends(get_current_user)],
|
||||||
|
session: Annotated[AsyncSession, Depends(get_session)],
|
||||||
|
):
|
||||||
|
"""문서 원본 파일 서빙"""
|
||||||
|
doc = await session.get(Document, doc_id)
|
||||||
|
if not doc:
|
||||||
|
raise HTTPException(status_code=404, detail="문서를 찾을 수 없습니다")
|
||||||
|
|
||||||
|
file_path = Path(settings.nas_mount_path) / doc.file_path
|
||||||
|
if not file_path.exists():
|
||||||
|
raise HTTPException(status_code=404, detail="파일을 찾을 수 없습니다")
|
||||||
|
|
||||||
|
return FileResponse(
|
||||||
|
path=str(file_path),
|
||||||
|
filename=file_path.name,
|
||||||
|
media_type=None, # 자동 감지
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/", response_model=DocumentResponse, status_code=201)
|
@router.post("/", response_model=DocumentResponse, status_code=201)
|
||||||
async def upload_document(
|
async def upload_document(
|
||||||
file: UploadFile,
|
file: UploadFile,
|
||||||
|
|||||||
@@ -56,12 +56,12 @@
|
|||||||
</div>
|
</div>
|
||||||
{:else if viewerType === 'pdf'}
|
{:else if viewerType === 'pdf'}
|
||||||
<iframe
|
<iframe
|
||||||
src="/documents/file/{doc.id}"
|
src="/api/documents/{doc.id}/file"
|
||||||
class="w-full h-[80vh] rounded"
|
class="w-full h-[80vh] rounded"
|
||||||
title={doc.title}
|
title={doc.title}
|
||||||
></iframe>
|
></iframe>
|
||||||
{:else if viewerType === 'image'}
|
{:else if viewerType === 'image'}
|
||||||
<img src="/documents/file/{doc.id}" alt={doc.title} class="max-w-full rounded" />
|
<img src="/api/documents/{doc.id}/file" alt={doc.title} class="max-w-full rounded" />
|
||||||
{:else if viewerType === 'synology'}
|
{:else if viewerType === 'synology'}
|
||||||
<div class="text-center py-10">
|
<div class="text-center py-10">
|
||||||
<p class="text-[var(--text-dim)] mb-4">Synology Office 문서</p>
|
<p class="text-[var(--text-dim)] mb-4">Synology Office 문서</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user