feat(attachments): base config for attachment management.

This commit is contained in:
Matthieu Haineault 2025-08-14 16:54:11 -04:00
parent 45386ac4bf
commit 9d3967c5c7
5 changed files with 48 additions and 2 deletions

View File

@ -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');

View File

@ -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(); //<ROOT>/_tmp
await fs.mkdir(tmp, { recursive: true }); // create if missing
return tmp;
}

View File

@ -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(),
};

View File

@ -0,0 +1,8 @@
import { attachmentsRootProvider } from "./attachment.provider";
import { Module } from "@nestjs/common";
@Module({
providers: [attachmentsRootProvider],
exports: [attachmentsRootProvider],
})
export class AppConfigModule {}

View File

@ -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();