UNPKG

ring-websites-toolbelt

Version:

Ring Publishing Platform tool to work with Ring Websites

165 lines (137 loc) 4.88 kB
const AWS = require('aws-sdk'); const cryptoJS = require('crypto-js'); const fs = require('fs'); const mime = require('mime-types'); class S3Provider { constructor(themeJson, ucsConfig, scriptOptions) { this._themeParams = themeJson; this._scriptOptions = scriptOptions; this._namespaceId = ucsConfig.namespaceId; this._variant = ucsConfig.ucsVariant; const _s3opts = { region: 'ocdn', endpoint: 'ocdn.eu', s3ForcePathStyle: true, sslEnabled: true, accessKeyId: ucsConfig.s3AccessKey, secretAccessKey: ucsConfig.s3SecretKey, signatureVersion: 's3', computeChecksums: false }; this._s3 = new AWS.S3(_s3opts); this._s3BucketName = ucsConfig.s3BucketName; } async uploadCompressedFile(zipFile, key) { //key = 'dev_statics/sm_test/'; const config = { bucket: this._s3BucketName, ocdnPath: key || 'dev_statics/' + cryptoJS.MD5(this._namespaceId + '/' + this._themeParams.theme + '/' + this._variant + Date.now()), contentType: S3Provider.CONTENT_TYPE }; try { return await this.upload(zipFile, config); } catch (err) { console.error('S3Provider/uploadCompressedFiles error while uploading theme to ocdn:', err); throw err; } } async uploadFile(file, key) { const config = { bucket: this._s3BucketName, ocdnPath: key, contentType: mime.lookup(file), }; try { return await this.upload(file, config); } catch (err) { console.error(`S3Provider/uploadFile error while uploading file ${file} to ocdn: `, err); throw err; } } async uploadFinalTheme(compressedTheme) { const uploadTimestamp = Date.now(); const config = { ocdnPath: 'ucs-theme-deploy/' + this._themeParams.theme + '/' + this._themeParams.version + '/' + uploadTimestamp, contentType: 'application/zip' }; try { return await this.upload(compressedTheme, config); } catch (err) { console.error('S3Provider/uploadFinalTheme error while uploading theme to ocdn:', err); throw err; } } async uploadFinalRouter(compressedRouter, routerName, routerVersion) { const uploadTimestamp = Date.now(); const config = { ocdnPath: 'ucs-router-deploy/' + routerName + '/' + routerVersion + '/' + uploadTimestamp, contentType: 'application/zip' }; try { return await this.upload(compressedRouter, config); } catch (err) { console.error('S3Provider/uploadFinalRouter error while uploading router to ocdn:', err); throw err; } } async upload(compressedFilePath, config) { const body = await this._readFile(compressedFilePath); const params = { Body: body, Bucket: this._s3BucketName, Key: config.ocdnPath, ContentType: config.contentType, ContentLength: body.length, CacheControl: 'no-cache', Metadata: {} }; const options = { partSize: 50 * 1024 * 1024, queueSize: 1 }; const data = await this._s3.upload(params, options).promise(); if (this._scriptOptions.verbose) { console.info('S3Provider/upload successful uploaded file', compressedFilePath, data); } return data; } async deleteFile(key) { const params = { Bucket: this._s3BucketName, Key: key }; try{ await this._s3.deleteObject(params).promise(); console.info('S3Provider/deleteFile successful deleted file', key); } catch(err){ console.error(`S3Provider/deleteFile error while deleting file ${key} to ocdn: `, err); console.error(err); } } async deleteFiles(files) { const params = { Bucket: this._s3BucketName, Delete: { Objects: files } }; try{ await this._s3.deleteObjects(params).promise(); } catch(err){ console.error(err); } } async _readFile(filePath) { return new Promise((resolve, reject) => { fs.readFile(filePath, (err, body) => { if (err) { console.log('Error reading file', filePath, err); return reject(err); } resolve(body); }); }); } } S3Provider.CONTENT_TYPE = 'multipart/archive; type=zip'; module.exports = S3Provider;