diff --git a/packages/nocodb/src/controllers/attachments-secure.controller.ts b/packages/nocodb/src/controllers/attachments-secure.controller.ts index dc04954946..40b6a6ebf6 100644 --- a/packages/nocodb/src/controllers/attachments-secure.controller.ts +++ b/packages/nocodb/src/controllers/attachments-secure.controller.ts @@ -60,11 +60,11 @@ export class AttachmentsSecureController { try { const fpath = await PresignedUrl.getPath(`dltemp/${param}`); - const { img } = await this.attachmentsService.fileRead({ + const file = await this.attachmentsService.getFile({ path: path.join('nc', 'uploads', fpath), }); - res.sendFile(img); + res.sendFile(file.path); } catch (e) { res.status(404).send('Not found'); } diff --git a/packages/nocodb/src/controllers/attachments.controller.ts b/packages/nocodb/src/controllers/attachments.controller.ts index 8b75a228d5..e5d4caf4de 100644 --- a/packages/nocodb/src/controllers/attachments.controller.ts +++ b/packages/nocodb/src/controllers/attachments.controller.ts @@ -60,14 +60,12 @@ export class AttachmentsController { // This route will match any URL that starts with async fileRead(@Param('filename') filename: string, @Response() res) { try { - const { img, type } = await this.attachmentsService.fileRead({ + const file = await this.attachmentsService.getFile({ path: path.join('nc', 'uploads', filename), }); - res.writeHead(200, { 'Content-Type': type }); - res.end(img, 'binary'); + res.sendFile(file.path); } catch (e) { - console.log(e); res.status(404).send('Not found'); } } @@ -82,7 +80,7 @@ export class AttachmentsController { @Response() res, ) { try { - const { img, type } = await this.attachmentsService.fileRead({ + const file = await this.attachmentsService.getFile({ path: path.join( 'nc', param1, @@ -92,8 +90,7 @@ export class AttachmentsController { ), }); - res.writeHead(200, { 'Content-Type': type }); - res.end(img, 'binary'); + res.sendFile(file.path); } catch (e) { res.status(404).send('Not found'); } @@ -104,11 +101,11 @@ export class AttachmentsController { try { const fpath = await PresignedUrl.getPath(`dltemp/${param}`); - const { img } = await this.attachmentsService.fileRead({ + const file = await this.attachmentsService.getFile({ path: path.join('nc', 'uploads', fpath), }); - res.sendFile(img); + res.sendFile(file.path); } catch (e) { res.status(404).send('Not found'); } diff --git a/packages/nocodb/src/services/attachments.service.ts b/packages/nocodb/src/services/attachments.service.ts index c8e835e61f..2b9ba3eed6 100644 --- a/packages/nocodb/src/services/attachments.service.ts +++ b/packages/nocodb/src/services/attachments.service.ts @@ -146,18 +146,21 @@ export class AttachmentsService { return attachments; } - async fileRead(param: { path: string }) { + async getFile(param: { path: string }): Promise<{ + path: string; + type: string; + }> { // get the local storage adapter to display local attachments const storageAdapter = new Local(); const type = mimetypes[path.extname(param.path).split('/').pop().slice(1)] || 'text/plain'; - const img = await storageAdapter.validateAndNormalisePath( + const filePath = await storageAdapter.validateAndNormalisePath( slash(param.path), true, ); - return { img, type }; + return { path: filePath, type }; } sanitizeUrlPath(paths) {