diff --git a/app/__test__/api/MediaApi.spec.ts b/app/__test__/api/MediaApi.spec.ts index 196d711..7387792 100644 --- a/app/__test__/api/MediaApi.spec.ts +++ b/app/__test__/api/MediaApi.spec.ts @@ -43,18 +43,6 @@ describe("MediaApi", () => { expect(api.getUploadHeaders().get("Authorization")).toBe("Bearer token"); }); - it("should upload file directly", async () => { - const name = "image.png"; - const file = await Bun.file(`${assetsPath}/${name}`); - - // @ts-ignore tests - const api = new MediaApi({}, mockedBackend.request); - const result = await api.uploadFile(file as any, name); - expect(result.name).toBe(name); - expect(result.is_file).toBe(true); - expect(result.size).toBe(file.size); - }); - it("should get file: native", async () => { const name = "image.png"; const path = `${assetsTmpPath}/${name}`; @@ -67,24 +55,24 @@ describe("MediaApi", () => { await file.delete(); }); - it("getFile", async () => { + it("download", async () => { // @ts-ignore tests const api = new MediaApi({}, mockedBackend.request); const name = "image.png"; - const file = await api.getFile(name); + const file = await api.download(name); expect(isFile(file)).toBe(true); expect(file.size).toBeGreaterThan(0); expect(file.type).toBe("image/png"); expect(file.name).toContain(name); }); - it("getFileResponse", async () => { + it("getFile", async () => { // @ts-ignore tests const api = new MediaApi({}, mockedBackend.request); const name = "image.png"; - const res = await api.getFileResponse(name); + const res = await api.getFile(name); expect(res.ok).toBe(true); // make sure it's a normal api request as usual expect(res.res.ok).toBe(true); @@ -143,7 +131,13 @@ describe("MediaApi", () => { await matches(api.upload(response, "response.png"), "response.png"); } - // upload via readable + // upload via readable from bun await matches(await api.upload(file.stream(), "readable.png"), "readable.png"); + + // upload via readable from response + { + const response = (await mockedBackend.request(url)) as Response; + await matches(await api.upload(response.body!, "readable.png"), "readable.png"); + } }); }); diff --git a/app/src/media/api/MediaApi.ts b/app/src/media/api/MediaApi.ts index 02eda71..83dd504 100644 --- a/app/src/media/api/MediaApi.ts +++ b/app/src/media/api/MediaApi.ts @@ -1,3 +1,4 @@ +import type { FileListObject } from "media"; import { type BaseModuleApiOptions, ModuleApi, type PrimaryFieldType } from "modules/ModuleApi"; import type { FileWithPath } from "ui/elements/media/file-selector"; @@ -10,34 +11,34 @@ export class MediaApi extends ModuleApi { }; } - getFiles() { - return this.get(["files"]); + listFiles() { + return this.get(["files"]); } - getFileResponse(filename: string) { - return this.get(["file", filename], undefined, { + getFile(filename: string) { + return this.get>(["file", filename], undefined, { headers: { Accept: "*/*" } }); } - async getFile(filename: string): Promise { - const { res } = await this.getFileResponse(filename); - if (!res.ok || !res.body) { - throw new Error("Failed to fetch file"); - } - return await res.blob(); - } - async getFileStream(filename: string): Promise> { - const { res } = await this.getFileResponse(filename); + const { res } = await this.getFile(filename); if (!res.ok || !res.body) { throw new Error("Failed to fetch file"); } return res.body; } + async download(filename: string): Promise { + const { res } = await this.getFile(filename); + if (!res.ok || !res.body) { + throw new Error("Failed to fetch file"); + } + return (await res.blob()) as File; + } + getFileUploadUrl(file: FileWithPath): string { return this.getUrl(`/upload/${file.path}`); } @@ -52,7 +53,7 @@ export class MediaApi extends ModuleApi { }); } - uploadFile(body: File | ReadableStream, filename?: string) { + protected uploadFile(body: File | ReadableStream, filename?: string) { let type: string = "application/octet-stream"; let name: string = filename || ""; try {