Make all media be uploaded with a unique hash

This commit is contained in:
Jesse Wierzbinski 2024-04-09 03:51:22 -10:00
parent be6b692a7b
commit 8ecdc6261e
No known key found for this signature in database
4 changed files with 37 additions and 28 deletions

View file

@ -10,7 +10,7 @@ export enum MediaBackendType {
interface UploadedFileMetadata {
uploadedFile: File;
path?: string;
path: string;
hash: string;
}
@ -115,7 +115,7 @@ export class LocalMediaBackend extends MediaBackend {
const hash = await new MediaHasher().getMediaHash(convertedFile);
const newFile = Bun.file(
`${this.config.media.local_uploads_folder}/${hash}`,
`${this.config.media.local_uploads_folder}/${hash}/${convertedFile.name}`,
);
if (await newFile.exists()) {
@ -126,7 +126,7 @@ export class LocalMediaBackend extends MediaBackend {
return {
uploadedFile: convertedFile,
path: `./uploads/${convertedFile.name}`,
path: `${hash}/${convertedFile.name}`,
hash: hash,
};
}
@ -186,7 +186,7 @@ export class S3MediaBackend extends MediaBackend {
const hash = await new MediaHasher().getMediaHash(convertedFile);
await this.s3Client.putObject(
convertedFile.name,
`${hash}/${convertedFile.name}`,
convertedFile.stream(),
{
size: convertedFile.size,
@ -195,6 +195,7 @@ export class S3MediaBackend extends MediaBackend {
return {
uploadedFile: convertedFile,
path: `${hash}/${convertedFile.name}`,
hash: hash,
};
}

View file

@ -148,7 +148,7 @@ describe("S3MediaBackend", () => {
expect(result.uploadedFile).toEqual(mockFile);
expect(result.hash).toHaveLength(64);
expect(mockS3Client.putObject).toHaveBeenCalledWith(
mockFile.name,
expect.stringContaining(mockFile.name),
expect.any(ReadableStream),
{ size: mockFile.size },
);
@ -236,7 +236,7 @@ describe("LocalMediaBackend", () => {
const result = await localMediaBackend.addFile(mockFile);
expect(result.uploadedFile).toEqual(mockFile);
expect(result.path).toEqual("./uploads/megamind.png");
expect(result.path).toEqual(expect.stringContaining("megamind.png"));
expect(result.hash).toHaveLength(64);
});

View file

@ -88,15 +88,19 @@ export default apiRoute<{
return;
}
resolve(
encode(
new Uint8ClampedArray(buffer),
metadata?.width ?? 0,
metadata?.height ?? 0,
4,
4,
) as string,
);
try {
resolve(
encode(
new Uint8ClampedArray(buffer),
metadata?.width ?? 0,
metadata?.height ?? 0,
4,
4,
) as string,
);
} catch {
resolve(null);
}
}))();
});
@ -116,16 +120,16 @@ export default apiRoute<{
throw new Error("Invalid media backend");
}
const { uploadedFile } = await mediaManager.addFile(file);
const { path } = await mediaManager.addFile(file);
url = getUrl(uploadedFile.name, config);
url = getUrl(path, config);
let thumbnailUrl = "";
if (thumbnail) {
const { uploadedFile } = await mediaManager.addFile(thumbnail);
const { path } = await mediaManager.addFile(thumbnail);
thumbnailUrl = getUrl(uploadedFile.name, config);
thumbnailUrl = getUrl(path, config);
}
const newAttachment = await client.attachment.create({

View file

@ -88,15 +88,19 @@ export default apiRoute<{
return;
}
resolve(
encode(
new Uint8ClampedArray(buffer),
metadata?.width ?? 0,
metadata?.height ?? 0,
4,
4,
) as string,
);
try {
resolve(
encode(
new Uint8ClampedArray(buffer),
metadata?.width ?? 0,
metadata?.height ?? 0,
4,
4,
) as string,
);
} catch {
resolve(null);
}
}))();
});