MIME Utilities (getMime & getMimes)
High-performance, zero-dependency MIME type resolution built natively into core XyPriss.
XyPriss provides built-in utilities (getMime and getMimes) exported directly from the core package. These helpers eliminate external packages (like mime-types) and streamline server security setup—especially for configuring allowed formats in native XHSC file uploads.
Leading Dot Normalization
Handles '.png', 'png', or '.PNG' seamlessly with case-insensitive resolution.
Automatic Deduplication
Resolves '.jpg' and '.jpeg' to a single deduplicated 'image/jpeg' MIME string.
XHSC Engine Bridge
Directly populates allowedMimeTypes for high-performance native Go binary uploads.
Framework Fallback
Reads global Configs allowedExtensions when called without parameters.
API Reference
getMime(ext: string): string
Resolves a single MIME type string from a given extension. Returns 'application/octet-stream' as a safe fallback if unknown.
import { getMime } from "xypriss";
console.log(getMime(".png")); // "image/png"
console.log(getMime("jpg")); // "image/jpeg"
console.log(getMime(".WEBP")); // "image/webp"
console.log(getMime(".pdf")); // "application/pdf"
console.log(getMime(".unknown")); // "application/octet-stream"getMimes(extensions?: string | string[]): string[]
Resolves a list or single extension into a deduplicated array of valid MIME strings.
import { getMimes } from "xypriss";
// 1. Array of extensions (auto-deduplicated)
const mimes = getMimes([".jpg", ".jpeg", ".png", ".svg"]);
// Returns: ["image/jpeg", "image/png", "image/svg+xml"]
// 2. Single extension string
const pdfMime = getMimes(".pdf");
// Returns: ["application/pdf"]
// 3. Fallback mode (reads global Configs.fileUpload.allowedExtensions)
const configMimes = getMimes();File Upload Integration
The primary use case for getMimes is mapping simplified extension lists to native XHSC file upload security parameters.
Single Server Configuration
import { createServer, getMimes } from "xypriss";
const app = createServer({
fileUpload: {
enabled: true,
destination: "./public/cdn",
maxFileSize: 15 * 1024 * 1024, // 15MB
// Automatically converts extensions to verified MIME array
allowedMimeTypes: getMimes([".png", ".jpg", ".jpeg", ".webp", ".svg"]),
useSubDir: true,
},
});Controller & Route Validation
import { Router, Upload, getMimes } from "xypriss";
const router = Router();
const ALLOWED_MIMES = getMimes([".png", ".jpg", ".webp"]);
router.post("/api/avatar", Upload.single("file"), (req, res) => {
const file = (req as any).file;
if (!file || !ALLOWED_MIMES.includes(file.mimetype)) {
return res.status(400).json({ error: "Invalid file type uploaded" });
}
res.success({ filename: file.filename, size: file.size });
});Supported MIME Types
| Category | Extensions | Resolved MIME Types |
|---|---|---|
| Images | .png, .jpg, .jpeg, .webp, .gif, .svg, .ico, .avif | image/png, image/jpeg, image/webp, image/gif, image/svg+xml... |
| Documents | .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .txt, .csv | application/pdf, application/msword, text/plain, text/csv... |
| Media | .mp3, .wav, .ogg, .mp4, .webm, .mkv | audio/mpeg, audio/wav, video/mp4, video/webm... |
| Archives & Code | .zip, .tar, .gz, .json, .xml, .html, .css, .js | application/zip, application/json, text/html... |
getMime and getMimes run in pure TypeScript with zero external Node modules, ensuring minimal memory overhead and blazingly fast execution.Learn how to handle high-performance file uploads with native XHSC acceleration.
