mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
fix: 🐛 Fix files without a filename crashing the media manager
This commit is contained in:
parent
e95cabb304
commit
5f7c77a3d8
|
|
@ -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"));
|
||||
|
|
|
|||
|
|
@ -30,8 +30,11 @@ export class DiskMediaDriver implements MediaDriver {
|
|||
public async addFile(
|
||||
file: File,
|
||||
): 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 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());
|
||||
|
|
|
|||
|
|
@ -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"));
|
||||
|
|
|
|||
|
|
@ -38,8 +38,11 @@ export class S3MediaDriver implements MediaDriver {
|
|||
public async addFile(
|
||||
file: File,
|
||||
): 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 path = `${hash}/${file.name}`;
|
||||
const path = `${hash}/${fileName}`;
|
||||
|
||||
await this.s3Client.putObject(path, file.stream(), {
|
||||
size: file.size,
|
||||
|
|
|
|||
Loading…
Reference in a new issue