improve media mime type inferring + added uploadToEntity in media api

This commit is contained in:
dswbx
2025-02-14 18:02:43 +01:00
parent 4bd201d454
commit fb76f8d789
12 changed files with 147 additions and 69 deletions

View File

@@ -14,10 +14,17 @@ export class EventListener<E extends Event = Event> {
event: EventClass;
handler: ListenerHandler<E>;
once: boolean = false;
id?: string;
constructor(event: EventClass, handler: ListenerHandler<E>, mode: ListenerMode = "async") {
constructor(
event: EventClass,
handler: ListenerHandler<E>,
mode: ListenerMode = "async",
id?: string
) {
this.event = event;
this.handler = handler;
this.mode = mode;
this.id = id;
}
}

View File

@@ -6,6 +6,7 @@ export type RegisterListenerConfig =
| {
mode?: ListenerMode;
once?: boolean;
id?: string;
};
export interface EmitsEvents {
@@ -124,6 +125,14 @@ export class EventManager<
addListener(listener: EventListener) {
this.throwIfEventNotRegistered(listener.event);
if (listener.id) {
const existing = this.listeners.find((l) => l.id === listener.id);
if (existing) {
console.warn(`Listener with id "${listener.id}" already exists.`);
return this;
}
}
this.listeners.push(listener);
return this;
}
@@ -140,6 +149,9 @@ export class EventManager<
if (config.once) {
listener.once = true;
}
if (config.id) {
listener.id = `${event.slug}-${config.id}`;
}
this.addListener(listener as any);
}

View File

@@ -207,7 +207,7 @@ export async function blobToFile(
if (isFile(blob)) return blob;
if (!isBlob(blob)) throw new Error("Not a Blob");
const type = !isMimeType(overrides.type, ["application/octet-stream"])
const type = isMimeType(overrides.type, ["application/octet-stream"])
? overrides.type
: await detectMimeType(blob);
const ext = type ? extension(type) : "";