Browse Source

fix: use sendFile for old apis

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/6761/head
mertmit 1 year ago
parent
commit
1b299ab8b2
  1. 4
      packages/nocodb/src/controllers/attachments-secure.controller.ts
  2. 15
      packages/nocodb/src/controllers/attachments.controller.ts
  3. 9
      packages/nocodb/src/services/attachments.service.ts

4
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');
}

15
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');
}

9
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) {

Loading…
Cancel
Save