@zag-js/file-utils 1.34.1 → 1.35.1
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,32 @@
|
|
|
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-total-file-size.ts
|
|
21
|
+
var get_total_file_size_exports = {};
|
|
22
|
+
__export(get_total_file_size_exports, {
|
|
23
|
+
getTotalFileSize: () => getTotalFileSize
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(get_total_file_size_exports);
|
|
26
|
+
var getTotalFileSize = (files) => {
|
|
27
|
+
return files.reduce((acc, file) => acc + file.size, 0);
|
|
28
|
+
};
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
getTotalFileSize
|
|
32
|
+
});
|
package/dist/index.d.mts
CHANGED
|
@@ -1,62 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*/
|
|
13
|
-
type: string;
|
|
14
|
-
/**
|
|
15
|
-
* The file contents
|
|
16
|
-
*/
|
|
17
|
-
file: File | Blob | string;
|
|
18
|
-
/**
|
|
19
|
-
* The window environment
|
|
20
|
-
*/
|
|
21
|
-
win?: typeof window | undefined;
|
|
22
|
-
/**
|
|
23
|
-
* Whether to add a BOM (Byte Order Mark) to the file.
|
|
24
|
-
* Useful for CSV files.
|
|
25
|
-
*/
|
|
26
|
-
appendBOM?: boolean | undefined;
|
|
27
|
-
/**
|
|
28
|
-
* The timeout in milliseconds to revoke the object URL.
|
|
29
|
-
* This is a safeguard for when the browser has not finished reading the
|
|
30
|
-
* file data from memory before the URL is revoked.
|
|
31
|
-
* @default 0
|
|
32
|
-
*/
|
|
33
|
-
revokeTimeout?: number | undefined;
|
|
34
|
-
}
|
|
35
|
-
declare function downloadFile(options: DownloadFileOptions): void;
|
|
36
|
-
|
|
37
|
-
declare function getAcceptAttrString(accept: Record<string, string[]> | string | string[] | undefined): string | undefined;
|
|
38
|
-
|
|
39
|
-
declare const getFileDataUrl: (file: File | Blob) => Promise<string | undefined>;
|
|
40
|
-
|
|
41
|
-
declare const getTotalFileSize: (files: File[]) => number;
|
|
42
|
-
|
|
43
|
-
declare const isFileEqual: (file1: File, file2: File) => boolean;
|
|
44
|
-
|
|
45
|
-
type AnyString = string & {};
|
|
46
|
-
type FileError = "TOO_MANY_FILES" | "FILE_INVALID_TYPE" | "FILE_TOO_LARGE" | "FILE_TOO_SMALL" | "FILE_INVALID" | "FILE_EXISTS" | AnyString;
|
|
47
|
-
type ImageFileMimeType = "image/png" | "image/gif" | "image/jpeg" | "image/svg+xml" | "image/webp" | "image/avif" | "image/heic" | "image/bmp";
|
|
48
|
-
type ApplicationFileMimeType = "application/pdf" | "application/zip" | "application/json" | "application/xml" | "application/msword" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | "application/vnd.ms-excel" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | "application/vnd.ms-powerpoint" | "application/vnd.openxmlformats-officedocument.presentationml.presentation" | "application/rtf" | "application/x-rar" | "application/x-7z-compressed" | "application/x-tar" | "application/vnd.microsoft.portable-executable";
|
|
49
|
-
type TextFileMimeType = "text/css" | "text/csv" | "text/html" | "text/markdown" | "text/plain";
|
|
50
|
-
type FontFileMimeType = "font/ttf" | "font/otf" | "font/woff" | "font/woff2" | "font/eot" | "font/svg";
|
|
51
|
-
type VideoFileMimeType = "video/mp4" | "video/webm" | "video/ogg" | "video/quicktime" | "video/x-msvideo";
|
|
52
|
-
type AudioFileMimeType = "audio/mpeg" | "audio/ogg" | "audio/wav" | "audio/webm" | "audio/aac" | "audio/flac" | "audio/x-m4a";
|
|
53
|
-
type FileMimeTypeGroup = "image/*" | "audio/*" | "video/*" | "text/*" | "application/*" | "font/*";
|
|
54
|
-
type FileMimeType = ImageFileMimeType | ApplicationFileMimeType | TextFileMimeType | FontFileMimeType | VideoFileMimeType | AudioFileMimeType | FileMimeTypeGroup | AnyString;
|
|
55
|
-
|
|
56
|
-
declare function isValidFileSize(file: File, minSize?: number, maxSize?: number): [boolean, FileError | null];
|
|
57
|
-
|
|
58
|
-
declare function isValidFileType(file: File, accept: string | undefined): [boolean, FileError | null];
|
|
59
|
-
|
|
60
|
-
declare function getFileMimeType(name: string): FileMimeType | null;
|
|
61
|
-
|
|
62
|
-
export { type ApplicationFileMimeType, type AudioFileMimeType, type DownloadFileOptions, type FileError, type FileMimeType, type FileMimeTypeGroup, type FontFileMimeType, type ImageFileMimeType, type TextFileMimeType, type VideoFileMimeType, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getFileEntries, getFileMimeType, getTotalFileSize, isFileEqual, isValidFileSize, isValidFileType };
|
|
1
|
+
export { getFileEntries } from './data-transfer.mjs';
|
|
2
|
+
export { dataURItoBlob } from './data-url-to-blob.mjs';
|
|
3
|
+
export { DownloadFileOptions, downloadFile } from './download-file.mjs';
|
|
4
|
+
export { getAcceptAttrString } from './get-accept-attr.mjs';
|
|
5
|
+
export { getFileDataUrl } from './get-file-data-url.mjs';
|
|
6
|
+
export { getTotalFileSize } from './get-total-file-size.mjs';
|
|
7
|
+
export { isFileEqual } from './is-file-equal.mjs';
|
|
8
|
+
export { isValidFileSize } from './is-valid-file-size.mjs';
|
|
9
|
+
export { isValidFileType } from './is-valid-file-type.mjs';
|
|
10
|
+
export { getFileMimeType } from './get-file-mime-type.mjs';
|
|
11
|
+
export { ApplicationFileMimeType, AudioFileMimeType, FileError, FileMimeType, FileMimeTypeGroup, FontFileMimeType, ImageFileMimeType, TextFileMimeType, VideoFileMimeType } from './types.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,62 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*/
|
|
13
|
-
type: string;
|
|
14
|
-
/**
|
|
15
|
-
* The file contents
|
|
16
|
-
*/
|
|
17
|
-
file: File | Blob | string;
|
|
18
|
-
/**
|
|
19
|
-
* The window environment
|
|
20
|
-
*/
|
|
21
|
-
win?: typeof window | undefined;
|
|
22
|
-
/**
|
|
23
|
-
* Whether to add a BOM (Byte Order Mark) to the file.
|
|
24
|
-
* Useful for CSV files.
|
|
25
|
-
*/
|
|
26
|
-
appendBOM?: boolean | undefined;
|
|
27
|
-
/**
|
|
28
|
-
* The timeout in milliseconds to revoke the object URL.
|
|
29
|
-
* This is a safeguard for when the browser has not finished reading the
|
|
30
|
-
* file data from memory before the URL is revoked.
|
|
31
|
-
* @default 0
|
|
32
|
-
*/
|
|
33
|
-
revokeTimeout?: number | undefined;
|
|
34
|
-
}
|
|
35
|
-
declare function downloadFile(options: DownloadFileOptions): void;
|
|
36
|
-
|
|
37
|
-
declare function getAcceptAttrString(accept: Record<string, string[]> | string | string[] | undefined): string | undefined;
|
|
38
|
-
|
|
39
|
-
declare const getFileDataUrl: (file: File | Blob) => Promise<string | undefined>;
|
|
40
|
-
|
|
41
|
-
declare const getTotalFileSize: (files: File[]) => number;
|
|
42
|
-
|
|
43
|
-
declare const isFileEqual: (file1: File, file2: File) => boolean;
|
|
44
|
-
|
|
45
|
-
type AnyString = string & {};
|
|
46
|
-
type FileError = "TOO_MANY_FILES" | "FILE_INVALID_TYPE" | "FILE_TOO_LARGE" | "FILE_TOO_SMALL" | "FILE_INVALID" | "FILE_EXISTS" | AnyString;
|
|
47
|
-
type ImageFileMimeType = "image/png" | "image/gif" | "image/jpeg" | "image/svg+xml" | "image/webp" | "image/avif" | "image/heic" | "image/bmp";
|
|
48
|
-
type ApplicationFileMimeType = "application/pdf" | "application/zip" | "application/json" | "application/xml" | "application/msword" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | "application/vnd.ms-excel" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | "application/vnd.ms-powerpoint" | "application/vnd.openxmlformats-officedocument.presentationml.presentation" | "application/rtf" | "application/x-rar" | "application/x-7z-compressed" | "application/x-tar" | "application/vnd.microsoft.portable-executable";
|
|
49
|
-
type TextFileMimeType = "text/css" | "text/csv" | "text/html" | "text/markdown" | "text/plain";
|
|
50
|
-
type FontFileMimeType = "font/ttf" | "font/otf" | "font/woff" | "font/woff2" | "font/eot" | "font/svg";
|
|
51
|
-
type VideoFileMimeType = "video/mp4" | "video/webm" | "video/ogg" | "video/quicktime" | "video/x-msvideo";
|
|
52
|
-
type AudioFileMimeType = "audio/mpeg" | "audio/ogg" | "audio/wav" | "audio/webm" | "audio/aac" | "audio/flac" | "audio/x-m4a";
|
|
53
|
-
type FileMimeTypeGroup = "image/*" | "audio/*" | "video/*" | "text/*" | "application/*" | "font/*";
|
|
54
|
-
type FileMimeType = ImageFileMimeType | ApplicationFileMimeType | TextFileMimeType | FontFileMimeType | VideoFileMimeType | AudioFileMimeType | FileMimeTypeGroup | AnyString;
|
|
55
|
-
|
|
56
|
-
declare function isValidFileSize(file: File, minSize?: number, maxSize?: number): [boolean, FileError | null];
|
|
57
|
-
|
|
58
|
-
declare function isValidFileType(file: File, accept: string | undefined): [boolean, FileError | null];
|
|
59
|
-
|
|
60
|
-
declare function getFileMimeType(name: string): FileMimeType | null;
|
|
61
|
-
|
|
62
|
-
export { type ApplicationFileMimeType, type AudioFileMimeType, type DownloadFileOptions, type FileError, type FileMimeType, type FileMimeTypeGroup, type FontFileMimeType, type ImageFileMimeType, type TextFileMimeType, type VideoFileMimeType, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getFileEntries, getFileMimeType, getTotalFileSize, isFileEqual, isValidFileSize, isValidFileType };
|
|
1
|
+
export { getFileEntries } from './data-transfer.js';
|
|
2
|
+
export { dataURItoBlob } from './data-url-to-blob.js';
|
|
3
|
+
export { DownloadFileOptions, downloadFile } from './download-file.js';
|
|
4
|
+
export { getAcceptAttrString } from './get-accept-attr.js';
|
|
5
|
+
export { getFileDataUrl } from './get-file-data-url.js';
|
|
6
|
+
export { getTotalFileSize } from './get-total-file-size.js';
|
|
7
|
+
export { isFileEqual } from './is-file-equal.js';
|
|
8
|
+
export { isValidFileSize } from './is-valid-file-size.js';
|
|
9
|
+
export { isValidFileType } from './is-valid-file-type.js';
|
|
10
|
+
export { getFileMimeType } from './get-file-mime-type.js';
|
|
11
|
+
export { ApplicationFileMimeType, AudioFileMimeType, FileError, FileMimeType, FileMimeTypeGroup, FontFileMimeType, ImageFileMimeType, TextFileMimeType, VideoFileMimeType } from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -1,240 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
10
13
|
};
|
|
11
|
-
var
|
|
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
|
-
const promises = entries.map((entry) => {
|
|
40
|
-
if (!entry) return null;
|
|
41
|
-
if (isDirectoryEntry(entry)) {
|
|
42
|
-
return getDirectoryFiles(entry.createReader(), `${path}${entry.name}`);
|
|
43
|
-
}
|
|
44
|
-
if (isFileEntry(entry)) {
|
|
45
|
-
return new Promise((resolve2) => {
|
|
46
|
-
entry.file((file) => {
|
|
47
|
-
resolve2(addRelativePath(file, path));
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}).filter((b) => b);
|
|
52
|
-
entryPromises.push(Promise.all(promises));
|
|
53
|
-
readDirectoryEntries();
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
readDirectoryEntries();
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
|
|
17
|
+
// src/index.ts
|
|
18
|
+
var index_exports = {};
|
|
19
|
+
module.exports = __toCommonJS(index_exports);
|
|
20
|
+
__reExport(index_exports, require("./data-transfer.cjs"), module.exports);
|
|
21
|
+
__reExport(index_exports, require("./data-url-to-blob.cjs"), module.exports);
|
|
22
|
+
__reExport(index_exports, require("./download-file.cjs"), module.exports);
|
|
23
|
+
__reExport(index_exports, require("./get-accept-attr.cjs"), module.exports);
|
|
24
|
+
__reExport(index_exports, require("./get-file-data-url.cjs"), module.exports);
|
|
25
|
+
__reExport(index_exports, require("./get-total-file-size.cjs"), module.exports);
|
|
26
|
+
__reExport(index_exports, require("./is-file-equal.cjs"), module.exports);
|
|
27
|
+
__reExport(index_exports, require("./is-valid-file-size.cjs"), module.exports);
|
|
28
|
+
__reExport(index_exports, require("./is-valid-file-type.cjs"), module.exports);
|
|
29
|
+
__reExport(index_exports, require("./get-file-mime-type.cjs"), module.exports);
|
|
30
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
31
|
+
0 && (module.exports = {
|
|
32
|
+
...require("./data-transfer.cjs"),
|
|
33
|
+
...require("./data-url-to-blob.cjs"),
|
|
34
|
+
...require("./download-file.cjs"),
|
|
35
|
+
...require("./get-accept-attr.cjs"),
|
|
36
|
+
...require("./get-file-data-url.cjs"),
|
|
37
|
+
...require("./get-total-file-size.cjs"),
|
|
38
|
+
...require("./is-file-equal.cjs"),
|
|
39
|
+
...require("./is-valid-file-size.cjs"),
|
|
40
|
+
...require("./is-valid-file-type.cjs"),
|
|
41
|
+
...require("./get-file-mime-type.cjs")
|
|
57
42
|
});
|
|
58
|
-
|
|
59
|
-
// src/data-url-to-blob.ts
|
|
60
|
-
function dataURItoBlob(uri) {
|
|
61
|
-
const binary = atob(uri.split(",")[1]);
|
|
62
|
-
const mimeString = uri.split(",")[0].split(":")[1].split(";")[0];
|
|
63
|
-
const buffer = new ArrayBuffer(binary.length);
|
|
64
|
-
const intArray = new Uint8Array(buffer);
|
|
65
|
-
for (let i = 0; i < binary.length; i++) {
|
|
66
|
-
intArray[i] = binary.charCodeAt(i);
|
|
67
|
-
}
|
|
68
|
-
return new Blob([buffer], { type: mimeString });
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// src/download-file.ts
|
|
72
|
-
var BOM_REGEX = /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i;
|
|
73
|
-
var MAC_REGEX = /Macintosh/;
|
|
74
|
-
var APPLE_WEBKIT_REGEX = /AppleWebKit/;
|
|
75
|
-
var SAFARI_REGEX = /Safari/;
|
|
76
|
-
function getBlob(blobOrString, type, appendBOM) {
|
|
77
|
-
let blob = typeof blobOrString === "string" ? new Blob([blobOrString], { type }) : blobOrString;
|
|
78
|
-
if (appendBOM && BOM_REGEX.test(blob.type)) {
|
|
79
|
-
return new Blob([String.fromCharCode(65279), blob], { type: blob.type });
|
|
80
|
-
}
|
|
81
|
-
return blob;
|
|
82
|
-
}
|
|
83
|
-
function isMSEdge(win) {
|
|
84
|
-
return Boolean(win.navigator && win.navigator.msSaveOrOpenBlob);
|
|
85
|
-
}
|
|
86
|
-
function isWebView(win) {
|
|
87
|
-
return win.navigator && MAC_REGEX.test(win.navigator.userAgent) && APPLE_WEBKIT_REGEX.test(win.navigator.userAgent) && !SAFARI_REGEX.test(win.navigator.userAgent);
|
|
88
|
-
}
|
|
89
|
-
function downloadFile(options) {
|
|
90
|
-
const { file, win = window, type, name, appendBOM, revokeTimeout = 0 } = options;
|
|
91
|
-
const doc = win.document;
|
|
92
|
-
const blob = getBlob(file, type, appendBOM);
|
|
93
|
-
const fileName = (file instanceof File ? name || file.name : name) || "file-download";
|
|
94
|
-
if (isMSEdge(win)) {
|
|
95
|
-
win.navigator.msSaveOrOpenBlob(blob, fileName);
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
const isMacOSWebView = isWebView(win);
|
|
99
|
-
const anchor = doc.createElement("a");
|
|
100
|
-
const canUseDownload = "download" in anchor && !isMacOSWebView;
|
|
101
|
-
if (canUseDownload) {
|
|
102
|
-
const url2 = win.URL.createObjectURL(blob);
|
|
103
|
-
anchor.href = url2;
|
|
104
|
-
anchor.rel = "noopener";
|
|
105
|
-
anchor.download = fileName;
|
|
106
|
-
anchor.style.display = "none";
|
|
107
|
-
doc.body.appendChild(anchor);
|
|
108
|
-
anchor.dispatchEvent(new win.MouseEvent("click"));
|
|
109
|
-
setTimeout(() => {
|
|
110
|
-
win.URL.revokeObjectURL(url2);
|
|
111
|
-
anchor.remove();
|
|
112
|
-
}, revokeTimeout);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const url = win.URL.createObjectURL(blob);
|
|
116
|
-
const popup = win.open(url, "_blank");
|
|
117
|
-
if (!popup) {
|
|
118
|
-
win.location.href = url;
|
|
119
|
-
}
|
|
120
|
-
setTimeout(() => {
|
|
121
|
-
win.URL.revokeObjectURL(url);
|
|
122
|
-
}, revokeTimeout);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// src/get-accept-attr.ts
|
|
126
|
-
function isMIMEType(v) {
|
|
127
|
-
return v === "audio/*" || v === "video/*" || v === "image/*" || v === "text/*" || /\w+\/[-+.\w]+/g.test(v);
|
|
128
|
-
}
|
|
129
|
-
function isExt(v) {
|
|
130
|
-
return /^.*\.[\w]+$/.test(v);
|
|
131
|
-
}
|
|
132
|
-
var isValidMIME = (v) => isMIMEType(v) || isExt(v);
|
|
133
|
-
function getAcceptAttrString(accept) {
|
|
134
|
-
if (accept == null) return;
|
|
135
|
-
if (typeof accept === "string") {
|
|
136
|
-
return accept;
|
|
137
|
-
}
|
|
138
|
-
if (Array.isArray(accept)) {
|
|
139
|
-
return accept.filter(isValidMIME).join(",");
|
|
140
|
-
}
|
|
141
|
-
return Object.entries(accept).reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], []).filter(isValidMIME).join(",");
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// src/get-file-data-url.ts
|
|
145
|
-
var getFileDataUrl = async (file) => {
|
|
146
|
-
const reader = new FileReader();
|
|
147
|
-
return new Promise((resolve, reject) => {
|
|
148
|
-
reader.onerror = () => {
|
|
149
|
-
reader.abort();
|
|
150
|
-
reject(new Error("There was an error reading a file"));
|
|
151
|
-
};
|
|
152
|
-
reader.onloadend = () => {
|
|
153
|
-
const { result } = reader;
|
|
154
|
-
if (result instanceof ArrayBuffer) {
|
|
155
|
-
reject(new Error("Expected DataURL as string from Blob/File, got ArrayBuffer"));
|
|
156
|
-
} else {
|
|
157
|
-
resolve(result || void 0);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
reader.readAsDataURL(file);
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
// src/get-total-file-size.ts
|
|
165
|
-
var getTotalFileSize = (files) => {
|
|
166
|
-
return files.reduce((acc, file) => acc + file.size, 0);
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
// src/is-file-equal.ts
|
|
170
|
-
var isFileEqual = (file1, file2) => {
|
|
171
|
-
return file1.name === file2.name && file1.size === file2.size && file1.type === file2.type;
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
// src/is-valid-file-size.ts
|
|
175
|
-
var isDefined = (v) => v !== void 0 && v !== null;
|
|
176
|
-
function isValidFileSize(file, minSize, maxSize) {
|
|
177
|
-
if (isDefined(file.size)) {
|
|
178
|
-
if (isDefined(minSize) && isDefined(maxSize)) {
|
|
179
|
-
if (file.size > maxSize) return [false, "FILE_TOO_LARGE"];
|
|
180
|
-
if (file.size < minSize) return [false, "FILE_TOO_SMALL"];
|
|
181
|
-
} else if (isDefined(minSize) && file.size < minSize) {
|
|
182
|
-
return [false, "FILE_TOO_SMALL"];
|
|
183
|
-
} else if (isDefined(maxSize) && file.size > maxSize) {
|
|
184
|
-
return [false, "FILE_TOO_LARGE"];
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return [true, null];
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// src/mime-types.ts
|
|
191
|
-
var mimeTypes = "3g2_video/3gpp2[3gp,3gpp_video/3gpp[3mf_model/3mf[7z_application/x-7z-compressed[aac_audio/aac[ac_application/pkix-attr-cert[adp_audio/adpcm[adts_audio/aac[ai_application/postscript[aml_application/automationml-aml+xml[amlx_application/automationml-amlx+zip[amr_audio/amr[apk_application/vnd.android.package-archive[apng_image/apng[appcache,manifest_text/cache-manifest[appinstaller_application/appinstaller[appx_application/appx[appxbundle_application/appxbundle[asc_application/pgp-keys[atom_application/atom+xml[atomcat_application/atomcat+xml[atomdeleted_application/atomdeleted+xml[atomsvc_application/atomsvc+xml[au,snd_audio/basic[avi_video/x-msvideo[avci_image/avci[avcs_image/avcs[avif_image/avif[aw_application/applixware[bdoc_application/bdoc[bin,bpk,buffer,deb,deploy,dist,distz,dll,dmg,dms,dump,elc,exe,img,iso,lrf,mar,msi,msm,msp,pkg,so_application/octet-stream[bmp,dib_image/bmp[btf,btif_image/prs.btif[bz2_application/x-bzip2[c_text/x-c[ccxml_application/ccxml+xml[cdfx_application/cdfx+xml[cdmia_application/cdmi-capability[cdmic_application/cdmi-container[cdmid_application/cdmi-domain[cdmio_application/cdmi-object[cdmiq_application/cdmi-queue[cer_application/pkix-cert[cgm_image/cgm[cjs_application/node[class_application/java-vm[coffee,litcoffee_text/coffeescript[conf,def,in,ini,list,log,text,txt_text/plain[cpp,cxx,cc_text/x-c++src[cpl_application/cpl+xml[cpt_application/mac-compactpro[crl_application/pkix-crl[css_text/css[csv_text/csv[cu_application/cu-seeme[cwl_application/cwl[cww_application/prs.cww[davmount_application/davmount+xml[dbk_application/docbook+xml[doc_application/msword[docx_application/vnd.openxmlformats-officedocument.wordprocessingml.document[dsc_text/prs.lines.tag[dssc_application/dssc+der[dtd_application/xml-dtd[dwd_application/atsc-dwd+xml[ear,jar,war_application/java-archive[ecma_application/ecmascript[emf_image/emf[eml,mime_message/rfc822[emma_application/emma+xml[emotionml_application/emotionml+xml[eot_application/vnd.ms-fontobject[eps,ps_application/postscript[epub_application/epub+zip[exi_application/exi[exp_application/express[exr_image/aces[ez_application/andrew-inset[fdf_application/fdf[fdt_application/fdt+xml[fits_image/fits[flac_audio/flac[flv_video/x-flv[g3_image/g3fax[geojson_application/geo+json[gif_image/gif[glb_model/gltf-binary[gltf_model/gltf+json[gml_application/gml+xml[go_text/x-go[gpx_application/gpx+xml[gz_application/gzip[h_text/x-h[h261_video/h261[h263_video/h263[h264_video/h264[heic_image/heic[heics_image/heic-sequence[heif_image/heif[heifs_image/heif-sequence[htm,html,shtml_text/html[ico_image/x-icon[icns_image/x-icns[ics,ifb_text/calendar[iges,igs_model/iges[ink,inkml_application/inkml+xml[ipa_application/octet-stream[java_text/x-java-source[jp2,jpg2_image/jp2[jpeg,jpe,jpg_image/jpeg[jpf,jpx_image/jpx[jpm,jpgm_image/jpm[jpgv_video/jpeg[jph_image/jph[js,mjs_text/javascript[json_application/json[json5_application/json5[jsonld_application/ld+json[jsx_text/jsx[jxl_image/jxl[jxr_image/jxr[ktx_image/ktx[ktx2_image/ktx2[less_text/less[m1v,m2v,mpe,mpeg,mpg_video/mpeg[m4a_audio/mp4[m4v_video/x-m4v[md,markdown_text/markdown[mid,midi,kar,rmi_audio/midi[mkv_video/x-matroska[mp2,mp2a,mp3,mpga,m3a,m2a_audio/mpeg[mp4,mp4v,mpg4_video/mp4[mp4a_audio/mp4[mp4s,m4p_application/mp4[odp_application/vnd.oasis.opendocument.presentation[oda_application/oda[ods_application/vnd.oasis.opendocument.spreadsheet[odt_application/vnd.oasis.opendocument.text[oga,ogg,opus,spx_audio/ogg[ogv_video/ogg[ogx_application/ogg[otf_font/otf[p12,pfx_application/x-pkcs12[pdf_application/pdf[pem_application/x-pem-file[php_text/x-php[png_image/png[ppt_application/vnd.ms-powerpoint[pptx_application/vnd.openxmlformats-officedocument.presentationml.presentation[pskcxml_application/pskc+xml[psd_image/vnd.adobe.photoshop[py_text/x-python[qt,mov_video/quicktime[rar_application/vnd.rar[rdf_application/rdf+xml[rtf_text/rtf[sass_text/x-sass[scss_text/x-scss[sgm,sgml_text/sgml[sh_application/x-sh[svg,svgz_image/svg+xml[swf_application/x-shockwave-flash[tar_application/x-tar[tif,tiff_image/tiff[toml_application/toml[ts_video/mp2t[tsx_text/tsx[tsv_text/tab-separated-values[ttc_font/collection[ttf_font/ttf[vtt_text/vtt[wasm_application/wasm[wav_audio/wav[weba_audio/webm[webm_video/webm[webmanifest_application/manifest+json[webp_image/webp[wma_audio/x-ms-wma[wmv_video/x-ms-wmv[woff_font/woff[woff2_font/woff2[xls_application/vnd.ms-excel[xlsx_application/vnd.openxmlformats-officedocument.spreadsheetml.sheet[xml_application/xml[xz_application/x-xz[yaml,yml_text/yaml[zip_application/zip";
|
|
192
|
-
var mimeTypesMap = new Map(
|
|
193
|
-
mimeTypes.split("[").flatMap((mime) => {
|
|
194
|
-
const [extensions, mimeType] = mime.split("_");
|
|
195
|
-
return extensions.split(",").map((ext) => [ext, mimeType]);
|
|
196
|
-
})
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
// src/get-file-mime-type.ts
|
|
200
|
-
function getFileMimeType(name) {
|
|
201
|
-
const extension = name.split(".").pop();
|
|
202
|
-
return extension ? mimeTypesMap.get(extension) || null : null;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// src/is-valid-file-type.ts
|
|
206
|
-
function isFileAccepted(file, accept) {
|
|
207
|
-
if (file && accept) {
|
|
208
|
-
const types = Array.isArray(accept) ? accept : typeof accept === "string" ? accept.split(",") : [];
|
|
209
|
-
if (types.length === 0) return true;
|
|
210
|
-
const fileName = file.name || "";
|
|
211
|
-
const mimeType = (file.type || getFileMimeType(fileName) || "").toLowerCase();
|
|
212
|
-
const baseMimeType = mimeType.replace(/\/.*$/, "");
|
|
213
|
-
return types.some((type) => {
|
|
214
|
-
const validType = type.trim().toLowerCase();
|
|
215
|
-
if (validType.charAt(0) === ".") {
|
|
216
|
-
return fileName.toLowerCase().endsWith(validType);
|
|
217
|
-
}
|
|
218
|
-
if (validType.endsWith("/*")) {
|
|
219
|
-
return baseMimeType === validType.replace(/\/.*$/, "");
|
|
220
|
-
}
|
|
221
|
-
return mimeType === validType;
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
return true;
|
|
225
|
-
}
|
|
226
|
-
function isValidFileType(file, accept) {
|
|
227
|
-
const isAcceptable = file.type === "application/x-moz-file" || isFileAccepted(file, accept);
|
|
228
|
-
return [isAcceptable, isAcceptable ? null : "FILE_INVALID_TYPE"];
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
exports.dataURItoBlob = dataURItoBlob;
|
|
232
|
-
exports.downloadFile = downloadFile;
|
|
233
|
-
exports.getAcceptAttrString = getAcceptAttrString;
|
|
234
|
-
exports.getFileDataUrl = getFileDataUrl;
|
|
235
|
-
exports.getFileEntries = getFileEntries;
|
|
236
|
-
exports.getFileMimeType = getFileMimeType;
|
|
237
|
-
exports.getTotalFileSize = getTotalFileSize;
|
|
238
|
-
exports.isFileEqual = isFileEqual;
|
|
239
|
-
exports.isValidFileSize = isValidFileSize;
|
|
240
|
-
exports.isValidFileType = isValidFileType;
|