@zag-js/file-utils 1.34.1 → 1.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-transfer.d.mts +3 -0
- package/dist/data-transfer.d.ts +3 -0
- package/dist/data-transfer.js +83 -0
- package/dist/data-transfer.mjs +58 -0
- package/dist/data-url-to-blob.d.mts +3 -0
- package/dist/data-url-to-blob.d.ts +3 -0
- package/dist/data-url-to-blob.js +39 -0
- package/dist/data-url-to-blob.mjs +14 -0
- package/dist/download-file.d.mts +33 -0
- package/dist/download-file.d.ts +33 -0
- package/dist/download-file.js +81 -0
- package/dist/download-file.mjs +56 -0
- package/dist/get-accept-attr.d.mts +3 -0
- package/dist/get-accept-attr.d.ts +3 -0
- package/dist/get-accept-attr.js +46 -0
- package/dist/get-accept-attr.mjs +21 -0
- package/dist/get-file-data-url.d.mts +3 -0
- package/dist/get-file-data-url.d.ts +3 -0
- package/dist/get-file-data-url.js +47 -0
- package/dist/get-file-data-url.mjs +22 -0
- package/dist/get-file-mime-type.d.mts +5 -0
- package/dist/get-file-mime-type.d.ts +5 -0
- package/dist/get-file-mime-type.js +34 -0
- package/dist/get-file-mime-type.mjs +9 -0
- package/dist/get-total-file-size.d.mts +3 -0
- package/dist/get-total-file-size.d.ts +3 -0
- package/dist/get-total-file-size.js +32 -0
- package/dist/get-total-file-size.mjs +7 -0
- package/dist/index.d.mts +11 -62
- package/dist/index.d.ts +11 -62
- package/dist/index.js +40 -238
- package/dist/index.mjs +11 -229
- package/dist/is-file-equal.d.mts +3 -0
- package/dist/is-file-equal.d.ts +3 -0
- package/dist/is-file-equal.js +32 -0
- package/dist/is-file-equal.mjs +7 -0
- package/dist/is-valid-file-size.d.mts +5 -0
- package/dist/is-valid-file-size.d.ts +5 -0
- package/dist/is-valid-file-size.js +43 -0
- package/dist/is-valid-file-size.mjs +18 -0
- package/dist/is-valid-file-type.d.mts +5 -0
- package/dist/is-valid-file-type.d.ts +5 -0
- package/dist/is-valid-file-type.js +54 -0
- package/dist/is-valid-file-type.mjs +29 -0
- package/dist/mime-types.d.mts +4 -0
- package/dist/mime-types.d.ts +4 -0
- package/dist/mime-types.js +38 -0
- package/dist/mime-types.mjs +12 -0
- package/dist/types.d.mts +12 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +0 -0
- package/package.json +2 -2
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/data-transfer.ts
|
|
21
|
+
var data_transfer_exports = {};
|
|
22
|
+
__export(data_transfer_exports, {
|
|
23
|
+
getFileEntries: () => getFileEntries
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(data_transfer_exports);
|
|
26
|
+
var getItemEntry = (item) => typeof item.getAsEntry === "function" ? item.getAsEntry() : typeof item.webkitGetAsEntry === "function" ? item.webkitGetAsEntry() : null;
|
|
27
|
+
var isDirectoryEntry = (entry) => entry.isDirectory;
|
|
28
|
+
var isFileEntry = (entry) => entry.isFile;
|
|
29
|
+
var addRelativePath = (file, path) => {
|
|
30
|
+
Object.defineProperty(file, "relativePath", { value: path ? `${path}/${file.name}` : file.name });
|
|
31
|
+
return file;
|
|
32
|
+
};
|
|
33
|
+
var getFileEntries = (items, traverseDirectories) => Promise.all(
|
|
34
|
+
Array.from(items).filter((item) => item.kind === "file").map((item) => {
|
|
35
|
+
const entry = getItemEntry(item);
|
|
36
|
+
if (!entry) return null;
|
|
37
|
+
if (isDirectoryEntry(entry) && traverseDirectories) {
|
|
38
|
+
return getDirectoryFiles(entry.createReader(), `${entry.name}`);
|
|
39
|
+
}
|
|
40
|
+
if (isFileEntry(entry) && typeof item.getAsFile === "function") {
|
|
41
|
+
const file = item.getAsFile();
|
|
42
|
+
return Promise.resolve(file ? addRelativePath(file, "") : null);
|
|
43
|
+
}
|
|
44
|
+
if (isFileEntry(entry)) {
|
|
45
|
+
return new Promise((resolve) => {
|
|
46
|
+
entry.file((file) => {
|
|
47
|
+
resolve(addRelativePath(file, ""));
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}).filter((b) => b)
|
|
52
|
+
);
|
|
53
|
+
var getDirectoryFiles = (reader, path = "") => new Promise((resolve) => {
|
|
54
|
+
const entryPromises = [];
|
|
55
|
+
const readDirectoryEntries = () => {
|
|
56
|
+
reader.readEntries((entries) => {
|
|
57
|
+
if (entries.length === 0) {
|
|
58
|
+
resolve(Promise.all(entryPromises).then((entries2) => entries2.flat()));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const promises = entries.map((entry) => {
|
|
62
|
+
if (!entry) return null;
|
|
63
|
+
if (isDirectoryEntry(entry)) {
|
|
64
|
+
return getDirectoryFiles(entry.createReader(), `${path}${entry.name}`);
|
|
65
|
+
}
|
|
66
|
+
if (isFileEntry(entry)) {
|
|
67
|
+
return new Promise((resolve2) => {
|
|
68
|
+
entry.file((file) => {
|
|
69
|
+
resolve2(addRelativePath(file, path));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}).filter((b) => b);
|
|
74
|
+
entryPromises.push(Promise.all(promises));
|
|
75
|
+
readDirectoryEntries();
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
readDirectoryEntries();
|
|
79
|
+
});
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
getFileEntries
|
|
83
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/data-transfer.ts
|
|
2
|
+
var getItemEntry = (item) => typeof item.getAsEntry === "function" ? item.getAsEntry() : typeof item.webkitGetAsEntry === "function" ? item.webkitGetAsEntry() : null;
|
|
3
|
+
var isDirectoryEntry = (entry) => entry.isDirectory;
|
|
4
|
+
var isFileEntry = (entry) => entry.isFile;
|
|
5
|
+
var addRelativePath = (file, path) => {
|
|
6
|
+
Object.defineProperty(file, "relativePath", { value: path ? `${path}/${file.name}` : file.name });
|
|
7
|
+
return file;
|
|
8
|
+
};
|
|
9
|
+
var getFileEntries = (items, traverseDirectories) => Promise.all(
|
|
10
|
+
Array.from(items).filter((item) => item.kind === "file").map((item) => {
|
|
11
|
+
const entry = getItemEntry(item);
|
|
12
|
+
if (!entry) return null;
|
|
13
|
+
if (isDirectoryEntry(entry) && traverseDirectories) {
|
|
14
|
+
return getDirectoryFiles(entry.createReader(), `${entry.name}`);
|
|
15
|
+
}
|
|
16
|
+
if (isFileEntry(entry) && typeof item.getAsFile === "function") {
|
|
17
|
+
const file = item.getAsFile();
|
|
18
|
+
return Promise.resolve(file ? addRelativePath(file, "") : null);
|
|
19
|
+
}
|
|
20
|
+
if (isFileEntry(entry)) {
|
|
21
|
+
return new Promise((resolve) => {
|
|
22
|
+
entry.file((file) => {
|
|
23
|
+
resolve(addRelativePath(file, ""));
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}).filter((b) => b)
|
|
28
|
+
);
|
|
29
|
+
var getDirectoryFiles = (reader, path = "") => new Promise((resolve) => {
|
|
30
|
+
const entryPromises = [];
|
|
31
|
+
const readDirectoryEntries = () => {
|
|
32
|
+
reader.readEntries((entries) => {
|
|
33
|
+
if (entries.length === 0) {
|
|
34
|
+
resolve(Promise.all(entryPromises).then((entries2) => entries2.flat()));
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const promises = entries.map((entry) => {
|
|
38
|
+
if (!entry) return null;
|
|
39
|
+
if (isDirectoryEntry(entry)) {
|
|
40
|
+
return getDirectoryFiles(entry.createReader(), `${path}${entry.name}`);
|
|
41
|
+
}
|
|
42
|
+
if (isFileEntry(entry)) {
|
|
43
|
+
return new Promise((resolve2) => {
|
|
44
|
+
entry.file((file) => {
|
|
45
|
+
resolve2(addRelativePath(file, path));
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}).filter((b) => b);
|
|
50
|
+
entryPromises.push(Promise.all(promises));
|
|
51
|
+
readDirectoryEntries();
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
readDirectoryEntries();
|
|
55
|
+
});
|
|
56
|
+
export {
|
|
57
|
+
getFileEntries
|
|
58
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/data-url-to-blob.ts
|
|
21
|
+
var data_url_to_blob_exports = {};
|
|
22
|
+
__export(data_url_to_blob_exports, {
|
|
23
|
+
dataURItoBlob: () => dataURItoBlob
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(data_url_to_blob_exports);
|
|
26
|
+
function dataURItoBlob(uri) {
|
|
27
|
+
const binary = atob(uri.split(",")[1]);
|
|
28
|
+
const mimeString = uri.split(",")[0].split(":")[1].split(";")[0];
|
|
29
|
+
const buffer = new ArrayBuffer(binary.length);
|
|
30
|
+
const intArray = new Uint8Array(buffer);
|
|
31
|
+
for (let i = 0; i < binary.length; i++) {
|
|
32
|
+
intArray[i] = binary.charCodeAt(i);
|
|
33
|
+
}
|
|
34
|
+
return new Blob([buffer], { type: mimeString });
|
|
35
|
+
}
|
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
37
|
+
0 && (module.exports = {
|
|
38
|
+
dataURItoBlob
|
|
39
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/data-url-to-blob.ts
|
|
2
|
+
function dataURItoBlob(uri) {
|
|
3
|
+
const binary = atob(uri.split(",")[1]);
|
|
4
|
+
const mimeString = uri.split(",")[0].split(":")[1].split(";")[0];
|
|
5
|
+
const buffer = new ArrayBuffer(binary.length);
|
|
6
|
+
const intArray = new Uint8Array(buffer);
|
|
7
|
+
for (let i = 0; i < binary.length; i++) {
|
|
8
|
+
intArray[i] = binary.charCodeAt(i);
|
|
9
|
+
}
|
|
10
|
+
return new Blob([buffer], { type: mimeString });
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
dataURItoBlob
|
|
14
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
interface DownloadFileOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The name of the file
|
|
4
|
+
*/
|
|
5
|
+
name?: string | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* The MIME type of the file
|
|
8
|
+
*/
|
|
9
|
+
type: string;
|
|
10
|
+
/**
|
|
11
|
+
* The file contents
|
|
12
|
+
*/
|
|
13
|
+
file: File | Blob | string;
|
|
14
|
+
/**
|
|
15
|
+
* The window environment
|
|
16
|
+
*/
|
|
17
|
+
win?: typeof window | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to add a BOM (Byte Order Mark) to the file.
|
|
20
|
+
* Useful for CSV files.
|
|
21
|
+
*/
|
|
22
|
+
appendBOM?: boolean | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* The timeout in milliseconds to revoke the object URL.
|
|
25
|
+
* This is a safeguard for when the browser has not finished reading the
|
|
26
|
+
* file data from memory before the URL is revoked.
|
|
27
|
+
* @default 0
|
|
28
|
+
*/
|
|
29
|
+
revokeTimeout?: number | undefined;
|
|
30
|
+
}
|
|
31
|
+
declare function downloadFile(options: DownloadFileOptions): void;
|
|
32
|
+
|
|
33
|
+
export { type DownloadFileOptions, downloadFile };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
interface DownloadFileOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The name of the file
|
|
4
|
+
*/
|
|
5
|
+
name?: string | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* The MIME type of the file
|
|
8
|
+
*/
|
|
9
|
+
type: string;
|
|
10
|
+
/**
|
|
11
|
+
* The file contents
|
|
12
|
+
*/
|
|
13
|
+
file: File | Blob | string;
|
|
14
|
+
/**
|
|
15
|
+
* The window environment
|
|
16
|
+
*/
|
|
17
|
+
win?: typeof window | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to add a BOM (Byte Order Mark) to the file.
|
|
20
|
+
* Useful for CSV files.
|
|
21
|
+
*/
|
|
22
|
+
appendBOM?: boolean | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* The timeout in milliseconds to revoke the object URL.
|
|
25
|
+
* This is a safeguard for when the browser has not finished reading the
|
|
26
|
+
* file data from memory before the URL is revoked.
|
|
27
|
+
* @default 0
|
|
28
|
+
*/
|
|
29
|
+
revokeTimeout?: number | undefined;
|
|
30
|
+
}
|
|
31
|
+
declare function downloadFile(options: DownloadFileOptions): void;
|
|
32
|
+
|
|
33
|
+
export { type DownloadFileOptions, downloadFile };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/download-file.ts
|
|
21
|
+
var download_file_exports = {};
|
|
22
|
+
__export(download_file_exports, {
|
|
23
|
+
downloadFile: () => downloadFile
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(download_file_exports);
|
|
26
|
+
var BOM_REGEX = /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i;
|
|
27
|
+
var MAC_REGEX = /Macintosh/;
|
|
28
|
+
var APPLE_WEBKIT_REGEX = /AppleWebKit/;
|
|
29
|
+
var SAFARI_REGEX = /Safari/;
|
|
30
|
+
function getBlob(blobOrString, type, appendBOM) {
|
|
31
|
+
let blob = typeof blobOrString === "string" ? new Blob([blobOrString], { type }) : blobOrString;
|
|
32
|
+
if (appendBOM && BOM_REGEX.test(blob.type)) {
|
|
33
|
+
return new Blob([String.fromCharCode(65279), blob], { type: blob.type });
|
|
34
|
+
}
|
|
35
|
+
return blob;
|
|
36
|
+
}
|
|
37
|
+
function isMSEdge(win) {
|
|
38
|
+
return Boolean(win.navigator && win.navigator.msSaveOrOpenBlob);
|
|
39
|
+
}
|
|
40
|
+
function isWebView(win) {
|
|
41
|
+
return win.navigator && MAC_REGEX.test(win.navigator.userAgent) && APPLE_WEBKIT_REGEX.test(win.navigator.userAgent) && !SAFARI_REGEX.test(win.navigator.userAgent);
|
|
42
|
+
}
|
|
43
|
+
function downloadFile(options) {
|
|
44
|
+
const { file, win = window, type, name, appendBOM, revokeTimeout = 0 } = options;
|
|
45
|
+
const doc = win.document;
|
|
46
|
+
const blob = getBlob(file, type, appendBOM);
|
|
47
|
+
const fileName = (file instanceof File ? name || file.name : name) || "file-download";
|
|
48
|
+
if (isMSEdge(win)) {
|
|
49
|
+
win.navigator.msSaveOrOpenBlob(blob, fileName);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const isMacOSWebView = isWebView(win);
|
|
53
|
+
const anchor = doc.createElement("a");
|
|
54
|
+
const canUseDownload = "download" in anchor && !isMacOSWebView;
|
|
55
|
+
if (canUseDownload) {
|
|
56
|
+
const url2 = win.URL.createObjectURL(blob);
|
|
57
|
+
anchor.href = url2;
|
|
58
|
+
anchor.rel = "noopener";
|
|
59
|
+
anchor.download = fileName;
|
|
60
|
+
anchor.style.display = "none";
|
|
61
|
+
doc.body.appendChild(anchor);
|
|
62
|
+
anchor.dispatchEvent(new win.MouseEvent("click"));
|
|
63
|
+
setTimeout(() => {
|
|
64
|
+
win.URL.revokeObjectURL(url2);
|
|
65
|
+
anchor.remove();
|
|
66
|
+
}, revokeTimeout);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const url = win.URL.createObjectURL(blob);
|
|
70
|
+
const popup = win.open(url, "_blank");
|
|
71
|
+
if (!popup) {
|
|
72
|
+
win.location.href = url;
|
|
73
|
+
}
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
win.URL.revokeObjectURL(url);
|
|
76
|
+
}, revokeTimeout);
|
|
77
|
+
}
|
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
+
0 && (module.exports = {
|
|
80
|
+
downloadFile
|
|
81
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/download-file.ts
|
|
2
|
+
var BOM_REGEX = /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i;
|
|
3
|
+
var MAC_REGEX = /Macintosh/;
|
|
4
|
+
var APPLE_WEBKIT_REGEX = /AppleWebKit/;
|
|
5
|
+
var SAFARI_REGEX = /Safari/;
|
|
6
|
+
function getBlob(blobOrString, type, appendBOM) {
|
|
7
|
+
let blob = typeof blobOrString === "string" ? new Blob([blobOrString], { type }) : blobOrString;
|
|
8
|
+
if (appendBOM && BOM_REGEX.test(blob.type)) {
|
|
9
|
+
return new Blob([String.fromCharCode(65279), blob], { type: blob.type });
|
|
10
|
+
}
|
|
11
|
+
return blob;
|
|
12
|
+
}
|
|
13
|
+
function isMSEdge(win) {
|
|
14
|
+
return Boolean(win.navigator && win.navigator.msSaveOrOpenBlob);
|
|
15
|
+
}
|
|
16
|
+
function isWebView(win) {
|
|
17
|
+
return win.navigator && MAC_REGEX.test(win.navigator.userAgent) && APPLE_WEBKIT_REGEX.test(win.navigator.userAgent) && !SAFARI_REGEX.test(win.navigator.userAgent);
|
|
18
|
+
}
|
|
19
|
+
function downloadFile(options) {
|
|
20
|
+
const { file, win = window, type, name, appendBOM, revokeTimeout = 0 } = options;
|
|
21
|
+
const doc = win.document;
|
|
22
|
+
const blob = getBlob(file, type, appendBOM);
|
|
23
|
+
const fileName = (file instanceof File ? name || file.name : name) || "file-download";
|
|
24
|
+
if (isMSEdge(win)) {
|
|
25
|
+
win.navigator.msSaveOrOpenBlob(blob, fileName);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const isMacOSWebView = isWebView(win);
|
|
29
|
+
const anchor = doc.createElement("a");
|
|
30
|
+
const canUseDownload = "download" in anchor && !isMacOSWebView;
|
|
31
|
+
if (canUseDownload) {
|
|
32
|
+
const url2 = win.URL.createObjectURL(blob);
|
|
33
|
+
anchor.href = url2;
|
|
34
|
+
anchor.rel = "noopener";
|
|
35
|
+
anchor.download = fileName;
|
|
36
|
+
anchor.style.display = "none";
|
|
37
|
+
doc.body.appendChild(anchor);
|
|
38
|
+
anchor.dispatchEvent(new win.MouseEvent("click"));
|
|
39
|
+
setTimeout(() => {
|
|
40
|
+
win.URL.revokeObjectURL(url2);
|
|
41
|
+
anchor.remove();
|
|
42
|
+
}, revokeTimeout);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const url = win.URL.createObjectURL(blob);
|
|
46
|
+
const popup = win.open(url, "_blank");
|
|
47
|
+
if (!popup) {
|
|
48
|
+
win.location.href = url;
|
|
49
|
+
}
|
|
50
|
+
setTimeout(() => {
|
|
51
|
+
win.URL.revokeObjectURL(url);
|
|
52
|
+
}, revokeTimeout);
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
downloadFile
|
|
56
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/get-accept-attr.ts
|
|
21
|
+
var get_accept_attr_exports = {};
|
|
22
|
+
__export(get_accept_attr_exports, {
|
|
23
|
+
getAcceptAttrString: () => getAcceptAttrString
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(get_accept_attr_exports);
|
|
26
|
+
function isMIMEType(v) {
|
|
27
|
+
return v === "audio/*" || v === "video/*" || v === "image/*" || v === "text/*" || /\w+\/[-+.\w]+/g.test(v);
|
|
28
|
+
}
|
|
29
|
+
function isExt(v) {
|
|
30
|
+
return /^.*\.[\w]+$/.test(v);
|
|
31
|
+
}
|
|
32
|
+
var isValidMIME = (v) => isMIMEType(v) || isExt(v);
|
|
33
|
+
function getAcceptAttrString(accept) {
|
|
34
|
+
if (accept == null) return;
|
|
35
|
+
if (typeof accept === "string") {
|
|
36
|
+
return accept;
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(accept)) {
|
|
39
|
+
return accept.filter(isValidMIME).join(",");
|
|
40
|
+
}
|
|
41
|
+
return Object.entries(accept).reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], []).filter(isValidMIME).join(",");
|
|
42
|
+
}
|
|
43
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
44
|
+
0 && (module.exports = {
|
|
45
|
+
getAcceptAttrString
|
|
46
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/get-accept-attr.ts
|
|
2
|
+
function isMIMEType(v) {
|
|
3
|
+
return v === "audio/*" || v === "video/*" || v === "image/*" || v === "text/*" || /\w+\/[-+.\w]+/g.test(v);
|
|
4
|
+
}
|
|
5
|
+
function isExt(v) {
|
|
6
|
+
return /^.*\.[\w]+$/.test(v);
|
|
7
|
+
}
|
|
8
|
+
var isValidMIME = (v) => isMIMEType(v) || isExt(v);
|
|
9
|
+
function getAcceptAttrString(accept) {
|
|
10
|
+
if (accept == null) return;
|
|
11
|
+
if (typeof accept === "string") {
|
|
12
|
+
return accept;
|
|
13
|
+
}
|
|
14
|
+
if (Array.isArray(accept)) {
|
|
15
|
+
return accept.filter(isValidMIME).join(",");
|
|
16
|
+
}
|
|
17
|
+
return Object.entries(accept).reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], []).filter(isValidMIME).join(",");
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
getAcceptAttrString
|
|
21
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/get-file-data-url.ts
|
|
21
|
+
var get_file_data_url_exports = {};
|
|
22
|
+
__export(get_file_data_url_exports, {
|
|
23
|
+
getFileDataUrl: () => getFileDataUrl
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(get_file_data_url_exports);
|
|
26
|
+
var getFileDataUrl = async (file) => {
|
|
27
|
+
const reader = new FileReader();
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
reader.onerror = () => {
|
|
30
|
+
reader.abort();
|
|
31
|
+
reject(new Error("There was an error reading a file"));
|
|
32
|
+
};
|
|
33
|
+
reader.onloadend = () => {
|
|
34
|
+
const { result } = reader;
|
|
35
|
+
if (result instanceof ArrayBuffer) {
|
|
36
|
+
reject(new Error("Expected DataURL as string from Blob/File, got ArrayBuffer"));
|
|
37
|
+
} else {
|
|
38
|
+
resolve(result || void 0);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
reader.readAsDataURL(file);
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
getFileDataUrl
|
|
47
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/get-file-data-url.ts
|
|
2
|
+
var getFileDataUrl = async (file) => {
|
|
3
|
+
const reader = new FileReader();
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
reader.onerror = () => {
|
|
6
|
+
reader.abort();
|
|
7
|
+
reject(new Error("There was an error reading a file"));
|
|
8
|
+
};
|
|
9
|
+
reader.onloadend = () => {
|
|
10
|
+
const { result } = reader;
|
|
11
|
+
if (result instanceof ArrayBuffer) {
|
|
12
|
+
reject(new Error("Expected DataURL as string from Blob/File, got ArrayBuffer"));
|
|
13
|
+
} else {
|
|
14
|
+
resolve(result || void 0);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
reader.readAsDataURL(file);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
export {
|
|
21
|
+
getFileDataUrl
|
|
22
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/get-file-mime-type.ts
|
|
21
|
+
var get_file_mime_type_exports = {};
|
|
22
|
+
__export(get_file_mime_type_exports, {
|
|
23
|
+
getFileMimeType: () => getFileMimeType
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(get_file_mime_type_exports);
|
|
26
|
+
var import_mime_types = require("./mime-types.cjs");
|
|
27
|
+
function getFileMimeType(name) {
|
|
28
|
+
const extension = name.split(".").pop();
|
|
29
|
+
return extension ? import_mime_types.mimeTypesMap.get(extension) || null : null;
|
|
30
|
+
}
|
|
31
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
32
|
+
0 && (module.exports = {
|
|
33
|
+
getFileMimeType
|
|
34
|
+
});
|