Last active
January 21, 2021 00:09
-
-
Save devotox/3618d34e8c870eff8a5cf4d35e0685f0 to your computer and use it in GitHub Desktop.
Revisions
-
devotox revised this gist
Sep 14, 2017 . 1 changed file with 10 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,26 +1,26 @@ #!/usr/bin/node const fs = require('fs'); const path = require('path'); const AWS = require('aws-sdk'); const Promise = require('bluebird'); const imagemin = require('imagemin'); const imageminJpegRecompress = require('imagemin-jpeg-recompress'); const config = { sslEnabled: true, bucket: process.env.AWS_BUCKET, region: process.env.AWS_REGION, accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY }; AWS.config.update(config); const s3Bucket = config.bucket; const s3Endpoint = 's3.' + config.region + '.amazonaws.com'; const s3PublicEndpoint = 'https://' + config.bucket + '.s3.amazonaws.com/'; const s3 = new AWS.S3({ signatureVersion: 'v4', endpoint: s3Endpoint }); const optimizeImage = (image) => { let imageminOptions = { -
devotox revised this gist
Sep 14, 2017 . 1 changed file with 17 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,15 +37,15 @@ const optimizeImage = (image) => { }; return imagemin.buffer(image.Body, imageminOptions) .then((buffer) => { console.info('Optimizing Image Finished:', image.Key); console.log('Prev Size', Math.round(image.Body.toString().length / 1000) + 'KB'); console.log('New Size', Math.round(buffer.toString().length / 1000) + 'KB\n'); return { Body: buffer, Key: image.Key }; }) .catch((error) => { console.error('Image Optimization Error:', image.Key, error); }); }; const listImages = (params) => { @@ -90,9 +90,9 @@ const setImage = (image) => { const optimize = (image) => { return getImage(image) .then(optimizeImage) .then(setImage) .catch(console.error); }; const run = (Marker, oneRun) => { @@ -102,12 +102,12 @@ const run = (Marker, oneRun) => { console.log('\nNext Marker', Marker); return listImages(params) .then((data) => { console.log('\nTotal Length:', data.Contents.length); return Promise.map(data.Contents, optimize) .then(() => data.IsTruncated && !oneRun ? run(data.Contents.slice(-1)[0].Key) : null); }); }; return run(process.argv[2], process.argv[3]); -
devotox revised this gist
Sep 14, 2017 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,6 +35,7 @@ const optimizeImage = (image) => { }) ] }; return imagemin.buffer(image.Body, imageminOptions) .then((buffer) => { console.info('Optimizing Image Finished:', image.Key); -
devotox revised this gist
Sep 14, 2017 . No changes.There are no files selected for viewing
-
devotox revised this gist
Sep 14, 2017 . No changes.There are no files selected for viewing
-
devotox revised this gist
Sep 14, 2017 . 1 changed file with 24 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +1,29 @@ #!/usr/bin/node const fs = require('fs'); const path = require('path'); const AWS = require('aws-sdk'); const Promise = require('bluebird'); const imagemin = require('imagemin'); const imageminJpegRecompress = require('imagemin-jpeg-recompress'); const config = { sslEnabled: true, bucket: process.env.AWS_BUCKET, region: process.env.AWS_REGION, accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY }; AWS.config.update(config); const s3Bucket = config.bucket; const s3Endpoint = 's3.' + config.region + '.amazonaws.com'; const s3PublicEndpoint = 'https://' + config.bucket + '.s3.amazonaws.com/'; const s3 = new AWS.S3({ signatureVersion: 'v4', endpoint: s3Endpoint }); const optimizeImage = (image) => { let imageminOptions = { plugins: [ imageminJpegRecompress({ progressive: true, @@ -35,7 +35,7 @@ const optimize_image = (image) => { }) ] }; return imagemin.buffer(image.Body, imageminOptions) .then((buffer) => { console.info('Optimizing Image Finished:', image.Key); console.log('Prev Size', Math.round(image.Body.toString().length / 1000) + 'KB'); @@ -47,15 +47,15 @@ const optimize_image = (image) => { }); }; const listImages = (params) => { return new Promise((resolve, reject) => { s3.listObjects(params, (error, images) => { return error ? reject(error) : resolve(images); }); }); }; const getImage = (image) => { let params = { Bucket: config.bucket, Key: image.Key }; return new Promise((resolve, reject) => { s3.getObject(params, (error, image) => { @@ -65,7 +65,7 @@ const get_image = (image) => { }); }; const setImage = (image) => { if(!(image && image.Key && image.Body)) { return Promise.resolve(); } @@ -81,16 +81,16 @@ const set_image = (image) => { return new Promise((resolve, reject) => { s3.putObject(params, (error) => { console.log('\nUploaded', params.Key, error ? error : ''); return error ? reject(error) : resolve(); }); }); }; const optimize = (image) => { return getImage(image) .then(optimizeImage) .then(setImage) .catch(console.error); }; @@ -100,7 +100,7 @@ const run = (Marker, oneRun) => { console.log('\nNext Marker', Marker); return listImages(params) .then((data) => { console.log('\nTotal Length:', data.Contents.length); -
devotox revised this gist
Sep 14, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ const path = require('path'); const AWS = require('aws-sdk'); const Promise = require('bluebird'); const imagemin = require('imagemin'); const imageminJpegRecompress = require('imagemin-jpeg-recompress'); const config = { sslEnabled: true, -
devotox revised this gist
Sep 14, 2017 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,16 @@ #!/usr/bin/node const fs = require('fs'); const path = require('path'); const AWS = require('aws-sdk'); const Promise = require('bluebird'); const imagemin = require('imagemin'); const imageminJpegRecompress = require('imagemin-jpeg-recompress'); const config = { sslEnabled: true, bucket: process.env.AWS_BUCKET, region: process.env.AWS_REGION, accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY }; -
devotox created this gist
Sep 14, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,112 @@ #!/usr/bin/node const fs = require('fs'); const path = require('path'); var AWS = require('aws-sdk'); const Promise = require('bluebird'); const imagemin = require('imagemin'); const imageminJpegRecompress = require('imagemin-jpeg-recompress'); const config = { sslEnabled: true, bucket: process.env.AWS_BUCKET, region: process.env.AWS_REGION, accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY }; AWS.config.update(config); const s3_bucket = config.bucket; const s3_endpoint = 's3.' + config.region + '.amazonaws.com'; const s3_public_endpoint = 'https://' + config.bucket + '.s3.amazonaws.com/'; const s3 = new AWS.S3({ signatureVersion: 'v4', endpoint: s3_endpoint }); const optimize_image = (image) => { let imagemin_options = { plugins: [ imageminJpegRecompress({ progressive: true, quality: 'high', accuracy: true, target: 0.995, min: 60, max: 95 }) ] }; return imagemin.buffer(image.Body, imagemin_options) .then((buffer) => { console.info('Optimizing Image Finished:', image.Key); console.log('Prev Size', Math.round(image.Body.toString().length / 1000) + 'KB'); console.log('New Size', Math.round(buffer.toString().length / 1000) + 'KB\n'); return { Body: buffer, Key: image.Key }; }) .catch((error) => { console.error('Image Optimization Error:', image.Key, error); }); }; const list_images = (params) => { return new Promise((resolve, reject) => { s3.listObjects(params, (error, images) => { return error ? reject(error) : resolve(images); }); }); }; const get_image = (image) => { let params = { Bucket: config.bucket, Key: image.Key }; return new Promise((resolve, reject) => { s3.getObject(params, (error, image) => { image ? image.Key = params.Key : null; return error ? reject(error) : resolve(image); }); }); }; const set_image = (image) => { if(!(image && image.Key && image.Body)) { return Promise.resolve(); } let params = { Body: image.Body, Key: image.Key, Bucket: config.bucket, ACL: "public-read", ContentType: "image/jpeg", CacheControl: "max-age=86400, public" }; return new Promise((resolve, reject) => { s3.putObject(params, (error) => { console.log('\nUploaded', params.Key, error); return error ? reject(error) : resolve(); }); }); }; const optimize = (image) => { return get_image(image) .then(optimize_image) .then(set_image) .catch(console.error); }; const run = (Marker, oneRun) => { let params = { Bucket: config.bucket, MaxKeys: oneRun ? 1 : 100 }; params.Marker = Marker; console.log('\nNext Marker', Marker); return list_images(params) .then((data) => { console.log('\nTotal Length:', data.Contents.length); return Promise.map(data.Contents, optimize) .then(() => data.IsTruncated && !oneRun ? run(data.Contents.slice(-1)[0].Key) : null); }); }; return run(process.argv[2], process.argv[3]);