mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-11-10 14:02:20 -05:00
29 lines
821 B
TypeScript
29 lines
821 B
TypeScript
import { alert } from "~/composables/use-toast";
|
|
import { useGlobalI18n } from "~/composables/use-global-i18n";
|
|
|
|
export function useDownloader() {
|
|
function download(url: string, filename: string) {
|
|
useFetch(url, {
|
|
method: "GET",
|
|
responseType: "blob",
|
|
onResponse({ response }) {
|
|
if (!response.ok) {
|
|
console.error("Download failed", response);
|
|
const i18n = useGlobalI18n();
|
|
alert.error(i18n.t("events.something-went-wrong"));
|
|
return;
|
|
}
|
|
|
|
const url = window.URL.createObjectURL(new Blob([response._data]));
|
|
const link = document.createElement("a");
|
|
link.href = url;
|
|
link.setAttribute("download", filename);
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
},
|
|
});
|
|
}
|
|
|
|
return download;
|
|
}
|