mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
Make all media be uploaded with a unique hash
This commit is contained in:
parent
be6b692a7b
commit
8ecdc6261e
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -88,6 +88,7 @@ export default apiRoute<{
|
|||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
resolve(
|
||||
encode(
|
||||
new Uint8ClampedArray(buffer),
|
||||
|
|
@ -97,6 +98,9 @@ export default apiRoute<{
|
|||
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({
|
||||
|
|
|
|||
|
|
@ -88,6 +88,7 @@ export default apiRoute<{
|
|||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
resolve(
|
||||
encode(
|
||||
new Uint8ClampedArray(buffer),
|
||||
|
|
@ -97,6 +98,9 @@ export default apiRoute<{
|
|||
4,
|
||||
) as string,
|
||||
);
|
||||
} catch {
|
||||
resolve(null);
|
||||
}
|
||||
}))();
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue