fix: Enhance response headers for file attachments

This commit is contained in:
Maksim Eltyshev
2026-01-30 21:53:53 +01:00
parent db99227f32
commit 6335b3bd3c
2 changed files with 40 additions and 7 deletions

View File

@@ -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);
},

View File

@@ -70,21 +70,38 @@ 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}/${attachment.data.filename}`,
{
withHeaders: true,
},
);
} catch (error) {
throw Errors.FILE_ATTACHMENT_NOT_FOUND;
}
if (attachment.data.mimeType) {
this.res.type(attachment.data.mimeType);
headers['Content-Type'] = attachment.data.mimeType;
}
if (!INLINE_MIME_TYPES_SET.has(attachment.data.mimeType) && !attachment.data.image) {
this.res.set('Content-Disposition', 'attachment');
headers['Content-Disposition'] = 'attachment';
}
this.res.set('Cache-Control', 'private, max-age=86400, no-transform'); // TODO: move to config
this.res.set({
...headers,
'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);
},