From 5f7c77a3d84a403e4edbe835f34a6b8ac1464bad Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Fri, 28 Jun 2024 21:00:02 -1000 Subject: [PATCH] fix: :bug: Fix files without a filename crashing the media manager --- classes/media/drivers/disk.test.ts | 16 ++++++++++++++++ classes/media/drivers/disk.ts | 5 ++++- classes/media/drivers/s3.test.ts | 17 +++++++++++++++++ classes/media/drivers/s3.ts | 5 ++++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/classes/media/drivers/disk.test.ts b/classes/media/drivers/disk.test.ts index 27da1366..9942a68c 100644 --- a/classes/media/drivers/disk.test.ts +++ b/classes/media/drivers/disk.test.ts @@ -77,6 +77,22 @@ describe("DiskMediaDriver", () => { }); }); + it("should properly handle a Blob instead of a File", async () => { + const file = new Blob(["test"], { type: "image/webp" }); + const result = await diskDriver.addFile(file as File); + + expect(mockMediaHasher.getMediaHash).toHaveBeenCalledWith(file); + expect(bunWriteSpy).toHaveBeenCalledWith( + expect.stringContaining("testhash"), + expect.any(ArrayBuffer), + ); + expect(result).toEqual({ + uploadedFile: expect.any(Blob), + path: expect.stringContaining("testhash"), + hash: "testhash", + }); + }); + it("should get a file by hash", async () => { const hash = "testhash"; const databaseHashFetcher = mock(() => Promise.resolve("test.webp")); diff --git a/classes/media/drivers/disk.ts b/classes/media/drivers/disk.ts index fdf42a92..7df0501c 100644 --- a/classes/media/drivers/disk.ts +++ b/classes/media/drivers/disk.ts @@ -30,8 +30,11 @@ export class DiskMediaDriver implements MediaDriver { public async addFile( file: File, ): Promise> { + // Sometimes the file name is not available, so we generate a random name + const fileName = file.name ?? crypto.randomUUID(); + const hash = await this.mediaHasher.getMediaHash(file); - const path = join(hash, file.name); + const path = join(hash, fileName); const fullPath = join(this.config.media.local_uploads_folder, path); await Bun.write(fullPath, await file.arrayBuffer()); diff --git a/classes/media/drivers/s3.test.ts b/classes/media/drivers/s3.test.ts index 562a590c..315349da 100644 --- a/classes/media/drivers/s3.test.ts +++ b/classes/media/drivers/s3.test.ts @@ -66,6 +66,23 @@ describe("S3MediaDriver", () => { }); }); + it("should handle a Blob instead of a File", async () => { + const file = new Blob(["test"], { type: "image/webp" }); + const result = await s3Driver.addFile(file as File); + + expect(mockMediaHasher.getMediaHash).toHaveBeenCalledWith(file); + expect(mockS3Client.putObject).toHaveBeenCalledWith( + expect.stringContaining("testhash"), + expect.any(ReadableStream), + { size: file.size }, + ); + expect(result).toEqual({ + uploadedFile: expect.any(Blob), + path: expect.stringContaining("testhash"), + hash: "testhash", + }); + }); + it("should get a file by hash", async () => { const hash = "testhash"; const databaseHashFetcher = mock(() => Promise.resolve("test.webp")); diff --git a/classes/media/drivers/s3.ts b/classes/media/drivers/s3.ts index d1e1e2d2..81c1175e 100644 --- a/classes/media/drivers/s3.ts +++ b/classes/media/drivers/s3.ts @@ -38,8 +38,11 @@ export class S3MediaDriver implements MediaDriver { public async addFile( file: File, ): Promise> { + // Sometimes the file name is not available, so we generate a random name + const fileName = file.name ?? crypto.randomUUID(); + const hash = await this.mediaHasher.getMediaHash(file); - const path = `${hash}/${file.name}`; + const path = `${hash}/${fileName}`; await this.s3Client.putObject(path, file.stream(), { size: file.size,