Skip to content

Commit

Permalink
Merge pull request #7162 from nocodb/fix/filter-preview
Browse files Browse the repository at this point in the history
fix: filter mimetypes to preview in browser
  • Loading branch information
pranavxc committed Dec 6, 2023
2 parents 6cc4a1c + 3dc46bd commit 6222549
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export class AttachmentsSecureController {
path: path.join('nc', 'uploads', fpath),
});

res.sendFile(file.path);
if (this.attachmentsService.previewAvailable(file.type)) {
res.sendFile(file.path);
} else {
res.download(file.path);
}
} catch (e) {
res.status(404).send('Not found');
}
Expand Down
18 changes: 15 additions & 3 deletions packages/nocodb/src/controllers/attachments.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ export class AttachmentsController {
path: path.join('nc', 'uploads', filename),
});

res.sendFile(file.path);
if (this.attachmentsService.previewAvailable(file.type)) {
res.sendFile(file.path);
} else {
res.download(file.path);
}
} catch (e) {
res.status(404).send('Not found');
}
Expand All @@ -94,7 +98,11 @@ export class AttachmentsController {
),
});

res.sendFile(file.path);
if (this.attachmentsService.previewAvailable(file.type)) {
res.sendFile(file.path);
} else {
res.download(file.path);
}
} catch (e) {
res.status(404).send('Not found');
}
Expand All @@ -109,7 +117,11 @@ export class AttachmentsController {
path: path.join('nc', 'uploads', fpath),
});

res.sendFile(file.path);
if (this.attachmentsService.previewAvailable(file.type)) {
res.sendFile(file.path);
} else {
res.download(file.path);
}
} catch (e) {
res.status(404).send('Not found');
}
Expand Down
8 changes: 8 additions & 0 deletions packages/nocodb/src/services/attachments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ export class AttachmentsService {
return { path: filePath, type };
}

previewAvailable(mimetype: string) {
const available = ['image', 'pdf', 'text/plain'];
if (available.some((type) => mimetype.includes(type))) {
return true;
}
return false;
}

sanitizeUrlPath(paths) {
return paths.map((url) => url.replace(/[/.?#]+/g, '_'));
}
Expand Down

0 comments on commit 6222549

Please sign in to comment.