@bouko/ts 0.3.1 → 0.3.2
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/utils/converters.js
CHANGED
|
@@ -22,3 +22,23 @@ export function base64ToFile(data, opts = {}) {
|
|
|
22
22
|
const byteArray = Uint8Array.from(atob(data), c => c.charCodeAt(0));
|
|
23
23
|
return new File([byteArray], name || "file", { type });
|
|
24
24
|
}
|
|
25
|
+
export function dataToFile(data, opts) {
|
|
26
|
+
if (!data)
|
|
27
|
+
return;
|
|
28
|
+
// If the data is in "data:[mime];base64,..." format, extract it
|
|
29
|
+
const matches = data.match(/^data:(.*?);base64,(.*)$/);
|
|
30
|
+
let mime;
|
|
31
|
+
let base64Data;
|
|
32
|
+
if (matches) {
|
|
33
|
+
mime = matches[1];
|
|
34
|
+
base64Data = matches[2];
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
// If not a full data URI, just assume it's raw base64 without metadata
|
|
38
|
+
base64Data = data;
|
|
39
|
+
}
|
|
40
|
+
const byteArray = Uint8Array.from(atob(base64Data), c => c.charCodeAt(0));
|
|
41
|
+
return new File([byteArray], opts?.name || "file", {
|
|
42
|
+
type: mime || "application/octet-stream",
|
|
43
|
+
});
|
|
44
|
+
}
|