Support upload latest file for GCS. (#3258)

* Support upload latest file for GCS.

* Cache time
This commit is contained in:
Peter Hedenskog 2021-01-18 15:53:40 +01:00 committed by GitHub
parent ec29d8e401
commit 8899f5d424
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 2 deletions

View File

@ -9,6 +9,26 @@ const { Storage } = require('@google-cloud/storage');
const log = require('intel').getLogger('sitespeedio.plugin.gcs');
const throwIfMissing = require('../../support/util').throwIfMissing;
async function uploadLatestFiles(dir, gcsOptions, prefix) {
function ignoreDirs(file, stats) {
return stats.isDirectory();
}
const storage = new Storage({
projectId: gcsOptions.projectId,
keyFilename: gcsOptions.key
});
const bucket = storage.bucket(gcsOptions.bucketname);
const files = await readdir(dir, [ignoreDirs]);
const promises = [];
for (let file of files) {
promises.push(uploadFile(file, bucket, gcsOptions, prefix, dir, true));
}
return Promise.all(promises);
}
async function upload(dir, gcsOptions, prefix) {
const files = await readdir(dir);
const promises = [];
@ -32,7 +52,14 @@ async function upload(dir, gcsOptions, prefix) {
return Promise.all(promises);
}
async function uploadFile(file, bucket, gcsOptions, prefix, baseDir) {
async function uploadFile(
file,
bucket,
gcsOptions,
prefix,
baseDir,
noCacheTime
) {
const subPath = path.relative(baseDir, file);
const fileName = path.join(gcsOptions.path || prefix, subPath);
@ -44,7 +71,7 @@ async function uploadFile(file, bucket, gcsOptions, prefix, baseDir) {
gzip: !!gcsOptions.gzip,
metadata: {
metadata: {
cacheControl: 'public, max-age=31536000'
cacheControl: 'public, max-age=' + noCacheTime ? 0 : 31536000
}
}
};
@ -55,6 +82,7 @@ async function uploadFile(file, bucket, gcsOptions, prefix, baseDir) {
module.exports = {
open(context, options) {
this.gcsOptions = options.gcs;
this.options = options;
this.make = context.messageMaker('gcs').make;
throwIfMissing(this.gcsOptions, ['bucketname'], 'gcs');
this.storageManager = context.storageManager;
@ -81,6 +109,12 @@ module.exports = {
gcsOptions,
this.storageManager.getStoragePrefix()
);
if (this.options.copyLatestFilesToBase) {
const rootPath = path.resolve(baseDir, '..');
const dirsAsArray = rootPath.split(path.sep);
const rootName = dirsAsArray.slice(-1)[0];
await uploadLatestFiles(rootPath, gcsOptions, rootName);
}
log.info('Finished upload to Google Cloud Storage');
if (gcsOptions.public) {
log.info(