s5-js/src/utils/file.ts

39 lines
1.1 KiB
TypeScript

import mime from "mime";
import path from "path";
import { trimPrefix } from "./string.js";
/**
* 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 "";
}
/**
* Sometimes file object might have had the type property defined manually with
* Object.defineProperty and some browsers (namely firefox) can have problems
* reading it after the file has been appended to form data. To overcome this,
* we recreate the file object using native File constructor with a type defined
* as a constructor argument.
*
* @param file - The input file.
* @returns - The processed file.
*/
export function ensureFileObjectConsistency(file: File): File {
return new File([file], file.name, { type: getFileMimeType(file) });
}