socaity
Version:
SDK for Generative AI. Build AI-powered applications with ease
1,675 lines • 145 kB
JavaScript
var ue = Object.defineProperty;
var he = (i, e, t) => e in i ? ue(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
var x = (i, e, t) => he(i, typeof e != "symbol" ? e + "" : e, t);
class K extends Error {
constructor(e = "Invalid API key format. API keys should start with 'sk_' and be 67 characters long.") {
super(e), this.name = "ApiKeyError", Error.captureStackTrace && Error.captureStackTrace(this, K);
}
}
const j = class j {
constructor(e = {}) {
x(this, "apiKey");
x(this, "baseUrl");
x(this, "pollInterval");
x(this, "maxRetries");
this.apiKey = e.apiKey, this.baseUrl = e.baseUrl || "https://api.socaity.ai/v1", this.pollInterval = e.pollInterval || 5e3, this.maxRetries = e.maxRetries || 3;
}
/**
* Get the global configuration instance
*/
static getInstance() {
return j.instance || (j.instance = new j()), j.instance;
}
/**
* Updates global configuration with new values
*/
static update(e) {
if (e.apiKey !== void 0) {
if (!e.apiKey.startsWith("sk_") && !e.apiKey.startsWith("tk_") && !e.apiKey.startsWith("ey"))
throw new K("Invalid authentication token. Use either a Socaity API key from https://www.socaity.ai or a valid JWT token.");
if (e.apiKey.startsWith("sk_") && !e.apiKey.startsWith("tk_") && e.apiKey.length !== 67)
throw new K("Invalid API key format. API keys should be 67 characters long. Get your API key from https://www.socaity.ai");
}
const t = j.getInstance();
Object.assign(t, e);
}
};
x(j, "instance");
let D = j;
var g = /* @__PURE__ */ ((i) => (i.CREATED = "CREATED", i.QUEUED = "QUEUED", i.PROCESSING = "PROCESSING", i.COMPLETED = "COMPLETED", i.FAILED = "FAILED", i.UNKNOWN = "UNKNOWN", i))(g || {}), w = /* @__PURE__ */ ((i) => (i.INITIALIZING = "INITIALIZING", i.PREPARING = "PREPARING", i.SENDING = "SENDING", i.TRACKING = "TRACKING", i.PROCESSING_RESULT = "PROCESSING_RESULT", i.COMPLETED = "COMPLETED", i.FAILED = "FAILED", i))(w || {});
class ge {
/**
* Parse response into standardized job format
* @param response - API response object or string
* @returns Standardized SocaityJob object
*/
async parse(e) {
if (e == null)
return this.createErrorJob("No response received");
if (typeof e == "string")
try {
const t = JSON.parse(e);
return this.parseObject(t);
} catch {
return {
id: "",
status: g.COMPLETED,
progress: { progress: 1, message: null },
result: e,
createdAt: /* @__PURE__ */ new Date(),
updatedAt: /* @__PURE__ */ new Date()
};
}
return this.parseObject(e);
}
/**
* Parse object responses into JobData (Socaity)
* @param response - Object to parse
*/
async parseObject(e) {
if (typeof e != "object" || e === null)
return this.createErrorJob("Invalid response format");
console.log("raw_response", e);
const t = e, s = typeof t.id == "string" ? t.id : "", n = this.parseStatus(t.status), o = this.parseProgress(t, n), a = typeof t.error == "string" ? t.error : null, r = typeof t.refresh_job_url == "string" ? t.refresh_job_url : void 0, l = this.parseDate(t.createdAt), m = this.parseDate(t.updatedAt);
return {
id: s,
status: n,
progress: o,
result: t.result,
error: a,
createdAt: l,
updatedAt: m,
refresh_job_url: r
};
}
/**
* Creates a standard error job response
* @param errorMessage - Error message to include
*/
createErrorJob(e) {
const t = /* @__PURE__ */ new Date();
return {
id: "",
status: g.FAILED,
progress: { progress: 0, message: e },
result: null,
error: e,
createdAt: t,
updatedAt: t
};
}
/**
* Parse date from various formats
* @param dateValue - Date value to parse
*/
parseDate(e) {
if (e instanceof Date)
return e;
if (typeof e == "string" || typeof e == "number") {
const t = new Date(e);
if (!isNaN(t.getTime()))
return t;
}
return /* @__PURE__ */ new Date();
}
/**
* Parse status from different API formats
* @param status - Status string to parse
*/
parseStatus(e) {
if (typeof e != "string" || !e)
return g.UNKNOWN;
const t = e.toUpperCase();
return {
COMPLETED: g.COMPLETED,
SUCCEEDED: g.COMPLETED,
FINISHED: g.COMPLETED,
CREATED: g.CREATED,
FAILED: g.FAILED,
ERROR: g.FAILED,
IN_PROGRESS: g.PROCESSING,
PROCESSING: g.PROCESSING,
RUNNING: g.PROCESSING,
BOOTING: g.PROCESSING,
QUEUED: g.QUEUED,
PENDING: g.QUEUED,
IN_QUEUE: g.QUEUED,
STARTING: g.QUEUED
}[t] || g.UNKNOWN;
}
/**
* Parse progress from different API formats
* @param response - Response object containing progress information
* @param status - Parsed job status
*/
parseProgress(e, t) {
let s = 0, n = null;
const o = e.progress;
if (typeof o == "number")
s = o;
else if (typeof o == "string")
try {
s = parseFloat(o);
} catch {
s = 0;
}
else if (o && typeof o == "object") {
const a = o;
if (typeof a.progress == "number")
s = a.progress;
else if (typeof a.progress == "string")
try {
s = parseFloat(a.progress);
} catch {
s = 0;
}
n = typeof a.message == "string" ? a.message : null;
}
return isNaN(s) && (s = 0), s = Math.max(0, Math.min(1, s)), t === g.COMPLETED && (s = 1), !n && typeof e.message == "string" && (n = e.message), {
progress: s,
message: n
};
}
}
var ye = Object.defineProperty, we = (i, e, t) => e in i ? ye(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t, b = (i, e, t) => we(i, typeof e != "symbol" ? e + "" : e, t);
const N = {
wav: "audio/wav",
mp3: "audio/mpeg",
ogg: "audio/ogg",
flac: "audio/flac",
aac: "audio/aac",
m4a: "audio/mp4",
wma: "audio/x-ms-wma",
opus: "audio/opus",
aiff: "audio/aiff",
webm: "audio/webm"
}, U = {
mp4: "video/mp4",
avi: "video/x-msvideo",
mov: "video/quicktime",
mkv: "video/x-matroska",
webm: "video/webm",
flv: "video/x-flv",
wmv: "video/x-ms-wmv",
"3gp": "video/3gpp",
ogv: "video/ogg",
m4v: "video/x-m4v"
}, S = {
jpg: "image/jpeg",
jpeg: "image/jpeg",
png: "image/png",
gif: "image/gif",
bmp: "image/bmp",
tiff: "image/tiff",
tif: "image/tiff",
jfif: "image/jpeg",
ico: "image/x-icon",
webp: "image/webp",
avif: "image/avif",
heic: "image/heic",
heif: "image/heif",
svg: "image/svg+xml"
}, v = {
obj: "model/obj",
glb: "model/gltf-binary",
gltf: "model/gltf+json",
dae: "model/vnd.collada+xml",
fbx: "application/octet-stream",
"3ds": "application/x-3ds",
ply: "application/x-ply",
stl: "model/stl",
step: "application/step",
iges: "model/iges",
x3d: "model/x3d+xml",
blend: "application/x-blender"
}, be = {
pdf: "application/pdf",
txt: "text/plain",
html: "text/html",
htm: "text/html",
json: "application/json",
js: "application/javascript",
css: "text/css",
xml: "application/xml",
csv: "text/csv",
zip: "application/zip",
"7z": "application/x-7z-compressed",
tar: "application/x-tar",
gz: "application/gzip",
npy: "application/x-npy",
npz: "application/x-npz",
pkl: "application/x-pickle",
pickle: "application/x-pickle"
}, ee = {
...N,
...U,
...S,
...v,
...be
}, Fe = {
"image/jfif": "image/jpeg",
// JFIF is a JPEG format variant
"image/jpg": "image/jpeg",
// Some systems report image/jpg instead of image/jpeg
"audio/mp3": "audio/mpeg",
// MP3 files might be reported as audio/mp3
"audio/x-mp3": "audio/mpeg",
// Alternative MP3 MIME type
"video/x-mp4": "video/mp4",
// Some systems use x-mp4 prefix
"audio/x-m4a": "audio/mp4",
// Some systems use x-m4a for AAC audio
"image/x-png": "image/png",
// Rare but possible PNG variation
"image/x-jpeg": "image/jpeg"
// Rare JPEG variation
};
function Ne(i) {
const e = [];
switch (i) {
case "audio":
e.push(...Object.values(N)), e.push(...Object.keys(N).map((t) => `.${t}`));
break;
case "video":
e.push(...Object.values(U)), e.push(...Object.keys(U).map((t) => `.${t}`));
break;
case "image":
e.push(...Object.values(S)), e.push(...Object.keys(S).map((t) => `.${t}`));
break;
case "asset_3d":
e.push(...Object.values(v)), e.push(...Object.keys(v).map((t) => `.${t}`));
break;
case "file":
default:
return ["*/*"];
}
return e;
}
function Ue(i) {
return {
audio: "Audio",
video: "Video",
image: "Images",
asset_3d: "Asset3D",
file: "Files"
}[i] || i;
}
function Me(i) {
const e = Fe[i] || i;
return Object.values(N).includes(e) ? "audio" : Object.values(U).includes(e) ? "video" : Object.values(S).includes(e) ? "image" : Object.values(v).includes(e) ? "asset_3d" : null;
}
function Q(i) {
const e = (i.startsWith(".") ? i.slice(1) : i).toLowerCase();
return e in ee ? ee[e] : null;
}
const F = typeof window > "u";
async function le(i) {
if (i == null)
return null;
if (Array.isArray(i)) {
const e = i.map((t) => le(t));
return Promise.all(e);
}
if (V(i))
try {
return await new u().fromDict(i);
} catch {
return i;
}
return i;
}
var te = {}, se = {}, I = {}, ie;
function de() {
if (ie) return I;
ie = 1, Object.defineProperty(I, "__esModule", { value: !0 }), I.fromHex = I.toHex = void 0;
const i = (s) => new Number(s).toString(16).toLowerCase(), e = (s) => `0x${i(s).length === 1 ? "0" + i(s) : i(s)}`;
I.toHex = e;
const t = (s) => new Number(s);
return I.fromHex = t, I;
}
var ne = {}, oe;
function ve() {
return oe || (oe = 1, function(i) {
Object.defineProperty(i, "__esModule", { value: !0 }), i.createComplexNode = i.createNode = i.merge = void 0;
const e = (a) => ({
typename: a.typename,
mime: a.info.mime,
extension: a.info.extension
}), t = (a, r) => a && r.length === 0, s = (a, r) => {
if (a.bytes.length === 0)
return r;
const [l, ...m] = a.bytes, c = r.bytes[l];
if (t(c, m)) {
const d = r.bytes[l];
return r.bytes[l] = {
...d,
matches: [
...d.matches ?? [],
e(a)
]
}, r;
}
return r.bytes[l] ? r.bytes[l] = i.merge(i.createNode(a.typename, m, a.info), r.bytes[l]) : r.bytes[l] = i.createComplexNode(a.typename, m, a.info), r;
};
i.merge = s;
const n = (a, r, l) => ({ typename: a, bytes: r, info: l || {} });
i.createNode = n;
const o = (a, r, l) => {
let m = {
bytes: {},
matches: void 0
};
const [c, ...d] = r;
return r.length === 0 ? {
matches: [
e({
typename: a,
info: l ? { extension: l.extension, mime: l.mime } : {}
})
],
bytes: {}
} : (m.bytes[c] = i.createComplexNode(a, d, l), m);
};
i.createComplexNode = o;
}(ne)), ne;
}
var ae;
function _e() {
return ae || (ae = 1, function(i) {
Object.defineProperty(i, "__esModule", { value: !0 }), i.createTree = i.add = void 0;
const e = de(), t = ve();
let s = {
noOffset: null,
offset: {}
};
const n = (a, r, l, m) => {
if (m) {
const c = s.offset[e.toHex(m)];
if (!c)
s.offset[e.toHex(m)] = t.createComplexNode(a, r.map((d) => d.toLowerCase()), l);
else {
const d = t.merge(t.createNode(a, r.map((p) => p.toLowerCase()), l), { ...c });
s.offset[e.toHex(m)] = d;
}
} else
s.noOffset === null ? s.noOffset = t.createComplexNode(a, r.map((c) => c.toLowerCase()), l) : s.noOffset = t.merge(t.createNode(a, r.map((c) => c.toLowerCase()), l), s.noOffset);
};
i.add = n, i.add("gif", ["0x47", "0x49", "0x46", "0x38", "0x37", "0x61"], {
mime: "image/gif",
extension: "gif"
}), i.add("gif", ["0x47", "0x49", "0x46", "0x38", "0x39", "0x61"], {
mime: "image/gif",
extension: "gif"
}), i.add("jpg", ["0xFF", "0xD8", "0xFF"], {
mime: "image/jpeg",
extension: "jpeg"
}), i.add("webp", [
"0x52",
"0x49",
"0x46",
"0x46",
"?",
"?",
"?",
"?",
"0x57",
"0x45",
"0x42",
"0x50"
], { mime: "image/webp", extension: "webp" }), i.add("heif", ["0x66", "0x74", "0x79", "0x70", "0x6D", "0x69", "0x66", "0x31"], { mime: "image/heif", extension: "heif" }, 4), i.add("heif", ["0x66", "0x74", "0x79", "0x70", "0x68", "0x65", "0x69", "0x63"], { mime: "image/heif", extension: "heic" }, 4), i.add("rpm", ["0xed", "0xab", "0xee", "0xdb"]), i.add("bin", ["0x53", "0x50", "0x30", "0x31"], {
mime: "application/octet-stream",
extension: "bin"
}), i.add("pic", ["0x00"]), i.add("pif", ["0x00"]), i.add("sea", ["0x00"]), i.add("ytr", ["0x00"]), i.add("mp4", ["0x66", "0x74", "0x79", "0x70"], { mime: "video/mp4", extension: "mp4" }, 4), i.add("ttf", ["0x00", "0x01", "0x00", "0x00", "0x00"], {
mime: "font/ttf",
extension: "ttf"
}), i.add("otf", ["0x4F", "0x54", "0x54", "0x4F"], {
mime: "font/otf",
extension: "otf"
}), i.add("eot", ["0x50", "0x4C"], {
mime: "application/vnd.ms-fontobject",
extension: "eot"
}), i.add("woff", ["0x77", "0x4F", "0x46", "0x46"], {
mime: "font/woff",
extension: "woff"
}), i.add("woff2", ["0x77", "0x4F", "0x46", "0x32"], {
mime: "font/woff2",
extension: "woff2"
}), i.add("pdb", [
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00"
]), i.add("dba", ["0xBE", "0xBA", "0xFE", "0xCA"]), i.add("dba2", ["0x00", "0x01", "0x42", "0x44"]), i.add("tda", ["0x00", "0x01", "0x44", "0x54"]), i.add("tda2", ["0x00", "0x01", "0x00", "0x00"]), i.add("ico", ["0x00", "0x00", "0x01", "0x00"], {
mime: "image/x-icon",
extension: "ico"
}), i.add("3gp", ["0x66", "0x74", "0x79", "0x70", "0x33", "0x67"]), i.add("z", ["0x1F", "0x9D"]), i.add("tar.z", ["0x1F", "0xA0"]), i.add("bac", [
"0x42",
"0x41",
"0x43",
"0x4B",
"0x4D",
"0x49",
"0x4B",
"0x45",
"0x44",
"0x49",
"0x53",
"0x4B"
]), i.add("bz2", ["0x42", "0x5A", "0x68"], {
mime: "application/x-bzip2",
extension: "bz2"
}), i.add("tif", ["0x49", "0x49", "0x2A", "0x00"], {
mime: "image/tiff",
extension: "tif"
}), i.add("tiff", ["0x4D", "0x4D", "0x00", "0x2A"], {
mime: "image/tiff",
extension: "tiff"
}), i.add("cr2", [
"0x49",
"0x49",
"0x2A",
"0x00",
"0x10",
"0x00",
"0x00",
"0x00",
"0x43",
"0x52"
]), i.add("cin", ["0x80", "0x2A", "0x5F", "0xD7"]), i.add("cin1", ["0x52", "0x4E", "0x43", "0x01"]), i.add("cin2", ["0x52", "0x4E", "0x43", "0x02"]), i.add("dpx", ["0x53", "0x44", "0x50", "0x58"]), i.add("dpx2", ["0x58", "0x50", "0x44", "0x53"]), i.add("exr", ["0x76", "0x2F", "0x31", "0x01"]), i.add("bpg", ["0x42", "0x50", "0x47", "0xFB"]), i.add("ilbm", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x49",
"0x4C",
"0x42",
"0x4D"
]), i.add("8svx", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x38",
"0x53",
"0x56",
"0x58"
]), i.add("acbm", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x41",
"0x43",
"0x42",
"0x4D"
]), i.add("anbm", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x41",
"0x4E",
"0x42",
"0x4D"
]), i.add("anim", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x41",
"0x4E",
"0x49",
"0x4D"
]), i.add("faxx", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x46",
"0x41",
"0x58",
"0x58"
]), i.add("ftxt", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x46",
"0x54",
"0x58",
"0x54"
]), i.add("smus", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x53",
"0x4D",
"0x55",
"0x53"
]), i.add("cmus", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x43",
"0x4D",
"0x55",
"0x53"
]), i.add("yuvn", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x59",
"0x55",
"0x56",
"0x4E"
]), i.add("iff", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x46",
"0x41",
"0x4E",
"0x54"
]), i.add("aiff", [
"0x46",
"0x4F",
"0x52",
"0x4D",
"?",
"?",
"?",
"?",
"0x41",
"0x49",
"0x46",
"0x46"
], { mime: "audio/x-aiff", extension: "aiff" }), i.add("idx", ["0x49", "0x4E", "0x44", "0x58"]), i.add("lz", ["0x4C", "0x5A", "0x49", "0x50"]), i.add("exe", ["0x4D", "0x5A"]), i.add("zip", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/zip",
extension: "zip"
}), i.add("zip", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/zip",
extension: "zip"
}), i.add("zip", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/zip",
extension: "zip"
}), i.add("jar", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/java-archive",
extension: "jar"
}), i.add("jar", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/java-archive",
extension: "jar"
}), i.add("jar", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/java-archive",
extension: "jar"
}), i.add("odt", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/vnd.oasis.opendocument.text",
extension: "odt"
}), i.add("odt", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/vnd.oasis.opendocument.text",
extension: "odt"
}), i.add("odt", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/vnd.oasis.opendocument.text",
extension: "odt"
}), i.add("ods", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/vnd.oasis.opendocument.spreadsheet",
extension: "ods"
}), i.add("ods", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/vnd.oasis.opendocument.spreadsheet",
extension: "ods"
}), i.add("ods", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/vnd.oasis.opendocument.spreadsheet",
extension: "ods"
}), i.add("odp", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/vnd.oasis.opendocument.presentation",
extension: "odp"
}), i.add("odp", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/vnd.oasis.opendocument.presentation",
extension: "odp"
}), i.add("odp", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/vnd.oasis.opendocument.presentation",
extension: "odp"
}), i.add("docx", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
extension: "docx"
}), i.add("docx", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
extension: "docx"
}), i.add("docx", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
extension: "docx"
}), i.add("xlsx", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
extension: "xlsx"
}), i.add("xlsx", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
extension: "xlsx"
}), i.add("xlsx", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
extension: "xlsx"
}), i.add("pptx", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
extension: "pptx"
}), i.add("pptx", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
extension: "pptx"
}), i.add("pptx", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
extension: "pptx"
}), i.add("vsdx", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/vnd.ms-visio.drawing",
extension: "vsdx"
}), i.add("vsdx", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/vnd.ms-visio.drawing",
extension: "vsdx"
}), i.add("vsdx", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/vnd.ms-visio.drawing",
extension: "vsdx"
}), i.add("apk", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/vnd.android.package-archive",
extension: "apk"
}), i.add("apk", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/vnd.android.package-archive",
extension: "apk"
}), i.add("apk", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/vnd.android.package-archive",
extension: "apk"
}), i.add("aar", ["0x50", "0x4B", "0x03", "0x04"], {
mime: "application/vnd.android.package-archive",
extension: "aar"
}), i.add("aar", ["0x50", "0x4B", "0x05", "0x06"], {
mime: "application/vnd.android.package-archive",
extension: "aar"
}), i.add("aar", ["0x50", "0x4B", "0x07", "0x08"], {
mime: "application/vnd.android.package-archive",
extension: "aar"
}), i.add("rar", ["0x52", "0x61", "0x72", "0x21", "0x1A", "0x07", "0x00"], {
mime: "application/vnd.rar",
extension: "rar"
}), i.add("rar", ["0x52", "0x61", "0x72", "0x21", "0x1A", "0x07", "0x01", "0x00"], {
mime: "application/vnd.rar",
extension: "rar"
}), i.add("rar", ["0x7F", "0x45", "0x4C", "0x46"], {
mime: "application/vnd.rar",
extension: "rar"
}), i.add("png", ["0x89", "0x50", "0x4E", "0x47", "0x0D", "0x0A", "0x1A", "0x0A"], {
mime: "image/png",
extension: "png"
}), i.add("apng", ["0x89", "0x50", "0x4E", "0x47", "0x0D", "0x0A", "0x1A", "0x0A"], {
mime: "image/apng",
extension: "apng"
}), i.add("class", ["0xCA", "0xFE", "0xBA", "0xBE"]), i.add("class", ["0xEF", "0xBB", "0xBF"]), i.add("class", ["0xFE", "0xed", "0xFA", "0xCE"], void 0, 4096), i.add("class", ["0xFE", "0xed", "0xFA", "0xCF"], void 0, 4096), i.add("class", ["0xCE", "0xFA", "0xed", "0xFE"]), i.add("class", ["0xCF", "0xFA", "0xed", "0xFE"]), i.add("class", ["0xFF", "0xFE"]), i.add("class", ["0xFF", "0xFE"]), i.add("class", ["0xFF", "0xFE", "0x00", "0x00"]), i.add("ps", ["0x25", "0x21", "0x50", "0x53"], {
mime: "application/postscript",
extension: ".ps"
}), i.add("pdf", ["0x25", "0x50", "0x44", "0x46"], {
mime: "application/pdf",
extension: "pdf"
}), i.add("asf", [
"0x30",
"0x26",
"0xB2",
"0x75",
"0x8E",
"0x66",
"0xCF",
"0x11",
"0xA6",
"0xD9",
"0x00",
"0xAA",
"0x00",
"0x62",
"0xCE",
"0x6C"
]), i.add("wma", [
"0x30",
"0x26",
"0xB2",
"0x75",
"0x8E",
"0x66",
"0xCF",
"0x11",
"0xA6",
"0xD9",
"0x00",
"0xAA",
"0x00",
"0x62",
"0xCE",
"0x6C"
]), i.add("wmv", [
"0x30",
"0x26",
"0xB2",
"0x75",
"0x8E",
"0x66",
"0xCF",
"0x11",
"0xA6",
"0xD9",
"0x00",
"0xAA",
"0x00",
"0x62",
"0xCE",
"0x6C"
]), i.add("deploymentimage", [
"0x24",
"0x53",
"0x44",
"0x49",
"0x30",
"0x30",
"0x30",
"0x31"
]), i.add("ogv", [
"0x4F",
"0x67",
"0x67",
"0x53",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"0x80",
"0x74",
"0x68",
"0x65",
"0x6F",
"0x72",
"0x61"
], {
mime: "video/ogg",
extension: "ogv"
}), i.add("ogm", [
"0x4F",
"0x67",
"0x67",
"0x53",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"0x01",
"0x76",
"0x69",
"0x64",
"0x65",
"0x6F",
"0x00"
], {
mime: "video/ogg",
extension: "ogm"
}), i.add("oga", [
"0x4F",
"0x67",
"0x67",
"0x53",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"0x7F",
"0x46",
"0x4C",
"0x41",
"0x43"
], {
mime: "audio/ogg",
extension: "oga"
}), i.add("spx", [
"0x4F",
"0x67",
"0x67",
"0x53",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"0x53",
"0x70",
"0x65",
"0x65",
"0x78",
"0x20",
"0x20"
], {
mime: "audio/ogg",
extension: "spx"
}), i.add("ogg", [
"0x4F",
"0x67",
"0x67",
"0x53",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"?",
"0x01",
"0x76",
"0x6F",
"0x72",
"0x62",
"0x69",
"0x73"
], {
mime: "audio/ogg",
extension: "ogg"
}), i.add("ogx", ["0x4F", "0x67", "0x67", "0x53"], {
mime: "application/ogg",
extension: "ogx"
}), i.add("psd", ["0x38", "0x42", "0x50", "0x53"], {
mime: "application/x-photoshop",
extension: "psd"
}), i.add("clip", ["0x43", "0x53", "0x46", "0x43", "0x48", "0x55", "0x4e", "0x4b"]), i.add("wav", [
"0x52",
"0x49",
"0x46",
"0x46",
"?",
"?",
"?",
"?",
"0x57",
"0x41",
"0x56",
"0x45"
], { mime: "audio/x-wav", extension: "wav" }), i.add("avi", [
"0x52",
"0x49",
"0x46",
"0x46",
"?",
"?",
"?",
"?",
"0x41",
"0x56",
"0x49",
"0x20"
], { mime: "video/x-msvideo", extension: "avi" }), i.add("mp3", ["0xFF", "0xFB"], { mime: "audio/mpeg", extension: "mp3" }), i.add("mp3", ["0xFF", "0xF3"], { mime: "audio/mpeg", extension: "mp3" }), i.add("mp3", ["0xFF", "0xF2"], { mime: "audio/mpeg", extension: "mp3" }), i.add("mp3", ["0x49", "0x44", "0x33"], { mime: "audio/mpeg", extension: "mp3" }), i.add("bmp", ["0x42", "0x4D"], { mime: "image/bmp", extension: "bmp" }), i.add("iso", ["0x43", "0x44", "0x30", "0x30", "0x31"]), i.add("flac", ["0x66", "0x4C", "0x61", "0x43"]), i.add("mid", ["0x4D", "0x54", "0x68", "0x64"], {
mime: "audio/midi",
extension: "mid"
}), i.add("midi", ["0x4D", "0x54", "0x68", "0x64"], {
mime: "audio/midi",
extension: "midi"
}), i.add("doc", ["0xD0", "0xCF", "0x11", "0xE0", "0xA1", "0xB1", "0x1A", "0xE1"], {
mime: "application/msword",
extension: "doc"
}), i.add("xls", ["0xD0", "0xCF", "0x11", "0xE0", "0xA1", "0xB1", "0x1A", "0xE1"], {
mime: "application/vnd.ms-excel",
extension: "xls"
}), i.add("ppt", ["0xD0", "0xCF", "0x11", "0xE0", "0xA1", "0xB1", "0x1A", "0xE1"], {
mime: "application/vnd.ms-powerpoint",
extension: "ppt"
}), i.add("msg", ["0xD0", "0xCF", "0x11", "0xE0", "0xA1", "0xB1", "0x1A", "0xE1"]), i.add("dex", ["0x64", "0x65", "0x78", "0x0A", "0x30", "0x33", "0x35", "0x00"]), i.add("vmdk", ["0x4B", "0x44", "0x4D"]), i.add("crx", ["0x43", "0x72", "0x32", "0x34"]), i.add("fh8", ["0x41", "0x47", "0x44", "0x33"]), i.add("cwk", [
"0x05",
"0x07",
"0x00",
"0x00",
"0x42",
"0x4F",
"0x42",
"0x4F",
"0x05",
"0x07",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x01"
]), i.add("cwk", [
"0x06",
"0x07",
"0xE1",
"0x00",
"0x42",
"0x4F",
"0x42",
"0x4F",
"0x06",
"0x07",
"0xE1",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x00",
"0x01"
]), i.add("toast", ["0x45", "0x52", "0x02", "0x00", "0x00", "0x00"]), i.add("toast", ["0x8B", "0x45", "0x52", "0x02", "0x00", "0x00", "0x00"]), i.add("dmg", ["0x78", "0x01", "0x73", "0x0D", "0x62", "0x62", "0x60"]), i.add("xar", ["0x78", "0x61", "0x72", "0x21"]), i.add("dat", ["0x50", "0x4D", "0x4F", "0x43", "0x43", "0x4D", "0x4F", "0x43"]), i.add("nes", ["0x4E", "0x45", "0x53", "0x1A"]), i.add("tar", ["0x75", "0x73", "0x74", "0x61", "0x72", "0x00", "0x30", "0x30"], {
// As per Mozilla documentation available at:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
// or wikipedia page:
// https://en.wikipedia.org/wiki/List_of_archive_formats
mime: "application/x-tar",
extension: "tar"
}, 257), i.add("tar", ["0x75", "0x73", "0x74", "0x61", "0x72", "0x20", "0x20", "0x00"], {
mime: "application/x-tar",
extension: "tar"
}, 257), i.add("tox", ["0x74", "0x6F", "0x78", "0x33"]), i.add("mlv", ["0x4D", "0x4C", "0x56", "0x49"]), i.add("windowsupdate", [
"0x44",
"0x43",
"0x4D",
"0x01",
"0x50",
"0x41",
"0x33",
"0x30"
]), i.add("7z", ["0x37", "0x7A", "0xBC", "0xAF", "0x27", "0x1C"], {
mime: "application/x-7z-compressed",
extension: "7z"
}), i.add("gz", ["0x1F", "0x8B"], { mime: "application/gzip", extension: "gz" }), i.add("tar.gz", ["0x1F", "0x8B"], {
mime: "application/gzip",
extension: "tar.gz"
}), i.add("xz", ["0xFD", "0x37", "0x7A", "0x58", "0x5A", "0x00", "0x00"], {
mime: "application/gzip",
extension: "xz"
}), i.add("tar.xz", ["0xFD", "0x37", "0x7A", "0x58", "0x5A", "0x00", "0x00"], {
mime: "application/gzip",
extension: "tar.xz"
}), i.add("lz2", ["0x04", "0x22", "0x4D", "0x18"]), i.add("cab", ["0x4D", "0x53", "0x43", "0x46"]), i.add("mkv", ["0x1A", "0x45", "0xDF", "0xA3"], {
mime: "video/x-matroska",
extension: "mkv"
}), i.add("mka", ["0x1A", "0x45", "0xDF", "0xA3"], {
mime: "audio/x-matroska",
extension: "mka"
}), i.add("mks", ["0x1A", "0x45", "0xDF", "0xA3"], {
mime: "video/x-matroska",
extension: "mks"
}), i.add("mk3d", ["0x1A", "0x45", "0xDF", "0xA3"]), i.add("webm", ["0x1A", "0x45", "0xDF", "0xA3"], {
mime: "audio/webm",
extension: "webm"
}), i.add("dcm", ["0x44", "0x49", "0x43", "0x4D"], void 0, 128), i.add("xml", ["0x3C", "0x3f", "0x78", "0x6d", "0x6C", "0x20"], {
mime: "application/xml",
extension: "xml"
}), i.add("wasm", ["0x00", "0x61", "0x73", "0x6d"], {
mime: "application/wasm",
extension: "wasm"
}), i.add("lep", ["0xCF", "0x84", "0x01"]), i.add("swf", ["0x43", "0x57", "0x53"], {
mime: "application/x-shockwave-flash",
extension: "swf"
}), i.add("swf", ["0x46", "0x57", "0x53"], {
mime: "application/x-shockwave-flash",
extension: "swf"
}), i.add("deb", ["0x21", "0x3C", "0x61", "0x72", "0x63", "0x68", "0x3E"]), i.add("rtf", ["0x7B", "0x5C", "0x72", "0x74", "0x66", "0x31"], {
mime: "application/rtf",
extension: "rtf"
}), i.add("m2p", ["0x00", "0x00", "0x01", "0xBA"]), i.add("vob", ["0x00", "0x00", "0x01", "0xBA"]), i.add("mpg", ["0x00", "0x00", "0x01", "0xBA"], {
mime: "video/mpeg",
extension: "mpg"
}), i.add("mpeg", ["0x00", "0x00", "0x01", "0xBA"], {
mime: "video/mpeg",
extension: "mpeg"
}), i.add("mpeg", ["0x47"], { mime: "video/mpeg", extension: "mpeg" }), i.add("mpeg", ["0x00", "0x00", "0x01", "0xB3"], {
mime: "video/mpeg",
extension: "mpeg"
}), i.add("mov", ["0x66", "0x72", "0x65", "0x65"], {
mime: "video/quicktime",
extension: "mov"
}, 4), i.add("mov", ["0x6D", "0x64", "0x61", "0x74"], {
mime: "video/quicktime",
extension: "mov"
}, 4), i.add("mov", ["0x6D", "0x6F", "0x6F", "0x76"], {
mime: "video/quicktime",
extension: "mov"
}, 4), i.add("mov", ["0x77", "0x69", "0x64", "0x65"], {
mime: "video/quicktime",
extension: "mov"
}, 4), i.add("mov", ["0x66", "0x74", "0x79", "0x70", "0x71", "0x74"], {
mime: "video/quicktime",
extension: "mov"
}, 4), i.add("hl2demo", ["0x48", "0x4C", "0x32", "0x44", "0x45", "0x4D", "0x4F"]), i.add("txt", ["0xEF", "0xBB", "0xBF"], {
mime: "text/plain; charset=UTF-8",
extension: "txt"
}), i.add("txt", ["0xFF", "0xFE"], {
mime: "text/plain; charset=UTF-16LE",
extension: "txt"
}), i.add("txt", ["0xFE", "0xFF"], {
mime: "text/plain; charset=UTF-16BE",
extension: "txt"
}), i.add("txt", ["0xFF", "0xFE", "0x00", "0x00"], {
mime: "text/plain; charset=UTF-32LE",
extension: "txt"
}), i.add("txt", ["0x00", "0x00", "0xFE", "0xFF"], {
mime: "text/plain; charset=UTF-32BE",
extension: "txt"
}), i.add("SubRip", ["0x31", "0x0D", "0x0A", "0x30", "0x30", "0x3A"], {
mime: "application/x-subrip",
extension: "srt"
}), i.add("WebVTT", [
"0xEF",
"0xBB",
"0xBF",
"0x57",
"0x45",
"0x42",
"0x56",
"0x54",
"0x54",
"0x0A"
], {
mime: "text/vtt",
extension: "vtt"
}), i.add("WebVTT", [
"0xEF",
"0xBB",
"0xBF",
"0x57",
"0x45",
"0x42",
"0x56",
"0x54",
"0x54",
"0x0D"
], {
mime: "text/vtt",
extension: "vtt"
}), i.add("WebVTT", [
"0xEF",
"0xBB",
"0xBF",
"0x57",
"0x45",
"0x42",
"0x56",
"0x54",
"0x54",
"0x20"
], {
mime: "text/vtt",
extension: "vtt"
}), i.add("WebVTT", [
"0xEF",
"0xBB",
"0xBF",
"0x57",
"0x45",
"0x42",
"0x56",
"0x54",
"0x54",
"0x09"
], {
mime: "text/vtt",
extension: "vtt"
}), i.add("WebVTT", ["0x57", "0x45", "0x42", "0x56", "0x54", "0x54", "0x0A"], {
mime: "text/vtt",
extension: "vtt"
}), i.add("WebVTT", ["0x57", "0x45", "0x42", "0x56", "0x54", "0x54", "0x0D"], {
mime: "text/vtt",
extension: "vtt"
}), i.add("WebVTT", ["0x57", "0x45", "0x42", "0x56", "0x54", "0x54", "0x20"], {
mime: "text/vtt",
extension: "vtt"
}), i.add("WebVTT", ["0x57", "0x45", "0x42", "0x56", "0x54", "0x54", "0x09"], {
mime: "text/vtt",
extension: "vtt"
}), i.add("Json", ["0x7B"], {
mime: "application/json",
extension: ".json"
}), i.add("Json", ["0x5B"], {
mime: "application/json",
extension: ".json"
}), i.add("ELF", ["0x7F", "0x45", "0x4C", "0x46"], {
mime: "application/x-executable",
extension: ".elf"
}), i.add("Mach-O", ["0xFE", "0xED", "0xFA", "0xC"], {
mime: "application/x-mach-binary",
extension: ".o"
}), i.add("Mach-O", ["0xFE", "0xED", "0xFA", "0xCF"], {
mime: "application/x-executable",
extension: "elf"
}), i.add("EML", ["0x52", "0x65", "0x63", "0x65", "0x69", "0x76", "0x65", "0x64", "0x3A"], {
mime: "message/rfc822",
extension: ".eml"
}), i.add("SVG", ["0x3c", "0x73", "0x76", "0x67"], {
mime: "image/svg+xml",
extension: "svg"
}), i.add("avif", ["0x66", "0x74", "0x79", "0x70", "0x61", "0x76", "0x69", "0x66"], {
mime: "image/avif",
extension: "avif"
}, 4);
const o = () => s;
i.createTree = o, i.default = () => s;
}(se)), se;
}
var re;
function Ee() {
return re || (re = 1, function(i) {
Object.defineProperty(i, "__esModule", { value: !0 }), i.register = i.filetypeextension = i.filetypemime = i.filetypename = i.filetypeinfo = void 0;
const e = _e(), t = de(), s = e.createTree(), n = (c) => {
let d = s;
for (const p of Object.keys(d.offset)) {
const f = t.fromHex(p);
if (f >= c.length)
continue;
const h = s.offset[p], y = o(f, c, h);
if (y.length > 0)
return y;
}
return d.noOffset === null ? [] : o(0, c, d.noOffset);
};
i.filetypeinfo = n;
const o = (c, d, p) => {
let f = p, h = [];
for (; ; ) {
const y = t.toHex(d[c]);
if (f.bytes["?"] && !f.bytes[y] ? f = f.bytes["?"] : f = f.bytes[y], !f)
return h;
f && f.matches && (h = f.matches.slice(0)), c += 1;
}
};
i.default = i.filetypeinfo;
const a = (c) => i.filetypeinfo(c).map((d) => d.typename);
i.filetypename = a;
const r = (c) => i.filetypeinfo(c).map((d) => d.mime ? d.mime : null).filter((d) => d !== null);
i.filetypemime = r;
const l = (c) => i.filetypeinfo(c).map((d) => d.extension ? d.extension : null).filter((d) => d !== null);
i.filetypeextension = l;
const m = (c, d, p, f) => {
e.add(c, d, p, f);
};
i.register = m;
}(te)), te;
}
var Ae = Ee();
class $ {
/**
* Detect MIME type from file content (ArrayBuffer).
* @param content - File content
* @returns Detected MIME type or `null` if unrecognized
*/
static detectMime(e) {
if (!e || e.byteLength < 4) return null;
const t = Ae.filetypeinfo(new Uint8Array(e));
return t.length > 0 && t[0].mime ? t[0].mime : this.detectSvg(e) || this.detect3DModel(e);
}
/**
* Detect image MIME type specifically.
* @param content - File content
*/
static detectImageFormat(e) {
const t = this.detectMime(e);
return t != null && t.startsWith("image/") ? t : null;
}
/**
* Detect video MIME type specifically.
* @param content - File content
*/
static detectVideoFormat(e) {
const t = this.detectMime(e);
return t != null && t.startsWith("video/") ? t : null;
}
/**
* Detect audio MIME type specifically.
* @param content - File content
*/
static detectAudioFormat(e) {
const t = this.detectMime(e);
return t != null && t.startsWith("audio/") ? t : null;
}
/**
* Custom fallback: detect 3D model formats by header patterns or keywords.
*/
static detect3DModel(e) {
const t = new Uint8Array(e).slice(0, 128), s = new TextDecoder().decode(t);
return t[0] === 103 && t[1] === 108 && t[2] === 84 && t[3] === 70 ? v.glb : s.startsWith("solid ") ? v.stl : s.includes('"asset"') || s.includes('"scene"') ? v.gltf : s.includes("Kaydara FBX") ? v.fbx : s.startsWith("ply") ? v.ply : s.includes("<COLLADA") ? v.dae : t[0] === 77 && t[1] === 77 ? v["3ds"] : s.includes("v ") || s.includes("f ") ? v.obj : null;
}
/**
* Detect 3D model format name specifically (human-readable name).
* @param content - File content
*/
static detect3DModelFormatName(e) {
const t = this.detect3DModel(e);
return t ? {
"model/obj": "Wavefront OBJ",
"model/gltf-binary": "GL Transmission Format Binary",
"model/gltf+json": "GL Transmission Format",
"application/x-fbx": "Autodesk FBX",
"model/stl": "STereoLithography",
"model/ply": "Polygon File Format",
"model/vnd.collada+xml": "COLLADA",
"application/x-3ds": "3D Studio",
"model/step": "STEP",
"model/iges": "IGES",
"model/x3d+xml": "X3D",
"application/x-blender": "Blender"
}[t] || "Unknown 3D Format" : null;
}
/**
* Detect inline SVG by checking for XML <svg> tag in first 200 bytes.
*/
static detectSvg(e) {
return new TextDecoder().decode(e.slice(0, 200)).includes("<svg") ? S.svg : null;
}
}
async function Ce(i) {
var e;
if (i instanceof u)
return i.getContentType();
if (V(i))
return i.content_type;
if (ce(i))
return i.type;
if (typeof i == "string" && J(i)) {
const t = (e = new URL(i).pathname.split(".").pop()) == null ? void 0 : e.toLowerCase();
if (t) {
const s = Q(t);
if (s) return s;
}
try {
const s = await fetch(i, {
method: "HEAD",
headers: { "User-Agent": "MediaFile/1.0.0" }
});
if (s.ok) {
const n = s.headers.get("content-type");
if (n) return n;
}
} catch {
}
}
if (typeof i == "string" && i.startsWith("data:")) {
const t = i.match(/^data:([^;,]+)/);
if (t && t[1])
return t[1];
}
if (typeof i == "string" && typeof window > "u")
try {
const t = await import("fs/promises"), s = await import("path");
await t.access(i);
const n = s.extname(i).slice(1).toLowerCase();
if (n) {
const o = Q(n);
if (o) return o;
}
} catch {
}
if (typeof Blob < "u" && i instanceof Blob) {
if (!i.type || i.type === "application/octet-stream")
try {
const t = await i.slice(0, 1024).arrayBuffer(), s = $.detectMime(t);
if (s) return s;
} catch {
}
return i.type;
}
if (i instanceof ArrayBuffer || i instanceof Uint8Array) {
let t;
return i instanceof Uint8Array ? typeof SharedArrayBuffer < "u" && i.buffer instanceof SharedArrayBuffer ? (t = new ArrayBuffer(i.byteLength), new Uint8Array(t).set(i)) : t = i.buffer.slice(i.byteOffset, i.byteOffset + i.byteLength) : t = i, $.detectMime(t);
}
return null;
}
function J(i) {
try {
const e = new URL(i);
return e.protocol === "http:" || e.protocol === "https:";
} catch {
return !1;
}
}
function ke(i) {
if (typeof i != "string" || !J(i)) return null;
try {
const e = ((new URL(i).pathname || "").split("/").pop() || "").split("?")[0].split("#")[0].split("."), t = e.length > 1 ? e.pop().toLowerCase() : "";
return t ? Q(t) : null;
} catch {
return null;
}
}
function Be(i) {
return i.startsWith("data:") || Pe(i);
}
function Pe(i) {
return /^[A-Za-z0-9+/=]+$/.test(i) && i.length % 4 === 0;
}
async function Y(i) {
if (!F)
return !1;
try {
return (await (await import("fs/promises")).stat(i)).isFile();
} catch {
return !1;
}
}
function ce(i) {
return i && typeof i == "object" && typeof i.data == "string" && typeof i.type == "string" && typeof i.name == "string";
}
function V(i) {
return i && typeof i == "object" && "file_name" in i && "content_type" in i && "content" in i;
}
class P {
/**
* Create a MediaFile or specialized subclass from any supported data type.
* Automatically detects data type and instantiates the appropriate class.
*
* @param data - Data to load (file path, URL, base64 string, etc.)
* @returns Promise resolving to a MediaFile instance or specialized subclass
*/
static async create(e) {
if (e == null)
throw new Error("Cannot create MediaFile from null or undefined data");
const t = await Ce(e);
let s = B.default;
return t && t.startsWith("image/") ? s = B.image : t && t.startsWith("audio/") ? s = B.audio : t && t.startsWith("video/") ? s = B.video : t && (t.startsWith("model/") || t === "application/x-blender") && (s = B.asset_3d), new s().fromAny(e);
}
}
class u {
/**
* Creates a new MediaFile instance.
*
* @param file_name - Default filename to use
* @param content_type - Default content type to use
*/
constructor(e = "file", t = "application/octet-stream") {
b(this, "content_type"), b(this, "file_name"), b(this, "_content", null), this.content_type = t, this.file_name = e;
}
/**
* Factory method to create a MediaFile from any supported data type.
* This is kept for backward compatibility. New code should use MediaFileFactory.create().
*
* @param data - Data to load (file path, URL, base64 string, etc.)
* @returns Promise resolving to a MediaFile instance or specialized subclass
* @deprecated Use MediaFileFactory.create() instead
*/
static async create(e) {
return P.create(e);
}
/**
* Load a file from any supported data type.
*
* @param data - Data to load (file path, URL, base64 string, etc.)
* @param websafe - Prevents loading from file paths and malformatted base64 strings.
* @returns Promise resolving to a MediaFile instance or null
*/
async fromAny(e) {
if (e == null)
throw new Error("Cannot create MediaFile from null or undefined data");
if (e instanceof u)
return e;
if (ce(e)) {
if (this.file_name = e.name || "file", this.content_type = e.type, typeof e.data == "string")
return this.fromBase64(e.data);
throw new Error("Invalid data format in FileReader object");
}
if (F && this._isBuffer(e))
return this.fromBytes(e);
if (typeof File < "u" && e instanceof File) {
this.file_name = e.name, this.content_type = e.type || "application/octet-stream";
const t = await e.arrayBuffer();
return this.fromBytes(t);
}
if (typeof Blob < "u" && e instanceof Blob) {
const t = await e.arrayBuffer();
return this.fromBytes(t);
}
if (e instanceof ArrayBuffer || e instanceof Uint8Array)
return this.fromBytes(e);
if (typeof e == "string") {
if (J(e))
return await this.fromUrl(e);
if (Be(e))
return this.fromBase64(e);
if (await Y(e))
return await this.fromFile(e);
throw typeof e == "string" ? new Error("Invalid data type for MediaFile " + e) : new Error("Invalid data type for MediaFile");
}
return V(e) ? await this.fromDict(e) : this;
}
/**
* Load file from a file path (Node.js only).
*
* @param filePath - Path to the file
* @returns Promise resolving to the MediaFile instance
*/
async fromFile(e) {
if (!F)
throw new Error("Loading from file path is only supported in Node.js environment");
try {
const t = await (await import("fs/promises")).readFile(e), s = await import("path");
return this.file_name = s.basename(e), this._content = this._bufferToArrayBuffer(t), this._setContentTypeFromFileName(), this;
} catch (t) {
throw new Error(`Failed to load file from path: ${e}. ${t.message}`);
}
}
/**
* Load file from a URL.
*
* @param url - URL to fetch the file from
* @param headers - Optional headers for the request
* @returns Promise resolving to the MediaFile instance
*/
async fromUrl(e, t) {
const s = async (n, o) => {
const a = {};
return o && Object.keys(o).length > 0 ? a.headers = o : F && (a.headers = {
"User-Agent": "MediaFile/1.0.0"
}), await fetch(n, a);
};
try {
let n = await s(e, t);
if (!n.ok) {
if (n.status === 401 || n.status === 403) {
const r = new URL(e);
r.search = "", n = await s(r.toString(), t);
}
if (!n.ok)
throw new Error(`HTTP error! Status: ${n.status}`);
}
const o = n.headers.get("content-type");
this.content_type = o ? o.split(";")[0].trim() : "application/octet-stream";
const a = n.headers.get("content-disposition");
if (a) {
const r = a.match(/filename=(?:['"]?)([^'";\n]+)/i);
r && r[1] && (this.file_name = r[1]);
}
if (!this.file_name || this.file_name === "file") {
const r = new URL(e).pathname.split("/"), l = r[r.length - 1];
l && l.trim() !== "" ? this.file_name = decodeURIComponent(l) : this.file_name = "downloaded_file";
}
return (!this.content_type || this.content_type === "application/octet-stream") && this._setContentTypeFromFileName(), this._content = await n.arrayBuffer(), this;
} catch (n) {
throw new Error(`Failed to load file from URL: ${e}. ${n.message}`);
}
}
/**
* Load file from base64 encoded string.
*
* @param base64Data - Base64 encoded string, optionally with data URI prefix
* @returns The MediaFile instance
*/
fromBase64(e) {
const { data: t, mediaType: s } = this._parseBase64Uri(e);
s && (this.content_type = s);
try {
return this._content = F ? this._decodeBase64NodeJs(t) : this._decodeBase64Browser(t), this;
} catch (n) {
throw new Error(`Failed to decode base64 data: ${n.message}`);
}
}
/**
* Load file from binary data.
*
* @param data - ArrayBuffer, Buffer, or Uint8Array containing the file data
* @r