mirror of
https://github.com/versia-pub/server.git
synced 2025-12-07 00:48:18 +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 {
|
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,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -88,15 +88,19 @@ export default apiRoute<{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(
|
try {
|
||||||
encode(
|
resolve(
|
||||||
new Uint8ClampedArray(buffer),
|
encode(
|
||||||
metadata?.width ?? 0,
|
new Uint8ClampedArray(buffer),
|
||||||
metadata?.height ?? 0,
|
metadata?.width ?? 0,
|
||||||
4,
|
metadata?.height ?? 0,
|
||||||
4,
|
4,
|
||||||
) as string,
|
4,
|
||||||
);
|
) 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({
|
||||||
|
|
|
||||||
|
|
@ -88,15 +88,19 @@ export default apiRoute<{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(
|
try {
|
||||||
encode(
|
resolve(
|
||||||
new Uint8ClampedArray(buffer),
|
encode(
|
||||||
metadata?.width ?? 0,
|
new Uint8ClampedArray(buffer),
|
||||||
metadata?.height ?? 0,
|
metadata?.width ?? 0,
|
||||||
4,
|
metadata?.height ?? 0,
|
||||||
4,
|
4,
|
||||||
) as string,
|
4,
|
||||||
);
|
) as string,
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
}))();
|
}))();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue