mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
/** How the app shows a file, from its extension. */
|
|
export type ArtifactKind =
|
|
| "image"
|
|
| "svg"
|
|
| "audio"
|
|
| "video"
|
|
| "pdf"
|
|
| "html"
|
|
| "markdown"
|
|
| "mermaid"
|
|
| "table"
|
|
| "font"
|
|
| "document"
|
|
| "spreadsheet"
|
|
| "presentation"
|
|
| "text"
|
|
|
|
const officeKinds = new Map<string, ArtifactKind>([
|
|
["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "document"],
|
|
["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "spreadsheet"],
|
|
["application/vnd.openxmlformats-officedocument.presentationml.presentation", "presentation"],
|
|
])
|
|
|
|
const mimes = new Map([
|
|
["png", "image/png"],
|
|
["jpg", "image/jpeg"],
|
|
["jpeg", "image/jpeg"],
|
|
["gif", "image/gif"],
|
|
["webp", "image/webp"],
|
|
["avif", "image/avif"],
|
|
["bmp", "image/bmp"],
|
|
["ico", "image/x-icon"],
|
|
["tif", "image/tiff"],
|
|
["tiff", "image/tiff"],
|
|
["heic", "image/heic"],
|
|
["svg", "image/svg+xml"],
|
|
["mp3", "audio/mpeg"],
|
|
["wav", "audio/wav"],
|
|
["ogg", "audio/ogg"],
|
|
["oga", "audio/ogg"],
|
|
["m4a", "audio/mp4"],
|
|
["aac", "audio/aac"],
|
|
["flac", "audio/flac"],
|
|
["opus", "audio/ogg"],
|
|
["weba", "audio/webm"],
|
|
["mp4", "video/mp4"],
|
|
["m4v", "video/mp4"],
|
|
["webm", "video/webm"],
|
|
["mov", "video/quicktime"],
|
|
["ogv", "video/ogg"],
|
|
["mkv", "video/x-matroska"],
|
|
["pdf", "application/pdf"],
|
|
["html", "text/html"],
|
|
["htm", "text/html"],
|
|
["md", "text/markdown"],
|
|
["markdown", "text/markdown"],
|
|
["mdx", "text/markdown"],
|
|
["mmd", "text/vnd.mermaid"],
|
|
["mermaid", "text/vnd.mermaid"],
|
|
["csv", "text/csv"],
|
|
["tsv", "text/tab-separated-values"],
|
|
["ttf", "font/ttf"],
|
|
["otf", "font/otf"],
|
|
["woff", "font/woff"],
|
|
["woff2", "font/woff2"],
|
|
["docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
|
|
["xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],
|
|
["pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"],
|
|
])
|
|
|
|
function artifactExtension(path: string) {
|
|
const name = path.split(/[\\/]/).pop() ?? ""
|
|
const index = name.lastIndexOf(".")
|
|
if (index <= 0) return ""
|
|
return name.slice(index + 1).toLowerCase()
|
|
}
|
|
|
|
export function artifactMime(path: string) {
|
|
return mimes.get(artifactExtension(path))
|
|
}
|
|
|
|
export function artifactKind(path: string): ArtifactKind {
|
|
const mime = artifactMime(path)
|
|
if (!mime) return "text"
|
|
if (mime === "image/svg+xml") return "svg"
|
|
if (mime === "application/pdf") return "pdf"
|
|
if (mime === "text/html") return "html"
|
|
if (mime === "text/markdown") return "markdown"
|
|
if (mime === "text/vnd.mermaid") return "mermaid"
|
|
if (mime === "text/csv" || mime === "text/tab-separated-values") return "table"
|
|
if (mime.startsWith("image/")) return "image"
|
|
if (mime.startsWith("audio/")) return "audio"
|
|
if (mime.startsWith("font/")) return "font"
|
|
return officeKinds.get(mime) ?? "video"
|
|
}
|