Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions src/components/AssetDownload/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import type { ImageProps, StaticImageData } from "next/image"

import AssetDownloadArtist from "@/components/AssetDownload/AssetDownloadArtist"
import AssetDownloadImage from "@/components/AssetDownload/AssetDownloadImage"
import { Button } from "@/components/ui/buttons/Button"
import { Flex, Stack } from "@/components/ui/flex"

import { cn } from "@/lib/utils/cn"
import { trackCustomEvent } from "@/lib/utils/matomo"

import { ButtonLink } from "../ui/buttons/Button"
import { Flex, Stack } from "../ui/flex"

import { useTranslation } from "@/hooks/useTranslation"

type AssetDownloadProps = {
Expand Down Expand Up @@ -41,6 +40,40 @@ const AssetDownload = ({
eventName: title,
})
}

const handleDownload = async (url: string, fileExtension: string) => {
if (!url) return

matomoHandler()

try {
const response = await fetch(url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that these are same-origin files, we can use the download attr in the links instead of doing this complex workaround, we don't need to fetch the file.

<ButtonLink href={imgSrc} onClick={matomoHandler} download={`${title.replace(/\s+/g, "-").toLowerCase()}.${fileExtension}`}>

const blob = await response.blob()
const blobUrl = window.URL.createObjectURL(blob)
const link = document.createElement("a")
link.href = blobUrl
link.download = `${title.replace(/\s+/g, "-").toLowerCase()}.${fileExtension}`
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
window.URL.revokeObjectURL(blobUrl)
} catch (error) {
console.error("Failed to download file:", error)
}
}

const handleSvgDownload = () => {
if (!svgUrl) return
handleDownload(svgUrl, "svg")
}

const handleImageDownload = () => {
const imgSrc = (image as StaticImageData).src
if (!imgSrc) return
const fileExt = extname(imgSrc).slice(1)
handleDownload(imgSrc, fileExt)
}

const imgSrc = (image as StaticImageData).src

return (
Expand All @@ -56,14 +89,14 @@ const AssetDownload = ({
)}
</div>
<Flex className="mt-4 gap-5">
<ButtonLink href={imgSrc} onClick={matomoHandler} target="_blank">
<Button onClick={handleImageDownload}>
{t("page-assets-download-download")} (
{extname(imgSrc).slice(1).toUpperCase()})
</ButtonLink>
</Button>
{svgUrl && (
<ButtonLink href={svgUrl} onClick={matomoHandler} target="_blank">
<Button onClick={handleSvgDownload}>
{t("page-assets-download-download")} (SVG)
</ButtonLink>
</Button>
)}
</Flex>
</Stack>
Expand Down