2024-03-16 16:03:42 +00:00
|
|
|
import mime from "mime";
|
2023-12-10 20:03:56 +00:00
|
|
|
import path from "path";
|
|
|
|
|
2023-12-10 20:29:43 +00:00
|
|
|
import { trimPrefix } from "./string.js";
|
2023-12-10 20:03:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file mime type. In case the type is not provided, try to guess the
|
|
|
|
* file type based on the extension.
|
|
|
|
*
|
|
|
|
* @param file - The file.
|
|
|
|
* @returns - The mime type.
|
|
|
|
*/
|
|
|
|
export function getFileMimeType(file: File): string {
|
|
|
|
if (file.type) return file.type;
|
|
|
|
let ext = path.extname(file.name);
|
|
|
|
ext = trimPrefix(ext, ".");
|
|
|
|
if (ext !== "") {
|
|
|
|
const mimeType = mime.getType(ext);
|
|
|
|
if (mimeType) {
|
|
|
|
return mimeType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|