Skip to content

Commit

Permalink
Merge pull request #33 from bookracy/bugfix/improve-file-extension-su…
Browse files Browse the repository at this point in the history
…pport

improve file extension support
  • Loading branch information
JorrinKievit authored Sep 13, 2024
2 parents 65697a3 + 46e13a4 commit 43759de
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 35 deletions.
13 changes: 3 additions & 10 deletions src/api/backend/downloads/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useMutation, useQuery } from "@tanstack/react-query";
import { client } from "../base";
import { ExternalDownloadResponse } from "./types";
import { ofetch } from "ofetch";
import { getFileExtensionByContentType } from "@/lib/extension";

export const getExternalDownloads = (md5s: string[]) => {
if (md5s.length === 0) return Promise.resolve([]);
Expand All @@ -23,22 +22,16 @@ export const useExternalDownloadsQuery = (md5s: string[]) => {
export const useDownloadMutation = () => {
return useMutation({
mutationKey: ["download"],
mutationFn: async (link: string) => {
mutationFn: async (link: string): Promise<string> => {
if (link.includes("ipfs")) {
return {
url: link,
extension: "pdf",
};
return link;
}

const response = await ofetch(link, {
responseType: "blob",
});

return {
url: URL.createObjectURL(response),
extension: getFileExtensionByContentType(response.type),
};
return URL.createObjectURL(response);
},
});
};
4 changes: 2 additions & 2 deletions src/components/books/book-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function BookItemCard(props: BookItemProps) {
<p className="text-sm dark:text-gray-400">MD5: {props.md5}</p>
</div>
<div className="mt-4 flex flex-wrap gap-5">
{"externalDownloads" in props && <BookDownloadButton title={props.title} externalDownloads={props.externalDownloads} primaryLink={props.link} />}
{"externalDownloads" in props && <BookDownloadButton title={props.title} extension={props.book_filetype} externalDownloads={props.externalDownloads} primaryLink={props.link} />}
{isEpub && <EpubReader title={props.title} link={props.link} open={isReaderOpen} setIsOpen={setIsReaderOpen} />}
</div>
</div>
Expand Down Expand Up @@ -130,7 +130,7 @@ export function BookItemDialog(props: BookItemProps) {
</div>
</ScrollArea>
<DialogFooter className="flex flex-row justify-between md:justify-end">
{"externalDownloads" in props && <BookDownloadButton title={props.title} externalDownloads={props.externalDownloads} primaryLink={props.link} />}
{"externalDownloads" in props && <BookDownloadButton title={props.title} extension={props.book_filetype} externalDownloads={props.externalDownloads} primaryLink={props.link} />}

{isEpub && <EpubReader title={props.title} link={props.link} open={isReaderOpen} setIsOpen={setIsReaderOpen} />}
</DialogFooter>
Expand Down
3 changes: 2 additions & 1 deletion src/components/books/download-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { titleToSlug } from "@/lib/string";

interface BookDownloadButtonProps {
title: string;
extension: string;
primaryLink?: string;
externalDownloads?: ExternalDownloadResponse[number]["external_downloads"];
}
Expand All @@ -21,7 +22,7 @@ export function BookDownloadButton(props: BookDownloadButtonProps) {
const handleDownload = (link?: string) => {
if (!link) return;
mutate(link, {
onSuccess: ({ url, extension }) => saveAs(url, `${titleToSlug(props.title)}${extension}`, link.includes("ipfs")),
onSuccess: (url) => saveAs(url, `${titleToSlug(props.title)}${props.extension}`, link.includes("ipfs")),

onError: () => toast.error("Failed to download file"),
});
Expand Down
22 changes: 0 additions & 22 deletions src/lib/extension.ts

This file was deleted.

0 comments on commit 43759de

Please sign in to comment.