fix media mime guessing when uploading to entity

This commit is contained in:
dswbx
2025-02-20 11:32:24 +01:00
parent eaa7276173
commit 94c40de011
10 changed files with 81 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
import { randomString } from "core/utils";
import { isFile, randomString } from "core/utils";
import { extension } from "media/storage/mime-types-tiny";
export function getExtension(filename: string): string | undefined {
export function getExtensionFromName(filename: string): string | undefined {
if (!filename.includes(".")) return;
const parts = filename.split(".");
@@ -17,6 +18,12 @@ export function getRandomizedFilename(file: File | string, length = 16): string
throw new Error("Invalid file name");
}
let ext = getExtensionFromName(filename);
if (isFile(file) && file.type) {
const _ext = extension(file.type);
if (_ext.length > 0) ext = _ext;
}
// @todo: use uuid instead?
return [randomString(length), getExtension(filename)].filter(Boolean).join(".");
return [randomString(length), ext].filter(Boolean).join(".");
}