Skip to content

Commit

Permalink
👀 preview pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
jiisanda committed Dec 10, 2023
1 parent 109f14c commit 0224f23
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
30 changes: 28 additions & 2 deletions api/routes/documents/document.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Dict, Optional, Union
from uuid import UUID

from fastapi import APIRouter, status, File, UploadFile, Depends

from schemas.documents.documents_metadata import DocumentMetadataRead
from fastapi.responses import FileResponse

from api.dependencies.auth_utils import get_current_user
from api.dependencies.repositories import get_repository
Expand All @@ -11,6 +11,8 @@
from db.repositories.documents.documents import DocumentRepository
from db.repositories.documents.documents_metadata import DocumentMetadataRepository
from schemas.auth.bands import TokenData
from schemas.documents.documents_metadata import DocumentMetadataRead


router = APIRouter(tags=["Document"])

Expand Down Expand Up @@ -189,3 +191,27 @@ async def perm_delete(
raise HTTP_404(
msg=f"No file with {file_name}"
) from e


@router.get(
"/preview/{document}",
status_code=status.HTTP_204_NO_CONTENT,
name="preview_document"
)
async def get_document_preview(
document: Union[str, UUID],
repository: DocumentRepository = Depends(DocumentRepository),
metadata_repository: DocumentMetadataRepository = Depends(get_repository(DocumentMetadataRepository)),
user: TokenData = Depends(get_current_user),
) -> FileResponse:
if not document:
raise HTTP_404(
msg="Enter document id or name."
)
try:
get_document_metadata = dict(await metadata_repository.get(document=document, owner=user))
return await repository.preview(document=get_document_metadata)
except Exception as e:
raise HTTP_404(
msg="Document does not exists."
) from e
15 changes: 15 additions & 0 deletions db/repositories/documents/documents.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import tempfile
from typing import Any, Dict, List
import boto3
import hashlib

from botocore.exceptions import ClientError
from fastapi import File
from fastapi.responses import FileResponse
from sqlalchemy.engine import Row
from ulid import ULID

Expand Down Expand Up @@ -189,3 +191,16 @@ async def perm_delete(
await self._delete_object(key=key)
await meta_repo.perm_delete(document=files.DocumentMetadata.id, owner=user, delete_all=False)
break

async def preview(self, document: Dict[str, Any]) -> FileResponse:

key = await get_key(s3_url=document["s3_url"])

s3_object = self.client.get_object(Bucket=settings.s3_bucket, Key=key)
file = s3_object['Body'].read()

with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp:
temp.write(file)
temp_path = temp.name

return FileResponse(temp_path, media_type='application/pdf')

0 comments on commit 0224f23

Please sign in to comment.