feat: Re-stream static files from S3, introduce protected static files
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const mime = require('mime-types');
|
||||
const {
|
||||
CopyObjectCommand,
|
||||
DeleteObjectsCommand,
|
||||
@@ -46,13 +47,28 @@ class S3FileManager {
|
||||
await upload.done();
|
||||
}
|
||||
|
||||
async read(filePathSegment) {
|
||||
async read(filePathSegment, { withHeaders = false } = {}) {
|
||||
const command = new GetObjectCommand({
|
||||
Bucket: sails.config.custom.s3Bucket,
|
||||
Key: filePathSegment,
|
||||
});
|
||||
|
||||
const result = await this.client.send(command);
|
||||
|
||||
if (withHeaders) {
|
||||
return [
|
||||
result.Body,
|
||||
{
|
||||
'Content-Type':
|
||||
result.ContentType || mime.lookup(filePathSegment) || 'application/octet-stream',
|
||||
'Content-Length': result.ContentLength,
|
||||
'Last-Modified': result.LastModified && result.LastModified.toUTCString(),
|
||||
ETag: result.ETag,
|
||||
'Accept-Ranges': result.AcceptRanges || 'bytes',
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
return result.Body;
|
||||
}
|
||||
|
||||
@@ -196,10 +212,10 @@ class S3FileManager {
|
||||
return !!result.Contents && result.Contents.length === 1;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
/* // eslint-disable-next-line class-methods-use-this
|
||||
buildUrl(filePathSegment) {
|
||||
return `${sails.hooks.s3.getBaseUrl()}/${filePathSegment}`;
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
module.exports = S3FileManager;
|
||||
|
||||
Reference in New Issue
Block a user