vue-files-preview-inno
Version:
A tool for previewing files such as doc, excel, pdf, image, markdown, txt, audio, and video and so on.
181 lines (180 loc) • 5.02 kB
JavaScript
import { getPreviewTypeByFileType as f, videoPreviewTypeList as x, pdfPreviewTypeList as g, imagePreviewTypeList as b, arrayBufferPreviewTypeList as w, textFilePreviewTypeList as v } from "../preview.const.mjs";
const y = {
mp4: "video/mp4",
webm: "video/webm",
ogg: "video/ogg",
mkv: "video/x-matroska",
avi: "video/x-msvideo",
mpeg: "video/mpeg",
flv: "video/x-flv",
mov: "video/quicktime",
wmv: "video/x-ms-wmv"
}, p = {
"image/svg+xml": "svg",
"image/png": "png",
"image/jpeg": "jpg",
"image/gif": "gif",
"image/webp": "webp",
"image/bmp": "bmp",
"image/x-icon": "ico",
"image/vnd.microsoft.icon": "ico",
"application/pdf": "pdf",
"text/plain": "txt",
"text/markdown": "md",
"text/html": "html",
"text/css": "css",
"text/javascript": "js",
"application/javascript": "js",
"application/json": "json",
"application/xml": "xml",
"text/xml": "xml",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
"application/msword": "doc",
"application/vnd.ms-excel": "xls",
"application/vnd.ms-powerpoint": "ppt",
"application/epub+zip": "epub",
"video/mp4": "mp4",
"video/webm": "webm",
"video/ogg": "ogg",
"video/quicktime": "mov",
"audio/mpeg": "mp3",
"audio/wav": "wav",
"audio/ogg": "ogg",
"audio/flac": "flac",
"application/vnd.ms-outlook": "msg"
};
function O(e) {
if (!e) return "";
if (p[e]) return p[e];
const n = e.split("/");
return n.length === 2 ? n[1].replace(/^x-/, "") : "";
}
function l(e, n) {
if ("name" in e && e.name) {
const t = e.name.lastIndexOf(".");
if (t !== -1) return e.name.substring(t + 1);
}
if (n) {
const t = n.lastIndexOf(".");
if (t !== -1) return n.substring(t + 1);
}
return O(e.type);
}
function R(e, n) {
if ("name" in e && e.name) {
const t = e.name.lastIndexOf(".");
return t === -1 ? e.name : e.name.substring(0, t);
}
if (n) {
const t = n.lastIndexOf(".");
return t === -1 ? n : n.substring(0, t);
}
return "";
}
function c(e) {
try {
return decodeURIComponent(e);
} catch {
return e;
}
}
function h(e, n) {
try {
const t = new URL(e, window.location.origin), s = c(t.pathname), o = s.lastIndexOf(".");
if (o !== -1) {
const a = s.substring(o + 1).toLowerCase().split("?")[0].split("#")[0];
if (a) return a;
}
} catch {
const t = c(e), s = t.lastIndexOf(".");
if (s !== -1) {
const i = t.substring(s + 1).toLowerCase().split("?")[0].split("#")[0];
if (i) return i;
}
}
if (n) {
const t = n.lastIndexOf(".");
if (t !== -1) return n.substring(t + 1).toLowerCase();
}
return "";
}
function U(e, n) {
try {
const t = new URL(e, window.location.origin), s = c(t.pathname), o = s.lastIndexOf("/"), i = s.substring(o + 1), a = i.lastIndexOf("."), r = a === -1 ? i : i.substring(0, a);
if (r) return r;
} catch {
const t = e.lastIndexOf("/"), s = c(e.substring(t + 1).split("?")[0].split("#")[0]), o = s.lastIndexOf("."), i = o === -1 ? s : s.substring(0, o);
if (i) return i;
}
if (n) {
const t = n.lastIndexOf(".");
return t === -1 ? n : n.substring(0, t);
}
return "";
}
async function I(e, n) {
const t = h(e), s = f(t), o = m(s);
if (o === "image" || o === "video" || o === "pdf")
return e;
const i = await fetch(e);
if (!i.ok)
throw new Error(`Failed to fetch file: ${i.status} ${i.statusText}`);
return o === "text" ? await i.text() : o === "arrayBuffer" ? await i.arrayBuffer() : e;
}
function j(e, n) {
const t = f(l(e, n)), s = m(t);
return new Promise((o) => {
const i = e, a = new FileReader();
switch (s) {
case "text":
a.readAsText(i), a.onload = () => {
o(a.result);
};
break;
case "arrayBuffer":
a.readAsArrayBuffer(i), a.onload = () => {
o(a.result);
};
break;
case "image":
o(window.URL.createObjectURL(i));
break;
case "pdf": {
const r = new Blob([i], { type: "application/pdf" }), d = URL.createObjectURL(r);
o(d);
break;
}
case "video": {
const r = l(e, n), d = y[r] || e.type || "video/mp4", u = URL.createObjectURL(new Blob([i], { type: d }));
o(u);
break;
}
default:
o(window.URL.createObjectURL(i));
break;
}
});
}
function m(e) {
const n = {
text: v.includes(e),
arrayBuffer: w.includes(e),
image: b.includes(e),
pdf: g.includes(e),
video: x.includes(e)
};
return Object.keys(n).find((t) => n[t]);
}
export {
O as getExtFromMimeType,
R as getFileName,
U as getFileNameFromUrl,
j as getFileRenderByFile,
I as getFileRenderByUrl,
m as getFileRenderType,
l as getFileType,
h as getFileTypeFromUrl
};
//# sourceMappingURL=utils.mjs.map