139 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-02-20 15:57:34 +01:00
'use strict';
/**
* Module dependencies
*/
2018-05-04 18:27:39 +02:00
/* eslint-disable no-unused-vars */
2018-02-20 15:57:34 +01:00
// Public node modules.
2023-03-08 10:44:21 +01:00
const { getOr } = require('lodash/fp');
2018-02-21 14:06:57 +01:00
const AWS = require('aws-sdk');
const { getBucketFromUrl } = require('./utils');
2018-02-20 15:57:34 +01:00
function assertUrlProtocol(url) {
// Regex to test protocol like "http://", "https://"
return /^\w*:\/\//.test(url);
}
2018-02-20 15:57:34 +01:00
module.exports = {
init({ baseUrl = null, rootPath = null, s3Options, ...legacyS3Options }) {
2023-03-20 12:27:33 +01:00
if (legacyS3Options) {
process.emitWarning(
2022-10-25 14:52:17 +02:00
"S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property."
);
2023-03-20 12:27:33 +01:00
}
const config = { ...s3Options, ...legacyS3Options };
2018-02-21 14:06:57 +01:00
const S3 = new AWS.S3({
apiVersion: '2006-03-01',
...config,
2018-02-21 14:06:57 +01:00
});
const filePrefix = rootPath ? `${rootPath.replace(/\/+$/, '')}/` : '';
2023-03-20 12:27:33 +01:00
const getFileKey = (file) => {
const path = file.path ? `${file.path}/` : '';
return `${filePrefix}${path}${file.hash}${file.ext}`;
2023-03-20 12:27:33 +01:00
};
2023-03-08 10:44:21 +01:00
const ACL = getOr('public-read', ['params', 'ACL'], config);
2023-02-14 19:02:00 +01:00
2022-01-05 19:02:04 +01:00
const upload = (file, customParams = {}) =>
new Promise((resolve, reject) => {
// upload file on S3 bucket
const fileKey = getFileKey(file);
2022-01-05 19:02:04 +01:00
S3.upload(
{
2023-02-09 15:02:15 +01:00
Key: fileKey,
2022-01-05 19:02:04 +01:00
Body: file.stream || Buffer.from(file.buffer, 'binary'),
2023-02-14 19:02:00 +01:00
ACL,
2022-01-05 19:02:04 +01:00
ContentType: file.mime,
...customParams,
},
(err, data) => {
if (err) {
return reject(err);
}
2018-02-21 14:06:57 +01:00
2022-01-05 19:02:04 +01:00
// set the bucket file url
if (assertUrlProtocol(data.Location)) {
file.url = baseUrl ? `${baseUrl}/${fileKey}` : data.Location;
} else {
// Default protocol to https protocol
file.url = `https://${data.Location}`;
}
2022-01-05 19:02:04 +01:00
resolve();
}
);
});
return {
2023-02-09 15:02:15 +01:00
isPrivate() {
2023-02-14 19:02:00 +01:00
return ACL === 'private';
2023-02-09 15:02:15 +01:00
},
/**
2023-02-14 19:02:00 +01:00
* @param {Object} file
* @param {string} file.path
* @param {string} file.hash
* @param {string} file.ext
* @param {Object} customParams
* @returns {Promise<{url: string}>}
2023-02-09 15:02:15 +01:00
*/
getSignedUrl(file, customParams = {}) {
// Do not sign the url if it does not come from the same bucket.
const { bucket } = getBucketFromUrl(file.url);
if (bucket !== config.params.Bucket) {
return { url: file.url };
}
2023-02-09 15:02:15 +01:00
return new Promise((resolve, reject) => {
const path = file.path ? `${file.path}/` : '';
const fileKey = `${path}${file.hash}${file.ext}`;
2023-02-14 19:02:00 +01:00
2023-02-09 15:02:15 +01:00
S3.getSignedUrl(
'getObject',
{
Bucket: config.params.Bucket,
Key: fileKey,
2023-03-08 10:44:21 +01:00
Expires: getOr(60 * 60 * 24 * 7, ['params', 'signedUrlExpires'], config), // 7 days
2023-02-09 15:02:15 +01:00
},
(err, url) => {
if (err) {
return reject(err);
}
resolve({ url });
}
);
});
},
2022-01-05 19:02:04 +01:00
uploadStream(file, customParams = {}) {
return upload(file, customParams);
},
upload(file, customParams = {}) {
return upload(file, customParams);
2018-02-20 15:57:34 +01:00
},
delete(file, customParams = {}) {
2018-02-22 14:43:10 +01:00
return new Promise((resolve, reject) => {
// delete file on S3 bucket
const fileKey = getFileKey(file);
2019-07-18 19:28:52 +02:00
S3.deleteObject(
{
Key: fileKey,
...customParams,
2019-07-18 19:28:52 +02:00
},
(err) => {
2019-07-18 19:28:52 +02:00
if (err) {
return reject(err);
}
2018-02-20 15:57:34 +01:00
2019-07-18 19:28:52 +02:00
resolve();
}
);
2018-02-22 14:43:10 +01:00
});
2019-07-18 19:28:52 +02:00
},
2018-02-20 15:57:34 +01:00
};
2019-07-18 19:28:52 +02:00
},
2018-02-20 15:57:34 +01:00
};