From 07b360c52a0009505f0b263f9e5e5646f095bf0b Mon Sep 17 00:00:00 2001 From: mertmit Date: Mon, 30 Sep 2024 07:28:09 +0000 Subject: [PATCH] fix: PR requested change & refactor helper name --- .../src/controllers/attachments-secure.controller.ts | 5 ++--- .../nocodb/src/controllers/attachments.controller.ts | 9 ++++----- packages/nocodb/src/helpers/attachmentHelpers.ts | 9 ++++++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/nocodb/src/controllers/attachments-secure.controller.ts b/packages/nocodb/src/controllers/attachments-secure.controller.ts index d9394138ef..22bd1601d6 100644 --- a/packages/nocodb/src/controllers/attachments-secure.controller.ts +++ b/packages/nocodb/src/controllers/attachments-secure.controller.ts @@ -1,5 +1,4 @@ import path from 'path'; -import fs from 'fs'; import { Body, Controller, @@ -29,7 +28,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' +import { localFileExists } from '~/helpers/attachmentHelpers'; @Controller() export class AttachmentsSecureController { @@ -96,7 +95,7 @@ export class AttachmentsSecureController { path: path.join('nc', filePath, fpath), }); - if (!(await fileExists(file.path))) { + if (!(await localFileExists(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 6190140116..087bff0b55 100644 --- a/packages/nocodb/src/controllers/attachments.controller.ts +++ b/packages/nocodb/src/controllers/attachments.controller.ts @@ -1,5 +1,4 @@ import path from 'path'; -import fs from 'fs'; import { Body, Controller, @@ -24,7 +23,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, fileExists } from '~/helpers/attachmentHelpers'; +import { isPreviewAllowed, localFileExists } 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 +84,7 @@ export class AttachmentsController { path: path.join('nc', 'uploads', filename), }); - if (!await fs.promises.stat(file.path)) { + if (!(await localFileExists(file.path))) { return res.status(404).send('File not found'); } @@ -126,7 +125,7 @@ export class AttachmentsController { ), }); - if (!(await fileExists(file.path))) { + if (!(await localFileExists(file.path))) { return res.status(404).send('File not found'); } @@ -172,7 +171,7 @@ export class AttachmentsController { path: path.join('nc', filePath, fpath), }); - if (!(await fileExists(file.path))) { + if (!(await localFileExists(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 a405bb358f..3f5c3d5496 100644 --- a/packages/nocodb/src/helpers/attachmentHelpers.ts +++ b/packages/nocodb/src/helpers/attachmentHelpers.ts @@ -66,6 +66,9 @@ 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); -} +export const localFileExists = (path: string) => { + return fs.promises + .access(path) + .then(() => true) + .catch(() => false); +};