Skip to content

Commit

Permalink
👀 preview docstring and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jiisanda committed Dec 10, 2023
1 parent dd6752f commit d689495
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
20 changes: 19 additions & 1 deletion api/routes/documents/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ async def get_document_preview(
metadata_repository: DocumentMetadataRepository = Depends(get_repository(DocumentMetadataRepository)),
user: TokenData = Depends(get_current_user),
) -> FileResponse:

"""
Get the preview of a document.
Args:
document (Union[str, UUID]): The ID or name of the document.
repository (DocumentRepository): The repository for accessing document data.
metadata_repository (DocumentMetadataRepository): The repository for accessing document metadata.
user (TokenData): The user token data.
Returns:
FileResponse: The file response containing the document preview.
Raises:
HTTP_404: If the document ID or name is not provided or if the document does not exist.
HTTP_400: If the file type is not supported for preview.
"""

if not document:
raise HTTP_404(
msg="Enter document id or name."
Expand All @@ -218,4 +236,4 @@ async def get_document_preview(
except ValueError as e:
raise HTTP_400(
msg="File type is not supported for preview"
)
) from e
27 changes: 27 additions & 0 deletions docs/features/preview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Preview in Docflow

Let's see how preview feature of DocFlow works. 🚀

- 🎯 Endpoint:
`GET /api/document/preview/:document`
- ⚙️ Params:
`{document: <docuent_id_or_name>}`
- 🔐 Authorization:
`Bearer <token>`

Here in Preview we use two important models, `fastapi.response`'s `FileResponse` amd `tempfile`'s `NamedTemporaryFile`.

`FileResponse` is used to return files.

`NamedTemporaryFile` is a function in Python's `tempfile` module that creates a temporary file with a unique name in the
system's default location for temporary files. This function returns a file-like object that can be used in similar way
to other file objects.

Here is the brief explanation on how it works:
- When we call `NameTemporaryFile()`, it creates a new file in you system's temporary directory.
- The temporary file is opened in binary mode (`wb+`) by default, and it can be read from and written to like any other
file object.
- The temporary file is deleted as soon as it is closed. This is controlled by the `delete` parameter, which is `True`
by default. This is important to set it `True`, as if not done then it could fill up the server's storage.

The following figure describes how Preview in DocFlow works.

0 comments on commit d689495

Please sign in to comment.