diginext-utils 4.1.7 → 4.1.8
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/esm/Timer.js +2 -11
- package/esm/color.js +1 -1
- package/esm/device/camera.js +3 -12
- package/esm/email/index.js +3 -12
- package/esm/images/downloadByUrl.js +24 -35
- package/esm/images/getImageDimensions.js +15 -26
- package/esm/images/loadImage.js +13 -24
- package/esm/images/loadImageAsBlobUrl.js +17 -28
- package/esm/images/resize.js +4 -13
- package/esm/images/upload.js +3 -12
- package/esm/lib/batchProcessor.js +12 -25
- package/esm/string/guessMimeType.js +64 -75
- package/package.json +1 -1
package/esm/Timer.js
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
const Timer = {
|
|
11
|
-
wait: (ms) =>
|
|
2
|
+
wait: async (ms) => {
|
|
12
3
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
13
|
-
}
|
|
4
|
+
},
|
|
14
5
|
};
|
|
15
6
|
export default Timer;
|
package/esm/color.js
CHANGED
|
@@ -42,7 +42,7 @@ export const pSBC = (p, c0, c1, l) => {
|
|
|
42
42
|
else if (l)
|
|
43
43
|
(r = m(P * f.r + p * t.r)), (g = m(P * f.g + p * t.g)), (b = m(P * f.b + p * t.b));
|
|
44
44
|
else
|
|
45
|
-
(r = m(
|
|
45
|
+
(r = m((P * f.r ** 2 + p * t.r ** 2) ** 0.5)), (g = m((P * f.g ** 2 + p * t.g ** 2) ** 0.5)), (b = m((P * f.b ** 2 + p * t.b ** 2) ** 0.5));
|
|
46
46
|
(a = f.a), (t = t.a), (f = a >= 0 || t >= 0), (a = f ? (a < 0 ? t : t < 0 ? a : a * P + t * p) : 0);
|
|
47
47
|
if (h)
|
|
48
48
|
return "rgb" + (f ? "a(" : "(") + r + "," + g + "," + b + (f ? "," + m(a * 1000) / 1000 : "") + ")";
|
package/esm/device/camera.js
CHANGED
|
@@ -1,19 +1,10 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { requestCamera } from "../permission/requestCamera.js";
|
|
11
2
|
export function getWebcam(params = { facingMode: "environment", audio: true }) {
|
|
12
3
|
if (typeof window == "undefined")
|
|
13
4
|
return;
|
|
14
5
|
const { container, facingMode = "environment", audio = true } = params;
|
|
15
6
|
const _container = container || document.body;
|
|
16
|
-
return new Promise((resolve, reject) =>
|
|
7
|
+
return new Promise(async (resolve, reject) => {
|
|
17
8
|
const video = document.createElement("video");
|
|
18
9
|
video.style.position = "absolute";
|
|
19
10
|
video.style.top = "0";
|
|
@@ -30,7 +21,7 @@ export function getWebcam(params = { facingMode: "environment", audio: true }) {
|
|
|
30
21
|
audio,
|
|
31
22
|
};
|
|
32
23
|
console.log("[Camera.js] Requesting:", requestedMediaConstraints);
|
|
33
|
-
const res =
|
|
24
|
+
const res = await requestCamera({ audio });
|
|
34
25
|
if (!res) {
|
|
35
26
|
setTimeout(function () {
|
|
36
27
|
handleError({ name: "Error", message: "NotSupported" });
|
|
@@ -173,5 +164,5 @@ export function getWebcam(params = { facingMode: "environment", audio: true }) {
|
|
|
173
164
|
console.error(errMsg);
|
|
174
165
|
reject();
|
|
175
166
|
}
|
|
176
|
-
})
|
|
167
|
+
});
|
|
177
168
|
}
|
package/esm/email/index.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import validate from "deep-email-validator";
|
|
11
2
|
import toString from "lodash/toString.js";
|
|
12
3
|
function extractReasons(emailValidatorOutput) {
|
|
@@ -21,11 +12,11 @@ function extractReasons(emailValidatorOutput) {
|
|
|
21
12
|
return reasons;
|
|
22
13
|
}
|
|
23
14
|
// SMTP Error: The mail address that you specified was not syntactically correct.
|
|
24
|
-
export const emailValidator = (email, opts) =>
|
|
15
|
+
export const emailValidator = async (email, opts) => {
|
|
25
16
|
email = `${toString(email)}`;
|
|
26
|
-
const res =
|
|
17
|
+
const res = await validate(Object.assign({ email: email, sender: email, validateRegex: true, validateMx: true, validateTypo: false, validateDisposable: true, validateSMTP: false }, opts));
|
|
27
18
|
if (res.valid)
|
|
28
19
|
return { status: true };
|
|
29
20
|
const reason = extractReasons(res);
|
|
30
21
|
return { status: false, reason };
|
|
31
|
-
}
|
|
22
|
+
};
|
|
@@ -1,33 +1,24 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import saveAs from "file-saver";
|
|
11
2
|
import { isAndroid, isMobile } from "../device/index.js";
|
|
12
3
|
import { randomFileName } from "../string/random.js";
|
|
13
4
|
import { getExtensionFromMimeType, getFileNameWithExtension } from "../string/url.js";
|
|
14
|
-
export const doGetFile = (url, data) =>
|
|
15
|
-
const response =
|
|
5
|
+
export const doGetFile = async (url, data) => {
|
|
6
|
+
const response = await fetch(url);
|
|
16
7
|
if (!response.ok) {
|
|
17
8
|
return undefined;
|
|
18
9
|
}
|
|
19
|
-
const blob =
|
|
10
|
+
const blob = await response.blob();
|
|
20
11
|
const ext = getExtensionFromMimeType(blob.type) || "png";
|
|
21
12
|
const filename = (data === null || data === void 0 ? void 0 : data.filename) ? `${data === null || data === void 0 ? void 0 : data.filename}.${ext}` : `${randomFileName("img", 3)}.${ext}`;
|
|
22
13
|
const file = new File([blob], filename, {
|
|
23
14
|
type: blob.type,
|
|
24
15
|
});
|
|
25
16
|
return { file, filename };
|
|
26
|
-
}
|
|
27
|
-
export const doSaveFileDesktop = (url, data) =>
|
|
17
|
+
};
|
|
18
|
+
export const doSaveFileDesktop = async (url, data) => {
|
|
28
19
|
//
|
|
29
20
|
if (!(data === null || data === void 0 ? void 0 : data.file)) {
|
|
30
|
-
const __data =
|
|
21
|
+
const __data = await doGetFile(url, data);
|
|
31
22
|
const file = __data === null || __data === void 0 ? void 0 : __data.file;
|
|
32
23
|
const filename = __data === null || __data === void 0 ? void 0 : __data.filename;
|
|
33
24
|
if (!file || !filename)
|
|
@@ -41,9 +32,9 @@ export const doSaveFileDesktop = (url, data) => __awaiter(void 0, void 0, void 0
|
|
|
41
32
|
}
|
|
42
33
|
}
|
|
43
34
|
return true;
|
|
44
|
-
}
|
|
45
|
-
export const doSaveFileMobile = (url, data) =>
|
|
46
|
-
return new Promise((resolve, _reject) =>
|
|
35
|
+
};
|
|
36
|
+
export const doSaveFileMobile = async (url, data) => {
|
|
37
|
+
return new Promise(async (resolve, _reject) => {
|
|
47
38
|
if (isAndroid()) {
|
|
48
39
|
const link = document.createElement("a");
|
|
49
40
|
link.href = url;
|
|
@@ -52,7 +43,7 @@ export const doSaveFileMobile = (url, data) => __awaiter(void 0, void 0, void 0,
|
|
|
52
43
|
link.click();
|
|
53
44
|
document.body.removeChild(link);
|
|
54
45
|
}
|
|
55
|
-
const __data =
|
|
46
|
+
const __data = await doGetFile(url, data);
|
|
56
47
|
const file = __data === null || __data === void 0 ? void 0 : __data.file;
|
|
57
48
|
const filename = __data === null || __data === void 0 ? void 0 : __data.filename;
|
|
58
49
|
if (!file || !filename)
|
|
@@ -67,29 +58,27 @@ export const doSaveFileMobile = (url, data) => __awaiter(void 0, void 0, void 0,
|
|
|
67
58
|
// alert(0);
|
|
68
59
|
// console.log(0);
|
|
69
60
|
})
|
|
70
|
-
.catch((err) =>
|
|
61
|
+
.catch(async (err) => {
|
|
71
62
|
var _a, _b, _c, _d;
|
|
72
63
|
if ((_b = (_a = `${err}`).includes) === null || _b === void 0 ? void 0 : _b.call(_a, "AbortError"))
|
|
73
64
|
return resolve(false);
|
|
74
65
|
if ((_d = (_c = err === null || err === void 0 ? void 0 : err.message) === null || _c === void 0 ? void 0 : _c.includes) === null || _d === void 0 ? void 0 : _d.call(_c, "cancellation"))
|
|
75
66
|
return resolve(false);
|
|
76
67
|
console.error(`navigator error`, err);
|
|
77
|
-
|
|
68
|
+
await doSaveFileDesktop(url, data);
|
|
78
69
|
resolve(true);
|
|
79
|
-
})
|
|
80
|
-
}));
|
|
81
|
-
});
|
|
82
|
-
export function downloadByUrl(url, data) {
|
|
83
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
-
const res = yield fetch(url);
|
|
85
|
-
const blob = yield res.blob();
|
|
86
|
-
const blobUrl = URL.createObjectURL(blob);
|
|
87
|
-
if (isMobile()) {
|
|
88
|
-
return doSaveFileMobile(blobUrl, data);
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
return doSaveFileDesktop(blobUrl, data);
|
|
92
|
-
}
|
|
70
|
+
});
|
|
93
71
|
});
|
|
72
|
+
};
|
|
73
|
+
export async function downloadByUrl(url, data) {
|
|
74
|
+
const res = await fetch(url);
|
|
75
|
+
const blob = await res.blob();
|
|
76
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
77
|
+
if (isMobile()) {
|
|
78
|
+
return doSaveFileMobile(blobUrl, data);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
return doSaveFileDesktop(blobUrl, data);
|
|
82
|
+
}
|
|
94
83
|
}
|
|
95
84
|
export default downloadByUrl;
|
|
@@ -1,27 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
image.onload = resolve;
|
|
17
|
-
image.onerror = (e) => reject(e);
|
|
18
|
-
});
|
|
19
|
-
return { width: image.naturalWidth, height: image.naturalHeight };
|
|
20
|
-
}
|
|
21
|
-
catch (error) {
|
|
22
|
-
// console.error(`getImageDimensions error`, error);
|
|
23
|
-
return { width: 0, height: 0 };
|
|
24
|
-
// throw new Error("");
|
|
25
|
-
}
|
|
26
|
-
});
|
|
1
|
+
export default async function getImageDimensions(url) {
|
|
2
|
+
try {
|
|
3
|
+
const image = new Image();
|
|
4
|
+
image.src = url;
|
|
5
|
+
await new Promise((resolve, reject) => {
|
|
6
|
+
image.onload = resolve;
|
|
7
|
+
image.onerror = (e) => reject(e);
|
|
8
|
+
});
|
|
9
|
+
return { width: image.naturalWidth, height: image.naturalHeight };
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
// console.error(`getImageDimensions error`, error);
|
|
13
|
+
return { width: 0, height: 0 };
|
|
14
|
+
// throw new Error("");
|
|
15
|
+
}
|
|
27
16
|
}
|
package/esm/images/loadImage.js
CHANGED
|
@@ -1,26 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return;
|
|
15
|
-
return yield Promise.all(list.map((url) => {
|
|
16
|
-
return new Promise((resolve, reject) => {
|
|
17
|
-
const img = new Image();
|
|
18
|
-
img.onload = () => resolve(url);
|
|
19
|
-
img.onerror = reject;
|
|
20
|
-
img.src = url;
|
|
21
|
-
return img;
|
|
22
|
-
});
|
|
23
|
-
}));
|
|
24
|
-
});
|
|
1
|
+
export async function loadImage(list) {
|
|
2
|
+
//
|
|
3
|
+
if (!(list === null || list === void 0 ? void 0 : list.length))
|
|
4
|
+
return;
|
|
5
|
+
return await Promise.all(list.map((url) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
const img = new Image();
|
|
8
|
+
img.onload = () => resolve(url);
|
|
9
|
+
img.onerror = reject;
|
|
10
|
+
img.src = url;
|
|
11
|
+
return img;
|
|
12
|
+
});
|
|
13
|
+
}));
|
|
25
14
|
}
|
|
26
15
|
export default loadImage;
|
|
@@ -1,42 +1,31 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import getUrlBypassCors from "./getUrlBypassCors.js";
|
|
11
|
-
const tryLoad = (url) =>
|
|
12
|
-
const response =
|
|
2
|
+
const tryLoad = async (url) => {
|
|
3
|
+
const response = await fetch(url, {
|
|
13
4
|
// mode: "cors",
|
|
14
5
|
cache: "force-cache",
|
|
15
6
|
});
|
|
16
7
|
if (!response.ok) {
|
|
17
8
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
18
9
|
}
|
|
19
|
-
const imageBlob =
|
|
10
|
+
const imageBlob = await (response === null || response === void 0 ? void 0 : response.blob());
|
|
20
11
|
const blobUrl = URL.createObjectURL(imageBlob);
|
|
21
12
|
return blobUrl;
|
|
22
|
-
}
|
|
23
|
-
export default function loadImageAsBlobUrl(
|
|
24
|
-
|
|
13
|
+
};
|
|
14
|
+
export default async function loadImageAsBlobUrl(imageUrl, addVerion = true) {
|
|
15
|
+
try {
|
|
16
|
+
const imageBlob = await tryLoad(imageUrl);
|
|
17
|
+
return imageBlob;
|
|
18
|
+
}
|
|
19
|
+
catch (error) { }
|
|
20
|
+
if (addVerion) {
|
|
25
21
|
try {
|
|
26
|
-
const
|
|
22
|
+
const url = getUrlBypassCors(imageUrl);
|
|
23
|
+
if (!url)
|
|
24
|
+
throw new Error("imageUrl error");
|
|
25
|
+
const imageBlob = await tryLoad(url);
|
|
27
26
|
return imageBlob;
|
|
28
27
|
}
|
|
29
28
|
catch (error) { }
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const url = getUrlBypassCors(imageUrl);
|
|
33
|
-
if (!url)
|
|
34
|
-
throw new Error("imageUrl error");
|
|
35
|
-
const imageBlob = yield tryLoad(url);
|
|
36
|
-
return imageBlob;
|
|
37
|
-
}
|
|
38
|
-
catch (error) { }
|
|
39
|
-
}
|
|
40
|
-
return imageUrl;
|
|
41
|
-
});
|
|
29
|
+
}
|
|
30
|
+
return imageUrl;
|
|
42
31
|
}
|
package/esm/images/resize.js
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import loadImage from "blueimp-load-image";
|
|
11
|
-
export const resize = (file, options) =>
|
|
2
|
+
export const resize = async (file, options) => {
|
|
12
3
|
if (!file) {
|
|
13
4
|
console.error("NO FILE AVAIABLE!");
|
|
14
5
|
return;
|
|
@@ -16,11 +7,11 @@ export const resize = (file, options) => __awaiter(void 0, void 0, void 0, funct
|
|
|
16
7
|
const maxWidth = (options === null || options === void 0 ? void 0 : options.maxWidth) || 2048;
|
|
17
8
|
const maxHeight = (options === null || options === void 0 ? void 0 : options.maxHeight) || 2048;
|
|
18
9
|
const { type } = file;
|
|
19
|
-
const data =
|
|
10
|
+
const data = await loadImage(file, Object.assign(Object.assign({
|
|
20
11
|
//
|
|
21
12
|
meta: false, canvas: true }, options), { maxWidth,
|
|
22
13
|
maxHeight }));
|
|
23
|
-
const blob =
|
|
14
|
+
const blob = await new Promise(function (resolve) {
|
|
24
15
|
try {
|
|
25
16
|
const canvas = data.image;
|
|
26
17
|
canvas.toBlob(function (blob) {
|
|
@@ -34,5 +25,5 @@ export const resize = (file, options) => __awaiter(void 0, void 0, void 0, funct
|
|
|
34
25
|
if (blob)
|
|
35
26
|
return blob;
|
|
36
27
|
return data;
|
|
37
|
-
}
|
|
28
|
+
};
|
|
38
29
|
export default resize;
|
package/esm/images/upload.js
CHANGED
|
@@ -1,28 +1,19 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { getFailedResponse } from "./../response/index.js";
|
|
11
2
|
import resize from "./resize.js";
|
|
12
3
|
import { isImage } from "./../string/url.js";
|
|
13
|
-
export const upload = (file) =>
|
|
4
|
+
export const upload = async (file) => {
|
|
14
5
|
const { name } = file;
|
|
15
6
|
if (!isImage(name)) {
|
|
16
7
|
const err = "Please Choose Image!";
|
|
17
8
|
console.error(err);
|
|
18
9
|
return getFailedResponse(err);
|
|
19
10
|
}
|
|
20
|
-
const blob = (
|
|
11
|
+
const blob = (await resize(file));
|
|
21
12
|
const url = URL.createObjectURL(blob);
|
|
22
13
|
blob.name = name;
|
|
23
14
|
return {
|
|
24
15
|
blob,
|
|
25
16
|
url,
|
|
26
17
|
};
|
|
27
|
-
}
|
|
18
|
+
};
|
|
28
19
|
export default upload;
|
|
@@ -1,26 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// Function to process a single batch
|
|
14
|
-
function processBatch(batch) {
|
|
15
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
return yield Promise.all(batch.map((item) => callback(item)));
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
// Divide the list into batches
|
|
20
|
-
for (let i = 0; i < list.length; i += batchSize) {
|
|
21
|
-
const batch = list.slice(i, i + batchSize);
|
|
22
|
-
res = [...res, ...(yield processBatch(batch))]; // Process each batch and wait for it to complete
|
|
23
|
-
}
|
|
24
|
-
return res;
|
|
25
|
-
});
|
|
1
|
+
export default async function batchProcessor(list, callback, batchSize = 10) {
|
|
2
|
+
let res = [];
|
|
3
|
+
// Function to process a single batch
|
|
4
|
+
async function processBatch(batch) {
|
|
5
|
+
return await Promise.all(batch.map((item) => callback(item)));
|
|
6
|
+
}
|
|
7
|
+
// Divide the list into batches
|
|
8
|
+
for (let i = 0; i < list.length; i += batchSize) {
|
|
9
|
+
const batch = list.slice(i, i + batchSize);
|
|
10
|
+
res = [...res, ...(await processBatch(batch))]; // Process each batch and wait for it to complete
|
|
11
|
+
}
|
|
12
|
+
return res;
|
|
26
13
|
}
|
|
@@ -1,84 +1,73 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { createReadStream } from "fs";
|
|
11
|
-
export function guessMimeType(filepath) {
|
|
12
|
-
return
|
|
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
|
-
resolve("image/webp");
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
resolve("unknown");
|
|
50
|
-
}
|
|
2
|
+
export async function guessMimeType(filepath) {
|
|
3
|
+
return new Promise((resolve, reject) => {
|
|
4
|
+
const stream = createReadStream(filepath, {
|
|
5
|
+
start: 0,
|
|
6
|
+
end: 11, // Read only first 12 bytes
|
|
7
|
+
});
|
|
8
|
+
let buffer = new Uint8Array(0);
|
|
9
|
+
stream.on("data", (chunk) => {
|
|
10
|
+
const chunkArray = new Uint8Array(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
11
|
+
// Manual concatenation
|
|
12
|
+
const newBuffer = new Uint8Array(buffer.length + chunkArray.length);
|
|
13
|
+
newBuffer.set(buffer);
|
|
14
|
+
newBuffer.set(chunkArray, buffer.length);
|
|
15
|
+
buffer = newBuffer;
|
|
16
|
+
});
|
|
17
|
+
stream.on("end", () => {
|
|
18
|
+
// Convert to hex string
|
|
19
|
+
const toHex = (arr, start, end) => {
|
|
20
|
+
return Array.from(arr.slice(start, end))
|
|
21
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
22
|
+
.join("");
|
|
23
|
+
};
|
|
24
|
+
const signature = toHex(buffer, 0, 4);
|
|
25
|
+
switch (signature) {
|
|
26
|
+
case "89504e47":
|
|
27
|
+
resolve("image/png");
|
|
28
|
+
break;
|
|
29
|
+
case "47494638":
|
|
30
|
+
resolve("image/gif");
|
|
31
|
+
break;
|
|
32
|
+
case "52494646":
|
|
33
|
+
if (buffer.length >= 12) {
|
|
34
|
+
const webpSignature = toHex(buffer, 8, 12);
|
|
35
|
+
if (webpSignature === "57454250") {
|
|
36
|
+
resolve("image/webp");
|
|
51
37
|
}
|
|
52
38
|
else {
|
|
53
39
|
resolve("unknown");
|
|
54
40
|
}
|
|
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
|
-
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
resolve("unknown");
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
case "00000018":
|
|
47
|
+
case "0000001c":
|
|
48
|
+
case "00000020":
|
|
49
|
+
resolve("video/mp4");
|
|
50
|
+
break;
|
|
51
|
+
case "66747970":
|
|
52
|
+
resolve("video/quicktime");
|
|
53
|
+
break;
|
|
54
|
+
case "1a45dfa3":
|
|
55
|
+
resolve("video/webm");
|
|
56
|
+
break;
|
|
57
|
+
case "000001ba":
|
|
58
|
+
case "000001b3":
|
|
59
|
+
resolve("video/mpeg");
|
|
60
|
+
break;
|
|
61
|
+
default:
|
|
62
|
+
if (signature.startsWith("ffd8ff")) {
|
|
63
|
+
resolve("image/jpeg");
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
resolve("application/octet-stream");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
81
69
|
});
|
|
70
|
+
stream.on("error", reject);
|
|
82
71
|
});
|
|
83
72
|
}
|
|
84
73
|
export default guessMimeType;
|