fix(api): 🐛 Deleting emojis now removes them from object storage

This commit is contained in:
Jesse Wierzbinski 2024-05-12 16:52:19 -10:00
parent 7846a03bcf
commit 29d7b09677
No known key found for this signature in database
5 changed files with 119 additions and 11 deletions

View file

@ -1,3 +1,4 @@
import { rm } from "node:fs/promises";
import { S3Client } from "@jsr/bradenmacdonald__s3-lite-client";
import type { Config } from "config-manager";
import { MediaConverter } from "./media-converter";
@ -71,6 +72,12 @@ export class MediaBackend {
);
}
public deleteFileByUrl(url: string): Promise<void> {
return Promise.reject(
new Error("Do not call MediaBackend directly: use a subclass"),
);
}
/**
* Fetches file from backend from filename
* @param filename File name
@ -133,6 +140,24 @@ export class LocalMediaBackend extends MediaBackend {
};
}
public async deleteFileByUrl(url: string) {
// url is of format https://base-url/media/SHA256HASH/FILENAME
const urlO = new URL(url);
const hash = urlO.pathname.split("/")[1];
const dirPath = `${this.config.media.local_uploads_folder}/${hash}`;
try {
await rm(dirPath, { recursive: true });
} catch (e) {
console.error(`Failed to delete directory at ${dirPath}`);
console.error(e);
}
return;
}
public async getFileByHash(
hash: string,
databaseHashFetcher: (sha256: string) => Promise<string | null>,
@ -173,6 +198,18 @@ export class S3MediaBackend extends MediaBackend {
super(config, MediaBackendType.S3);
}
public async deleteFileByUrl(url: string) {
// url is of format https://s3-base-url/SHA256HASH/FILENAME
const urlO = new URL(url);
const hash = urlO.pathname.split("/")[1];
const filename = urlO.pathname.split("/")[2];
await this.s3Client.deleteObject(`${hash}/${filename}`);
return;
}
public async addFile(file: File) {
let convertedFile = file;
if (this.shouldConvertImages(this.config)) {

View file

@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, jest, spyOn } from "bun:test";
import { beforeEach, describe, expect, it, jest, mock, spyOn } from "bun:test";
import type { S3Client } from "@jsr/bradenmacdonald__s3-lite-client";
import type { Config } from "config-manager";
import {
@ -127,6 +127,7 @@ describe("S3MediaBackend", () => {
blob: jest.fn().mockResolvedValue(new Blob()),
headers: new Headers({ "Content-Type": "image/jpeg" }),
}),
deleteObject: jest.fn().mockResolvedValue({}),
} as Partial<S3Client>;
s3MediaBackend = new S3MediaBackend(
mockConfig as Config,
@ -187,6 +188,21 @@ describe("S3MediaBackend", () => {
expect(file?.name).toEqual(mockFilename);
expect(file?.type).toEqual("image/jpeg");
});
it("should delete file", async () => {
// deleteFileByUrl
// Upload file first
const mockHash = "test-hash";
spyOn(mockMediaHasher, "getMediaHash").mockResolvedValue(mockHash);
const result = await s3MediaBackend.addFile(mockFile);
const url = result.path;
await s3MediaBackend.deleteFileByUrl(`http://localhost:4566/${url}`);
expect(mockS3Client.deleteObject).toHaveBeenCalledWith(
expect.stringContaining(url),
);
});
});
describe("LocalMediaBackend", () => {
@ -274,4 +290,28 @@ describe("LocalMediaBackend", () => {
expect(file?.name).toEqual(mockFilename);
expect(file?.type).toEqual("image/jpeg");
});
it("should delete file", async () => {
// deleteByUrl
const mockHash = "test-hash";
spyOn(mockMediaHasher, "getMediaHash").mockResolvedValue(mockHash);
const result = await localMediaBackend.addFile(mockFile);
const rmMock = jest.fn().mockResolvedValue(Promise.resolve());
// Spy on fs/promises rm
mock.module("fs/promises", () => {
return {
rm: rmMock,
};
});
await localMediaBackend.deleteFileByUrl(
"http://localhost:4566/test-hash",
);
expect(rmMock).toHaveBeenCalledWith(
`${mockConfig.media.local_uploads_folder}/${mockHash}`,
{ recursive: true },
);
});
});