Spaces:
Sleeping
Sleeping
File size: 3,717 Bytes
43a06dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import { get } from "svelte/store";
import settings from "$lib/state/settings";
import { device } from "$lib/device";
import { t } from "$lib/i18n/translations";
import { createDialog } from "$lib/state/dialogs";
import type { DialogInfo } from "$lib/types/dialog";
import type { CobaltFileUrlType } from "$lib/types/api";
type DownloadFileParams = {
url?: string,
file?: File,
urlType?: CobaltFileUrlType,
}
type SavingDialogParams = {
url?: string,
file?: File,
body?: string,
urlType?: CobaltFileUrlType,
}
const openSavingDialog = ({ url, file, body, urlType }: SavingDialogParams) => {
const dialogData: DialogInfo = {
type: "saving",
id: "saving",
file,
url,
urlType,
}
if (body) dialogData.bodyText = body;
createDialog(dialogData)
}
export const openFile = (file: File) => {
const a = document.createElement("a");
const url = URL.createObjectURL(file);
a.href = url;
a.download = file.name;
a.click();
URL.revokeObjectURL(url);
}
export const shareFile = async (file: File) => {
return await navigator?.share({
files: [
new File([file], file.name, {
type: file.type,
}),
],
});
}
export const openURL = (url: string) => {
if (!['http:', 'https:'].includes(new URL(url).protocol)) {
return alert('error: invalid url!');
}
const open = window.open(url, "_blank");
/* if new tab got blocked by user agent, show a saving dialog */
if (!open) {
return openSavingDialog({
url,
body: get(t)("dialog.saving.blocked")
});
}
}
export const shareURL = async (url: string) => {
return await navigator?.share({ url });
}
export const copyURL = async (url: string) => {
return await navigator?.clipboard?.writeText(url);
}
export const downloadFile = ({ url, file, urlType }: DownloadFileParams) => {
if (!url && !file) throw new Error("attempted to download void");
const pref = get(settings).save.savingMethod;
if (pref === "ask") {
return openSavingDialog({ url, file, urlType });
}
/*
user actions (such as invoke share, open new tab) have expiration.
in webkit, for example, that timeout is 5 seconds.
https://github.com/WebKit/WebKit/blob/b838f8bb/Source/WebCore/page/LocalDOMWindow.cpp#L167
navigator.userActivation.isActive makes sure that we're still able to
invoke an action without the user agent interrupting it.
if not, we show a saving dialog for user to re-invoke that action.
if browser is old or doesn't support this API, we just assume that it expired.
*/
if (!navigator?.userActivation?.isActive) {
return openSavingDialog({
url,
file,
body: get(t)("dialog.saving.timeout"),
urlType
});
}
try {
if (file) {
if (pref === "share" && device.supports.share) {
return shareFile(file);
} else if (pref === "download" && device.supports.directDownload) {
return openFile(file);
}
}
if (url) {
if (pref === "share" && device.supports.share) {
return shareURL(url);
} else if (pref === "download" && device.supports.directDownload
&& !(device.is.iOS && urlType === "redirect")) {
return openURL(url);
} else if (pref === "copy" && !file) {
return copyURL(url);
}
}
} catch { /* catch & ignore */ }
return openSavingDialog({ url, file, urlType });
}
|