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();
|
const fileManager = sails.hooks['file-manager'].getInstance();
|
||||||
|
|
||||||
let readStream;
|
let readStream;
|
||||||
|
let headers;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
readStream = await fileManager.read(
|
[readStream, headers] = await fileManager.read(
|
||||||
`${sails.config.custom.attachmentsPathSegment}/${attachment.data.uploadedFileId}/thumbnails/${inputs.fileName}.${inputs.fileExtension}`,
|
`${sails.config.custom.attachmentsPathSegment}/${attachment.data.uploadedFileId}/thumbnails/${inputs.fileName}.${inputs.fileExtension}`,
|
||||||
|
{
|
||||||
|
withHeaders: true,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw Errors.FILE_ATTACHMENT_NOT_FOUND;
|
throw Errors.FILE_ATTACHMENT_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.res.type(attachment.data.mimeType);
|
this.res.set({
|
||||||
this.res.set('Cache-Control', 'private, max-age=86400, no-transform'); // TODO: move to config
|
...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);
|
return exits.success(readStream);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -70,21 +70,38 @@ module.exports = {
|
|||||||
const fileManager = sails.hooks['file-manager'].getInstance();
|
const fileManager = sails.hooks['file-manager'].getInstance();
|
||||||
|
|
||||||
let readStream;
|
let readStream;
|
||||||
|
let headers;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
readStream = await fileManager.read(
|
[readStream, headers] = await fileManager.read(
|
||||||
`${sails.config.custom.attachmentsPathSegment}/${attachment.data.uploadedFileId}/${attachment.data.filename}`,
|
`${sails.config.custom.attachmentsPathSegment}/${attachment.data.uploadedFileId}/${attachment.data.filename}`,
|
||||||
|
{
|
||||||
|
withHeaders: true,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw Errors.FILE_ATTACHMENT_NOT_FOUND;
|
throw Errors.FILE_ATTACHMENT_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attachment.data.mimeType) {
|
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) {
|
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);
|
return exits.success(readStream);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user