Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function generateAndStoreDistributionReport(params) {
const {
reportStartTime,
reportEndTime
} = params;
const distributionReport = await generateDistributionReport({
reportStartTime,
reportEndTime
});
const { reportsBucket, reportsPrefix } = bucketsPrefixes();
const reportKey = await determineReportKey(DISTRIBUTION_REPORT, reportStartTime, reportsPrefix);
const s3Uri = aws.buildS3Uri(reportsBucket, reportKey);
log.info(`Uploading report to ${s3Uri}`);
return aws.s3().putObject({
Bucket: reportsBucket,
Key: reportKey,
Body: distributionReport
}).promise()
.then(() => ({ reportType: DISTRIBUTION_REPORT, file: s3Uri }));
}
// Export to support testing
async ingestFile(file, destinationBucket, duplicateHandling) {
const fileRemotePath = path.join(file.path, file.name);
// place files in the subdirectory
const stagingPath = path.join(this.fileStagingDir, this.collectionId);
const destinationKey = path.join(stagingPath, file.name);
// the staged file expected
const stagedFile = Object.assign(cloneDeep(file),
{
filename: aws.buildS3Uri(destinationBucket, destinationKey),
fileStagingDir: stagingPath,
url_path: this.getUrlPath(file),
bucket: destinationBucket
});
// bind arguments to sync function
const syncFileFunction = this.sync.bind(this, fileRemotePath);
const s3ObjAlreadyExists = await aws.s3ObjectExists(
{ Bucket: destinationBucket, Key: destinationKey }
);
log.debug(`file ${destinationKey} exists in ${destinationBucket}: ${s3ObjAlreadyExists}`);
let versionedFiles = [];
if (s3ObjAlreadyExists) {
stagedFile.duplicate_found = true;
const stagedFileKey = `${destinationKey}.${uuidv4()}`;
function generateFileUrl(file, distEndpoint, cmrGranuleUrlType = 'distribution') {
if (cmrGranuleUrlType === 'distribution') {
const extension = urljoin(file.bucket, getS3KeyOfFile(file));
return urljoin(distEndpoint, extension);
}
if (cmrGranuleUrlType === 's3') {
if (file.filename) {
return file.filename;
}
return aws.buildS3Uri(file.bucket, file.key);
}
return null;
}
function getS3UrlOfFile(file) {
if (file.filename) return file.filename;
if (file.bucket && file.filepath) return aws.buildS3Uri(file.bucket, file.filepath);
if (file.bucket && file.key) return aws.buildS3Uri(file.bucket, file.key);
throw new Error(`Unable to determine location of file: ${JSON.stringify(file)}`);
}
return [stagedFile].concat(versionedFiles.map((f) => (
{
bucket: destinationBucket,
name: path.basename(f.Key),
path: file.path,
filename: aws.buildS3Uri(f.Bucket, f.Key),
size: f.size,
fileStagingDir: stagingPath,
url_path: this.getUrlPath(file)
})));
}
async function uploadReportToS3(filename, reportBucket, reportKey) {
await aws.s3().putObject({
Bucket: reportBucket,
Key: reportKey,
Body: fs.createReadStream(filename)
}).promise();
fs.unlinkSync(filename);
const s3Uri = aws.buildS3Uri(reportBucket, reportKey);
log.info(`uploaded ${s3Uri}`);
return s3Uri;
}