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

@@ -1,5 +1,10 @@
import type { FileListObject } from "media";
import { type BaseModuleApiOptions, ModuleApi, type PrimaryFieldType } from "modules/ModuleApi";
import {
type BaseModuleApiOptions,
ModuleApi,
type PrimaryFieldType,
type TInput
} from "modules/ModuleApi";
import type { FileWithPath } from "ui/elements/media/file-selector";
export type MediaApiOptions = BaseModuleApiOptions & {};
@@ -53,12 +58,24 @@ export class MediaApi extends ModuleApi<MediaApiOptions> {
});
}
protected uploadFile(body: File | ReadableStream, filename?: string) {
let type: string = "application/octet-stream";
let name: string = filename || "";
protected uploadFile(
body: File | ReadableStream,
opts?: {
filename?: string;
path?: TInput;
_init?: Omit<RequestInit, "body">;
}
) {
const headers = {
"Content-Type": "application/octet-stream",
...(opts?._init?.headers || {})
};
let name: string = opts?.filename || "";
try {
type = (body as File).type;
if (!filename) {
if (typeof (body as File).type !== "undefined") {
headers["Content-Type"] = (body as File).type;
}
if (!opts?.filename) {
name = (body as File).name;
}
} catch (e) {}
@@ -67,32 +84,67 @@ export class MediaApi extends ModuleApi<MediaApiOptions> {
name = name.split("/").pop() || "";
}
const init = {
...(opts?._init || {}),
headers
};
if (opts?.path) {
return this.post(opts.path, body, init);
}
if (!name || name.length === 0) {
throw new Error("Invalid filename");
}
return this.post(["upload", name], body, {
headers: {
"Content-Type": type
}
});
return this.post(opts?.path ?? ["upload", name], body, init);
}
async upload(item: Request | Response | string | File | ReadableStream, filename?: string) {
async upload(
item: Request | Response | string | File | ReadableStream,
opts: {
filename?: string;
_init?: Omit<RequestInit, "body">;
path?: TInput;
} = {}
) {
if (item instanceof Request || typeof item === "string") {
const res = await this.fetcher(item);
if (!res.ok || !res.body) {
throw new Error("Failed to fetch file");
}
return this.uploadFile(res.body, filename);
return this.uploadFile(res.body, opts);
} else if (item instanceof Response) {
if (!item.body) {
throw new Error("Invalid response");
}
return this.uploadFile(item.body, filename);
return this.uploadFile(item.body, {
...(opts ?? {}),
_init: {
...(opts._init ?? {}),
headers: {
...(opts._init?.headers ?? {}),
"Content-Type": item.headers.get("Content-Type") || "application/octet-stream"
}
}
});
}
return this.uploadFile(item, filename);
return this.uploadFile(item, opts);
}
async uploadToEntity(
entity: string,
id: PrimaryFieldType,
field: string,
item: Request | Response | string | File | ReadableStream,
opts?: {
_init?: Omit<RequestInit, "body">;
}
) {
return this.upload(item, {
...opts,
path: ["entity", entity, id, field]
});
}
deleteFile(filename: string) {