mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
Media upload for avatars and banners, more work, fix tests
This commit is contained in:
parent
16cfd5d900
commit
460b68c381
17 changed files with 360 additions and 139 deletions
|
|
@ -37,4 +37,6 @@ describe("Instance", () => {
|
|||
|
||||
afterAll(async () => {
|
||||
await instance.remove();
|
||||
|
||||
await AppDataSource.destroy();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,17 +1,27 @@
|
|||
import { getConfig } from "@config";
|
||||
import { ConfigType, 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();
|
||||
const originalConfig = getConfig();
|
||||
const modifiedConfig: ConfigType = {
|
||||
...originalConfig,
|
||||
media: {
|
||||
...originalConfig.media,
|
||||
conversion: {
|
||||
...originalConfig.media.conversion,
|
||||
convert_images: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
describe("LocalBackend", () => {
|
||||
let localBackend: LocalBackend;
|
||||
let fileName: string;
|
||||
|
||||
beforeAll(() => {
|
||||
localBackend = new LocalBackend();
|
||||
localBackend = new LocalBackend(modifiedConfig);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
|
@ -25,7 +35,7 @@ describe("LocalBackend", () => {
|
|||
});
|
||||
|
||||
const hash = await localBackend.addMedia(media);
|
||||
fileName = `${hash}`;
|
||||
fileName = hash;
|
||||
|
||||
expect(hash).toBeDefined();
|
||||
});
|
||||
|
|
@ -33,16 +43,14 @@ describe("LocalBackend", () => {
|
|||
|
||||
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");
|
||||
const media = await localBackend.getMediaByHash(fileName);
|
||||
|
||||
expect(media).toBeInstanceOf(File);
|
||||
});
|
||||
|
||||
it("should return null if the file does not exist", async () => {
|
||||
const media = await localBackend.getMediaByHash(
|
||||
"does-not-exist",
|
||||
"txt"
|
||||
);
|
||||
const media =
|
||||
await localBackend.getMediaByHash("does-not-exist.txt");
|
||||
|
||||
expect(media).toBeNull();
|
||||
});
|
||||
|
|
@ -50,12 +58,12 @@ describe("LocalBackend", () => {
|
|||
});
|
||||
|
||||
describe("S3Backend", () => {
|
||||
const s3Backend = new S3Backend(config);
|
||||
const s3Backend = new S3Backend(modifiedConfig);
|
||||
let fileName: string;
|
||||
|
||||
afterAll(async () => {
|
||||
const command = new DeleteObjectCommand({
|
||||
Bucket: config.s3.bucket_name,
|
||||
Bucket: modifiedConfig.s3.bucket_name,
|
||||
Key: fileName,
|
||||
});
|
||||
|
||||
|
|
@ -69,7 +77,7 @@ describe("S3Backend", () => {
|
|||
});
|
||||
|
||||
const hash = await s3Backend.addMedia(media);
|
||||
fileName = `${hash}`;
|
||||
fileName = hash;
|
||||
|
||||
expect(hash).toBeDefined();
|
||||
});
|
||||
|
|
@ -77,16 +85,13 @@ describe("S3Backend", () => {
|
|||
|
||||
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");
|
||||
const media = await s3Backend.getMediaByHash(fileName);
|
||||
|
||||
expect(media).toBeInstanceOf(File);
|
||||
});
|
||||
|
||||
it("should return null if the file does not exist", async () => {
|
||||
const media = await s3Backend.getMediaByHash(
|
||||
"does-not-exist",
|
||||
"txt"
|
||||
);
|
||||
const media = await s3Backend.getMediaByHash("does-not-exist.txt");
|
||||
|
||||
expect(media).toBeNull();
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue