Browse Source

Merge pull request #6761 from nocodb/fix/attachments-old

fix: use sendFile for old apis
nc-fix/date-picker
Raju Udava 1 year ago committed by GitHub
parent
commit
257624efda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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 { try {
const fpath = await PresignedUrl.getPath(`dltemp/${param}`); 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), path: path.join('nc', 'uploads', fpath),
}); });
res.sendFile(img); res.sendFile(file.path);
} catch (e) { } catch (e) {
res.status(404).send('Not found'); 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 // This route will match any URL that starts with
async fileRead(@Param('filename') filename: string, @Response() res) { async fileRead(@Param('filename') filename: string, @Response() res) {
try { try {
const { img, type } = await this.attachmentsService.fileRead({ const file = await this.attachmentsService.getFile({
path: path.join('nc', 'uploads', filename), path: path.join('nc', 'uploads', filename),
}); });
res.writeHead(200, { 'Content-Type': type }); res.sendFile(file.path);
res.end(img, 'binary');
} catch (e) { } catch (e) {
console.log(e);
res.status(404).send('Not found'); res.status(404).send('Not found');
} }
} }
@ -82,7 +80,7 @@ export class AttachmentsController {
@Response() res, @Response() res,
) { ) {
try { try {
const { img, type } = await this.attachmentsService.fileRead({ const file = await this.attachmentsService.getFile({
path: path.join( path: path.join(
'nc', 'nc',
param1, param1,
@ -92,8 +90,7 @@ export class AttachmentsController {
), ),
}); });
res.writeHead(200, { 'Content-Type': type }); res.sendFile(file.path);
res.end(img, 'binary');
} catch (e) { } catch (e) {
res.status(404).send('Not found'); res.status(404).send('Not found');
} }
@ -104,11 +101,11 @@ export class AttachmentsController {
try { try {
const fpath = await PresignedUrl.getPath(`dltemp/${param}`); 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), path: path.join('nc', 'uploads', fpath),
}); });
res.sendFile(img); res.sendFile(file.path);
} catch (e) { } catch (e) {
res.status(404).send('Not found'); res.status(404).send('Not found');
} }

9
packages/nocodb/src/services/attachments.service.ts

@ -147,18 +147,21 @@ export class AttachmentsService {
return attachments; 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 // get the local storage adapter to display local attachments
const storageAdapter = new Local(); const storageAdapter = new Local();
const type = const type =
mimetypes[path.extname(param.path).split('/').pop().slice(1)] || mimetypes[path.extname(param.path).split('/').pop().slice(1)] ||
'text/plain'; 'text/plain';
const img = await storageAdapter.validateAndNormalisePath( const filePath = await storageAdapter.validateAndNormalisePath(
slash(param.path), slash(param.path),
true, true,
); );
return { img, type }; return { path: filePath, type };
} }
sanitizeUrlPath(paths) { sanitizeUrlPath(paths) {

Loading…
Cancel
Save