Use backend endpoint for attachment downloads

This commit is contained in:
TerryM
2026-05-27 12:17:47 +08:00
parent 68cbce9cf1
commit 9453777dba
5 changed files with 42 additions and 23 deletions

View File

@@ -1,3 +1,5 @@
import { assetUrl } from "../../../api";
type SaveFilePicker = (options?: {
suggestedName?: string;
types?: Array<{
@@ -20,6 +22,22 @@ type WindowWithSavePicker = Window & {
showSaveFilePicker?: SaveFilePicker;
};
export function attachmentDownloadUrl(postId: string, attachmentId: string) {
return assetUrl(
`/api/posts/${encodeURIComponent(postId)}/attachments/${encodeURIComponent(
attachmentId,
)}/download`,
);
}
export async function downloadAttachment(
postId: string,
attachmentId: string,
filename: string,
) {
return downloadFile(attachmentDownloadUrl(postId, attachmentId), filename);
}
export async function downloadFile(url: string, filename: string) {
const res = await fetch(url, { credentials: "include" });
if (!res.ok) throw new Error(await res.text());