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

View file

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

View file

@ -88,6 +88,7 @@ export default apiRoute<{
return; return;
} }
try {
resolve( resolve(
encode( encode(
new Uint8ClampedArray(buffer), new Uint8ClampedArray(buffer),
@ -97,6 +98,9 @@ export default apiRoute<{
4, 4,
) as string, ) as string,
); );
} catch {
resolve(null);
}
}))(); }))();
}); });
@ -116,16 +120,16 @@ export default apiRoute<{
throw new Error("Invalid media backend"); 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 = ""; let thumbnailUrl = "";
if (thumbnail) { 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({ const newAttachment = await client.attachment.create({

View file

@ -88,6 +88,7 @@ export default apiRoute<{
return; return;
} }
try {
resolve( resolve(
encode( encode(
new Uint8ClampedArray(buffer), new Uint8ClampedArray(buffer),
@ -97,6 +98,9 @@ export default apiRoute<{
4, 4,
) as string, ) as string,
); );
} catch {
resolve(null);
}
}))(); }))();
}); });