Local file storage (#5763)

Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
Apoorv Mishra
2023-09-21 03:42:03 +05:30
committed by GitHub
parent fea50feb0d
commit 67b1fe5514
41 changed files with 893 additions and 139 deletions

84
server/test/TestServer.ts Normal file
View File

@@ -0,0 +1,84 @@
import http from "http";
import { AddressInfo } from "net";
import Koa from "koa";
// eslint-disable-next-line no-restricted-imports
import nodeFetch from "node-fetch";
class TestServer {
private server: http.Server;
private listener?: Promise<void> | null;
constructor(app: Koa) {
this.server = http.createServer(app.callback() as any);
}
get address(): string {
const { port } = this.server.address() as AddressInfo;
return `http://localhost:${port}`;
}
listen() {
if (!this.listener) {
this.listener = new Promise((resolve, reject) => {
this.server
.listen(0, () => resolve())
.on("error", (err) => reject(err));
});
}
return this.listener;
}
fetch(path: string, opts: any) {
return this.listen().then(() => {
const url = `${this.address}${path}`;
const options = Object.assign({ headers: {} }, opts);
const contentType =
options.headers["Content-Type"] ?? options.headers["content-type"];
// automatic JSON encoding
if (!contentType && typeof options.body === "object") {
options.headers["Content-Type"] = "application/json";
options.body = JSON.stringify(options.body);
}
return nodeFetch(url, options);
});
}
close() {
this.listener = null;
return new Promise<void>((resolve, reject) => {
this.server.close((err) => (err ? reject(err) : resolve()));
});
}
delete(path: string, options?: any) {
return this.fetch(path, { ...options, method: "DELETE" });
}
get(path: string, options?: any) {
return this.fetch(path, { ...options, method: "GET" });
}
head(path: string, options?: any) {
return this.fetch(path, { ...options, method: "HEAD" });
}
options(path: string, options?: any) {
return this.fetch(path, { ...options, method: "OPTIONS" });
}
patch(path: string, options?: any) {
return this.fetch(path, { ...options, method: "PATCH" });
}
post(path: string, options?: any) {
return this.fetch(path, { ...options, method: "POST" });
}
put(path: string, options?: any) {
return this.fetch(path, { ...options, method: "PUT" });
}
}
export default TestServer;

View File

@@ -18,6 +18,8 @@ env.OIDC_USERINFO_URI = "http://localhost/userinfo";
env.RATE_LIMITER_ENABLED = false;
env.FILE_STORAGE = "local";
env.FILE_STORAGE_LOCAL_ROOT_DIR = "/tmp";
env.IFRAMELY_API_KEY = "123";
if (process.env.DATABASE_URL_TEST) {

View File

@@ -33,6 +33,7 @@ import {
SearchQuery,
Pin,
} from "@server/models";
import AttachmentHelper from "@server/models/helpers/AttachmentHelper";
export async function buildApiKey(overrides: Partial<ApiKey> = {}) {
if (!overrides.userId) {
@@ -420,7 +421,10 @@ export async function buildFileOperation(
});
}
export async function buildAttachment(overrides: Partial<Attachment> = {}) {
export async function buildAttachment(
overrides: Partial<Attachment> = {},
fileName?: string
) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
@@ -441,11 +445,14 @@ export async function buildAttachment(overrides: Partial<Attachment> = {}) {
overrides.documentId = document.id;
}
const id = uuidv4();
const acl = overrides.acl || "public-read";
const name = fileName || faker.system.fileName();
return Attachment.create({
key: `uploads/key/to/${faker.system.fileName}.png`,
key: AttachmentHelper.getKey({ acl, id, name, userId: overrides.userId }),
contentType: "image/png",
size: 100,
acl: "public-read",
acl,
createdAt: new Date("2018-01-02T00:00:00.000Z"),
updatedAt: new Date("2018-01-02T00:00:00.000Z"),
...overrides,

View File

@@ -1,22 +1,22 @@
import { faker } from "@faker-js/faker";
import TestServer from "fetch-test-server";
import sharedEnv from "@shared/env";
import env from "@server/env";
import onerror from "@server/onerror";
import webService from "@server/services/web";
import { sequelize } from "@server/storage/database";
import TestServer from "./TestServer";
export function getTestServer() {
const app = webService();
onerror(app);
const server = new TestServer(app.callback());
const server = new TestServer(app);
server.disconnect = async () => {
const disconnect = async () => {
await sequelize.close();
server.close();
return server.close();
};
afterAll(server.disconnect);
afterAll(disconnect);
return server;
}