fix: 🐛 Fix files without a filename crashing the media manager

This commit is contained in:
Jesse Wierzbinski 2024-06-28 21:00:02 -10:00
parent e95cabb304
commit 5f7c77a3d8
No known key found for this signature in database
4 changed files with 41 additions and 2 deletions

View file

@ -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 () => { it("should get a file by hash", async () => {
const hash = "testhash"; const hash = "testhash";
const databaseHashFetcher = mock(() => Promise.resolve("test.webp")); const databaseHashFetcher = mock(() => Promise.resolve("test.webp"));

View file

@ -30,8 +30,11 @@ export class DiskMediaDriver implements MediaDriver {
public async addFile( public async addFile(
file: File, file: File,
): Promise<Omit<UploadedFileMetadata, "blurhash">> { ): Promise<Omit<UploadedFileMetadata, "blurhash">> {
// 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 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); const fullPath = join(this.config.media.local_uploads_folder, path);
await Bun.write(fullPath, await file.arrayBuffer()); await Bun.write(fullPath, await file.arrayBuffer());

View file

@ -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 () => { it("should get a file by hash", async () => {
const hash = "testhash"; const hash = "testhash";
const databaseHashFetcher = mock(() => Promise.resolve("test.webp")); const databaseHashFetcher = mock(() => Promise.resolve("test.webp"));

View file

@ -38,8 +38,11 @@ export class S3MediaDriver implements MediaDriver {
public async addFile( public async addFile(
file: File, file: File,
): Promise<Omit<UploadedFileMetadata, "blurhash">> { ): Promise<Omit<UploadedFileMetadata, "blurhash">> {
// 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 hash = await this.mediaHasher.getMediaHash(file);
const path = `${hash}/${file.name}`; const path = `${hash}/${fileName}`;
await this.s3Client.putObject(path, file.stream(), { await this.s3Client.putObject(path, file.stream(), {
size: file.size, size: file.size,