Archived
feat: pdf reader
This commit is contained in:
@@ -168,18 +168,23 @@ class EBookViewSet(viewsets.ModelViewSet):
|
||||
|
||||
@action(detail=True, methods=["get"])
|
||||
def file(self, request: Request, pk: int | None = None) -> FileResponse | Response:
|
||||
"""Stream the EPUB file for authenticated in-browser reading."""
|
||||
"""Stream the ebook file for authenticated in-browser reading."""
|
||||
ebook = self.get_object()
|
||||
if ebook.format != "epub":
|
||||
content_types = {
|
||||
"epub": "application/epub+zip",
|
||||
"pdf": "application/pdf",
|
||||
}
|
||||
content_type = content_types.get(ebook.format)
|
||||
if not content_type:
|
||||
return Response(
|
||||
{"error": "Reader supports EPUB only."},
|
||||
{"error": "Unsupported format for in-browser reading."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
if not ebook.file:
|
||||
return Response({"error": "No file found for this e-book."}, status=status.HTTP_400_BAD_REQUEST)
|
||||
return FileResponse(
|
||||
ebook.file.open("rb"),
|
||||
content_type="application/epub+zip",
|
||||
content_type=content_type,
|
||||
filename=ebook.filename(),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user