@glodon-aiot/react-components
Version:
aiot react components
97 lines (96 loc) • 2.65 kB
JavaScript
var u = (e, s, t) => new Promise((n, c) => {
var a = (r) => {
try {
o(t.next(r));
} catch (h) {
c(h);
}
}, i = (r) => {
try {
o(t.throw(r));
} catch (h) {
c(h);
}
}, o = (r) => r.done ? n(r.value) : Promise.resolve(r.value).then(a, i);
o((t = t.apply(e, s)).next());
});
const m = (e) => {
if (e === 0)
return "0 B";
const s = 1024, t = ["B", "KB", "MB", "GB"], n = Math.floor(Math.log(e) / Math.log(s));
return Math.round(e / Math.pow(s, n) * 100) / 100 + " " + t[n];
}, S = (e) => u(void 0, null, function* () {
let s = !1, t = 0;
try {
const n = yield fetch(e, {
method: "HEAD",
headers: {
Range: "bytes=0-0"
}
}), c = n.headers.get("Accept-Ranges"), a = n.headers.get("Content-Range"), i = n.headers.get("Content-Length");
if (a) {
const o = a.match(/bytes \d+-\d+\/(\d+)/);
o && (t = parseInt(o[1], 10));
} else
i && (t = parseInt(i, 10));
return s = n.status === 206 || c === "bytes" || a !== null && a.startsWith("bytes"), {
supportsRange: s,
fileSize: t
};
} catch (n) {
return console.warn("Range 支持检测失败:", n), {
supportsRange: !1,
fileSize: 0
};
}
}), x = (e) => u(void 0, null, function* () {
try {
const t = (yield fetch(e, {
method: "HEAD"
})).headers.get("Content-Length");
return t ? parseInt(t, 10) : 0;
} catch (s) {
return console.warn("无法获取文件大小:", s), 0;
}
}), z = (e) => {
if (!e || e.startsWith("blob:") || e.startsWith("data:"))
return e;
const s = e.startsWith("http%3A%2F%2F") || e.startsWith("https%3A%2F%2F");
let t = e;
if (s)
try {
t = decodeURIComponent(e);
} catch (n) {
t = e;
}
try {
return new URL(t).toString();
} catch (n) {
try {
const c = t.indexOf("://");
if (c === -1)
return t;
const a = t.substring(0, c + 3), i = t.substring(c + 3), o = i.indexOf("/");
if (o === -1)
return t;
const r = i.substring(0, o), h = i.substring(o + 1), p = h.indexOf("?"), f = p === -1 ? h : h.substring(0, p), g = p === -1 ? "" : h.substring(p + 1), l = f.split("/").map((d) => {
if (!d)
return d;
try {
return decodeURIComponent(d) !== d ? d : encodeURIComponent(d);
} catch (R) {
return encodeURIComponent(d);
}
}).join("/");
return g ? `${a}${r}/${l}?${g}` : `${a}${r}/${l}`;
} catch (c) {
return t;
}
}
};
export {
S as checkRangeSupport,
m as formatFileSize,
x as getFileSize,
z as normalizeUrl
};