From 2c5558ab3bd71976b81feccb5199008dfb32f5de Mon Sep 17 00:00:00 2001 From: mertmit Date: Mon, 30 Sep 2024 07:28:09 +0000 Subject: [PATCH] fix: avoid using existsSync --- .../src/controllers/attachments-secure.controller.ts | 3 ++- packages/nocodb/src/controllers/attachments.controller.ts | 8 ++++---- packages/nocodb/src/helpers/attachmentHelpers.ts | 5 +++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/nocodb/src/controllers/attachments-secure.controller.ts b/packages/nocodb/src/controllers/attachments-secure.controller.ts index f9cfaee3b0..d9394138ef 100644 --- a/packages/nocodb/src/controllers/attachments-secure.controller.ts +++ b/packages/nocodb/src/controllers/attachments-secure.controller.ts @@ -29,6 +29,7 @@ import { DataApiLimiterGuard } from '~/guards/data-api-limiter.guard'; import { TenantContext } from '~/decorators/tenant-context.decorator'; import { Acl } from '~/middlewares/extract-ids/extract-ids.middleware'; import { NcError } from '~/helpers/catchError'; +import { fileExists } from '~/helpers/attachmentHelpers' @Controller() export class AttachmentsSecureController { @@ -95,7 +96,7 @@ export class AttachmentsSecureController { path: path.join('nc', filePath, fpath), }); - if (!fs.existsSync(file.path)) { + if (!(await fileExists(file.path))) { return res.status(404).send('File not found'); } diff --git a/packages/nocodb/src/controllers/attachments.controller.ts b/packages/nocodb/src/controllers/attachments.controller.ts index 5426667eff..6190140116 100644 --- a/packages/nocodb/src/controllers/attachments.controller.ts +++ b/packages/nocodb/src/controllers/attachments.controller.ts @@ -24,7 +24,7 @@ import { AttachmentsService } from '~/services/attachments.service'; import { PresignedUrl } from '~/models'; import { MetaApiLimiterGuard } from '~/guards/meta-api-limiter.guard'; import { NcContext, NcRequest } from '~/interface/config'; -import { isPreviewAllowed } from '~/helpers/attachmentHelpers'; +import { isPreviewAllowed, fileExists } from '~/helpers/attachmentHelpers'; import { DataTableService } from '~/services/data-table.service'; import { TenantContext } from '~/decorators/tenant-context.decorator'; import { DataApiLimiterGuard } from '~/guards/data-api-limiter.guard'; @@ -85,7 +85,7 @@ export class AttachmentsController { path: path.join('nc', 'uploads', filename), }); - if (!fs.existsSync(file.path)) { + if (!await fs.promises.stat(file.path)) { return res.status(404).send('File not found'); } @@ -126,7 +126,7 @@ export class AttachmentsController { ), }); - if (!fs.existsSync(file.path)) { + if (!(await fileExists(file.path))) { return res.status(404).send('File not found'); } @@ -172,7 +172,7 @@ export class AttachmentsController { path: path.join('nc', filePath, fpath), }); - if (!fs.existsSync(file.path)) { + if (!(await fileExists(file.path))) { return res.status(404).send('File not found'); } diff --git a/packages/nocodb/src/helpers/attachmentHelpers.ts b/packages/nocodb/src/helpers/attachmentHelpers.ts index bb3cfb9bc8..a405bb358f 100644 --- a/packages/nocodb/src/helpers/attachmentHelpers.ts +++ b/packages/nocodb/src/helpers/attachmentHelpers.ts @@ -1,4 +1,5 @@ import path from 'path'; +import fs from 'fs'; import mime from 'mime/lite'; import slash from 'slash'; import { getToolDir } from '~/utils/nc-config'; @@ -64,3 +65,7 @@ export function getPathFromUrl(url: string, removePrefix = false) { return decodeURI(`${pathName}${newUrl.search}${newUrl.hash}`); } + +export const fileExists = (path: string) => { + return fs.promises.access(path).then(() => true).catch(() => false); +}