Use backend endpoint for attachment downloads
This commit is contained in:
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user