Add more cases for media backend

This commit is contained in:
Jesse Wierzbinski 2023-10-17 14:57:47 -10:00
parent 47a53b6990
commit 16cfd5d900
No known key found for this signature in database
GPG key ID: F9A1E418934E40B0
6 changed files with 1042 additions and 711 deletions

3
.gitignore vendored
View file

@ -167,4 +167,5 @@ dist
.yarn/build-state.yml .yarn/build-state.yml
.yarn/install-state.gz .yarn/install-state.gz
.pnp.\* .pnp.\*
config/config.toml config/config.toml
uploads/

BIN
bun.lockb

Binary file not shown.

177
classes/media.ts Normal file
View file

@ -0,0 +1,177 @@
import {
GetObjectCommand,
GetObjectCommandOutput,
PutObjectCommand,
S3Client,
} from "@aws-sdk/client-s3";
import { ConfigType } from "@config";
class MediaBackend {
backend: string;
constructor(backend: string) {
this.backend = backend;
}
/**
* Adds media to the media backend
* @param media
* @returns The hash of the file in SHA-256 (hex format)
*/
async addMedia(media: File) {
const hash = new Bun.SHA256()
.update(await media.arrayBuffer())
.digest("hex");
return hash;
}
/**
* Retrieves element from media backend by hash
* @param hash The hash of the element in SHA-256 hex format
* @returns The file as a File object
*/
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
async getMediaByHash(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
hash: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
extension: string
): Promise<File | null> {
return new File([], "test");
}
}
/**
* S3 Backend, stores files in S3
*/
export class S3Backend extends MediaBackend {
endpoint: string;
bucket: string;
region: string;
accessKey: string;
secretKey: string;
publicUrl: string;
client: S3Client;
constructor(config: ConfigType) {
super("s3");
this.endpoint = config.s3.endpoint;
this.bucket = config.s3.bucket_name;
this.region = config.s3.region;
this.accessKey = config.s3.access_key;
this.secretKey = config.s3.secret_access_key;
this.publicUrl = config.s3.public_url;
this.client = new S3Client({
endpoint: this.endpoint,
region: this.region || "auto",
credentials: {
accessKeyId: this.accessKey,
secretAccessKey: this.secretKey,
},
});
}
async addMedia(media: File): Promise<string> {
const hash = await super.addMedia(media);
if (!hash) {
throw new Error("Failed to hash file");
}
// Check if file is already present
const existingFile = await this.getMediaByHash(
hash,
media.name.split(".").pop() || ""
);
if (existingFile) {
// File already exists, so return the hash without uploading it
return hash;
}
const command = new PutObjectCommand({
Bucket: this.bucket,
Key: hash,
Body: Buffer.from(await media.arrayBuffer()),
ContentType: media.type,
ContentLength: media.size,
Metadata: {
"x-amz-meta-original-name": media.name,
},
});
const response = await this.client.send(command);
if (response.$metadata.httpStatusCode !== 200) {
throw new Error("Failed to upload file");
}
return hash;
}
async getMediaByHash(
hash: string,
extension: string
): Promise<File | null> {
const command = new GetObjectCommand({
Bucket: this.bucket,
Key: hash,
});
let response: GetObjectCommandOutput;
try {
response = await this.client.send(command);
} catch {
return null;
}
if (response.$metadata.httpStatusCode !== 200) {
throw new Error("Failed to get file");
}
const body = await response.Body?.transformToByteArray();
if (!body) {
throw new Error("Failed to get file");
}
return new File([body], `${hash}.${extension}`, {
type: response.ContentType,
});
}
}
/**
* Local backend, stores files on filesystem
*/
export class LocalBackend extends MediaBackend {
constructor() {
super("local");
}
async addMedia(media: File): Promise<string> {
const hash = await super.addMedia(media);
await Bun.write(Bun.file(`${process.cwd()}/uploads/${hash}`), media);
return hash;
}
async getMediaByHash(
hash: string,
extension: string
): Promise<File | null> {
const file = Bun.file(`${process.cwd()}/uploads/${hash}`);
if (!(await file.exists())) {
return null;
}
return new File([await file.arrayBuffer()], `${hash}.${extension}`, {
type: file.type,
});
}
}

View file

@ -56,6 +56,7 @@
"typescript": "^5.0.0" "typescript": "^5.0.0"
}, },
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "^3.429.0",
"ip-matching": "^2.1.2", "ip-matching": "^2.1.2",
"isomorphic-dompurify": "^1.9.0", "isomorphic-dompurify": "^1.9.0",
"jsonld": "^8.3.1", "jsonld": "^8.3.1",

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,94 @@
import { getConfig } from "@config";
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import { LocalBackend, S3Backend } from "~classes/media";
import { unlink } from "fs/promises";
import { DeleteObjectCommand } from "@aws-sdk/client-s3";
const config = getConfig();
describe("LocalBackend", () => {
let localBackend: LocalBackend;
let fileName: string;
beforeAll(() => {
localBackend = new LocalBackend();
});
afterAll(async () => {
await unlink(`${process.cwd()}/uploads/${fileName}`);
});
describe("addMedia", () => {
it("should write the file to the local filesystem and return the hash", async () => {
const media = new File(["test"], "test.txt", {
type: "text/plain",
});
const hash = await localBackend.addMedia(media);
fileName = `${hash}`;
expect(hash).toBeDefined();
});
});
describe("getMediaByHash", () => {
it("should retrieve the file from the local filesystem and return it as a File object", async () => {
const media = await localBackend.getMediaByHash(fileName, "txt");
expect(media).toBeInstanceOf(File);
});
it("should return null if the file does not exist", async () => {
const media = await localBackend.getMediaByHash(
"does-not-exist",
"txt"
);
expect(media).toBeNull();
});
});
});
describe("S3Backend", () => {
const s3Backend = new S3Backend(config);
let fileName: string;
afterAll(async () => {
const command = new DeleteObjectCommand({
Bucket: config.s3.bucket_name,
Key: fileName,
});
await s3Backend.client.send(command);
});
describe("addMedia", () => {
it("should write the file to the S3 bucket and return the hash", async () => {
const media = new File(["test"], "test.txt", {
type: "text/plain",
});
const hash = await s3Backend.addMedia(media);
fileName = `${hash}`;
expect(hash).toBeDefined();
});
});
describe("getMediaByHash", () => {
it("should retrieve the file from the S3 bucket and return it as a File object", async () => {
const media = await s3Backend.getMediaByHash(fileName, "txt");
expect(media).toBeInstanceOf(File);
});
it("should return null if the file does not exist", async () => {
const media = await s3Backend.getMediaByHash(
"does-not-exist",
"txt"
);
expect(media).toBeNull();
});
});
});