diff --git a/src/config/attachment.config.ts b/src/config/attachment.config.ts new file mode 100644 index 0000000..739b1c6 --- /dev/null +++ b/src/config/attachment.config.ts @@ -0,0 +1,10 @@ +import { join } from "path"; + +export function resolveAttachmentsRoot() { + const explicit = process.env.ATTACHMENTS_ROOT?.trim(); + if (explicit) return explicit; //direct filepath if possible + + const id = (process.env.ATTACHMENTS_SERVER_ID ?? 'server').trim(); + return process.platform === 'win32' ? `\\\\${id}\\attachments` : `/mnt/attachments`; //check if server is using windows or linux +} +export const ATT_TMP_DIR = () => join(resolveAttachmentsRoot(), '_tmp'); \ No newline at end of file diff --git a/src/config/attachment.fs.ts b/src/config/attachment.fs.ts new file mode 100644 index 0000000..8f8ebd3 --- /dev/null +++ b/src/config/attachment.fs.ts @@ -0,0 +1,10 @@ +import { promises as fs } from "node:fs"; +import { ATT_TMP_DIR } from "./attachment.config"; + +export async function ensureAttachmentsTmpDir() { + const tmp = ATT_TMP_DIR(); ///_tmp + await fs.mkdir(tmp, { recursive: true }); // create if missing + + + return tmp; +} \ No newline at end of file diff --git a/src/config/attachment.provider.ts b/src/config/attachment.provider.ts new file mode 100644 index 0000000..d145157 --- /dev/null +++ b/src/config/attachment.provider.ts @@ -0,0 +1,8 @@ +import { Provider } from "@nestjs/common"; +import { resolveAttachmentsRoot } from "./attachment.config"; +export const ATTACHMENTS_ROOT = Symbol('ATTACHMENTS_ROOT'); + +export const attachmentsRootProvider: Provider = { + provide: ATTACHMENTS_ROOT, + useFactory: () => resolveAttachmentsRoot(), +}; \ No newline at end of file diff --git a/src/config/config.module.ts b/src/config/config.module.ts new file mode 100644 index 0000000..2c0ebd9 --- /dev/null +++ b/src/config/config.module.ts @@ -0,0 +1,8 @@ +import { attachmentsRootProvider } from "./attachment.provider"; +import { Module } from "@nestjs/common"; + +@Module({ + providers: [attachmentsRootProvider], + exports: [attachmentsRootProvider], +}) +export class AppConfigModule {} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 10deed9..504ffe1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,11 @@ import * as nodeCrypto from 'crypto'; if(!(globalThis as any).crypto) { (globalThis as any).crypto = nodeCrypto; } +import { ensureAttachmentsTmpDir } from './config/attachment.fs'; + +import { resolveAttachmentsRoot } from './config/attachment.config';// log to be removed post dev +import { ATT_TMP_DIR } from './config/attachment.config'; // log to be removed post dev + import { ModuleRef, NestFactory, Reflector } from '@nestjs/core'; import { AppModule } from './app.module'; import { ValidationPipe } from '@nestjs/common'; @@ -76,9 +81,14 @@ async function bootstrap() { const documentFactory = () => SwaggerModule.createDocument(app, config); const document = documentFactory() SwaggerModule.setup('api/docs', app, document); - writeFileSync('./docs/swagger/swagger-spec.json', JSON.stringify(document, null, 2)); - + + // logs to be removed post dev + console.log('[ENV.ATTACHMENTS_ROOT]', process.env.ATTACHMENTS_ROOT); + console.log('[resolveAttachmentsRoot()]', resolveAttachmentsRoot()); + console.log('[ATT_TMP_DIR()]', ATT_TMP_DIR()); + + await ensureAttachmentsTmpDir(); await app.listen(process.env.PORT ?? 3000); } bootstrap();