Skip to content

Commit

Permalink
refactor: optimize inferExtension util
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdvlpr committed Dec 4, 2024
1 parent d49e174 commit 0fcca05
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/utils/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,16 @@ export const isImageString = (url: string) => {
* inferExtension('A video', 'video/mp4') // A video.mp4
*/
export const inferExtension = async (filename: string, filetype?: string) => {
if (!filetype) return filename;
const { default: mime } = await import('mime');
const extractedExtension = mime.extension(filetype);
if (!extractedExtension) {
return filename;
}
const hasExtension = /\.[0-9a-z]+$/i.test(filename);
if (hasExtension) {
if (!filetype || /\.[0-9a-z]+$/i.test(filename)) return filename;

try {
const { default: mime } = await import('mime');
const extractedExtension = mime.extension(filetype);
return extractedExtension ? `${filename}.${extractedExtension}` : filename;
} catch (e) {
errorCatcher(e);
return filename;
}
return `${filename}.${extractedExtension}`;
};

/**
Expand Down

0 comments on commit 0fcca05

Please sign in to comment.