From d1c0d506e5d45be3df8a8b8420fe35922dccbad8 Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Mon, 4 Sep 2023 23:53:38 +0530 Subject: [PATCH] fix: move root dir creation under plugin as a util --- plugins/storage/server/api/files.ts | 3 +++ plugins/storage/server/utils.ts | 20 ++++++++++++++++++++ server/index.ts | 7 ------- 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 plugins/storage/server/utils.ts diff --git a/plugins/storage/server/api/files.ts b/plugins/storage/server/api/files.ts index 08513307bc..c70f226557 100644 --- a/plugins/storage/server/api/files.ts +++ b/plugins/storage/server/api/files.ts @@ -23,8 +23,11 @@ import { authorize } from "@server/policies"; import { APIContext } from "@server/types"; import { getAttachmentForJWT } from "@server/utils/jwt"; import { getFileFromRequest } from "@server/utils/koa"; +import { createRootDirForLocalStorage } from "../utils"; import * as T from "./schema"; +createRootDirForLocalStorage(); + const router = new Router(); router.post( diff --git a/plugins/storage/server/utils.ts b/plugins/storage/server/utils.ts new file mode 100644 index 0000000000..a96465fbbf --- /dev/null +++ b/plugins/storage/server/utils.ts @@ -0,0 +1,20 @@ +import { existsSync, mkdirSync } from "fs"; +import env from "@server/env"; +import Logger from "@server/logging/Logger"; + +export const createRootDirForLocalStorage = () => { + if (env.FILE_STORAGE === "local") { + const rootDir = env.FILE_STORAGE_LOCAL_ROOT; + try { + if (!existsSync(rootDir)) { + mkdirSync(rootDir); + Logger.debug("utils", `Created ${rootDir} for local storage`); + } + } catch (err) { + Logger.fatal( + "Couldn't create root dir for local storage of attachments", + err + ); + } + } +}; diff --git a/server/index.ts b/server/index.ts index f54c065994..781410bb54 100644 --- a/server/index.ts +++ b/server/index.ts @@ -26,7 +26,6 @@ import ShutdownHelper, { ShutdownOrder } from "./utils/ShutdownHelper"; import { sequelize } from "./storage/database"; import RedisAdapter from "./storage/redis"; import Metrics from "./logging/Metrics"; -import { mkdirSync, existsSync } from "fs"; // The default is to run all services to make development and OSS installations // easier to deal with. Separate services are only needed at scale. @@ -52,12 +51,6 @@ if (serviceNames.includes("collaboration")) { // This function will only be called once in the original process async function master() { await checkEnv(); - if (env.FILE_STORAGE === "local") { - const rootDir = env.FILE_STORAGE_LOCAL_ROOT; - if (!existsSync(rootDir)) { - mkdirSync(rootDir); - } - } await checkPendingMigrations(); if (env.TELEMETRY && env.ENVIRONMENT === "production") {