From ec09458ad35cea374c05ea38f1c77072ac657514 Mon Sep 17 00:00:00 2001 From: diced Date: Sat, 13 May 2023 00:12:29 -0700 Subject: [PATCH] feat: allow full paths (#393) --- src/lib/config/validateConfig.ts | 4 ++-- src/lib/datasources/Local.ts | 12 ++++++------ src/worker/upload.ts | 1 - 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/lib/config/validateConfig.ts b/src/lib/config/validateConfig.ts index cbc0f99..bd6b128 100644 --- a/src/lib/config/validateConfig.ts +++ b/src/lib/config/validateConfig.ts @@ -4,7 +4,7 @@ import { inspect } from 'util'; import Logger from 'lib/logger'; import { humanToBytes } from 'utils/bytes'; import { tmpdir } from 'os'; -import { join } from 'path'; +import { join, resolve } from 'path'; const discord_content = s .object({ @@ -53,7 +53,7 @@ const validator = s.object({ type: s.enum('local', 's3', 'supabase').default('local'), local: s .object({ - directory: s.string.default('./uploads'), + directory: s.string.default(resolve('./uploads')).transform((v) => resolve(v)), }) .default({ directory: './uploads', diff --git a/src/lib/datasources/Local.ts b/src/lib/datasources/Local.ts index bfeb4f6..19e38f5 100644 --- a/src/lib/datasources/Local.ts +++ b/src/lib/datasources/Local.ts @@ -11,23 +11,23 @@ export class Local extends Datasource { } public async save(file: string, data: Buffer): Promise { - await writeFile(join(process.cwd(), this.path, file), data); + await writeFile(join(this.path, file), data); } public async delete(file: string): Promise { - await rm(join(process.cwd(), this.path, file)); + await rm(join(this.path, file)); } public async clear(): Promise { - const files = await readdir(join(process.cwd(), this.path)); + const files = await readdir(this.path); for (let i = 0; i !== files.length; ++i) { - await rm(join(process.cwd(), this.path, files[i])); + await rm(join(this.path, files[i])); } } public get(file: string): ReadStream { - const full = join(process.cwd(), this.path, file); + const full = join(this.path, file); if (!existsSync(full)) return null; try { @@ -38,7 +38,7 @@ export class Local extends Datasource { } public async size(file: string): Promise { - const full = join(process.cwd(), this.path, file); + const full = join(this.path, file); if (!existsSync(full)) return 0; const stats = await stat(full); diff --git a/src/worker/upload.ts b/src/worker/upload.ts index d6b26b1..8cbebed 100644 --- a/src/worker/upload.ts +++ b/src/worker/upload.ts @@ -84,7 +84,6 @@ async function start() { if (config.datasource.type === 'local') { fd = await open( join( - process.cwd(), config.datasource.local.directory, `${fileName}${compressionUsed ? '.jpg' : `${ext ? '.' : ''}${ext}`}` ),