fix: Enhance response headers for file attachments
This commit is contained in:
@@ -77,16 +77,32 @@ module.exports = {
|
||||
const fileManager = sails.hooks['file-manager'].getInstance();
|
||||
|
||||
let readStream;
|
||||
let headers;
|
||||
|
||||
try {
|
||||
readStream = await fileManager.read(
|
||||
[readStream, headers] = await fileManager.read(
|
||||
`${sails.config.custom.attachmentsPathSegment}/${attachment.data.uploadedFileId}/thumbnails/${inputs.fileName}.${inputs.fileExtension}`,
|
||||
{
|
||||
withHeaders: true,
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
throw Errors.FILE_ATTACHMENT_NOT_FOUND;
|
||||
}
|
||||
|
||||
this.res.type(attachment.data.mimeType);
|
||||
this.res.set('Cache-Control', 'private, max-age=86400, no-transform'); // TODO: move to config
|
||||
this.res.set({
|
||||
...headers,
|
||||
'Content-Type': attachment.data.mimeType,
|
||||
'Cache-Control': 'private, max-age=86400, no-transform', // TODO: move to config
|
||||
});
|
||||
|
||||
readStream.on('error', () => {
|
||||
if (this.res.headersSent) {
|
||||
this.res.destroy();
|
||||
} else {
|
||||
throw Errors.FILE_ATTACHMENT_NOT_FOUND;
|
||||
}
|
||||
});
|
||||
|
||||
return exits.success(readStream);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user