This repository has been archived by the owner on Jun 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
package-lock.json |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
const http = require("http"); | ||
const https = require("https"); | ||
const fs = require("fs"); | ||
const Stream = require("stream").Transform; | ||
const sharp = require("sharp"); | ||
|
||
const imagemin = require("imagemin"); | ||
const imageminJpegtran = require("imagemin-jpegtran"); | ||
const imageminPngquant = require("imagemin-pngquant"); | ||
|
||
class AvatarLocalCache { | ||
constructor() { | ||
this.width = 120; | ||
this._formats = ["jpeg", "png", "webp"]; | ||
} | ||
|
||
get formats() { | ||
return this._formats; | ||
} | ||
|
||
set formats(formats) { | ||
this._formats = formats; | ||
} | ||
|
||
fetchUrl(url, outputFileSlug) { | ||
return new Promise((fetchResolve, fetchReject) => { | ||
(url.startsWith("http:") ? http : https).request(url, (res) => { | ||
const { statusCode } = res; | ||
|
||
if (statusCode !== 200) { | ||
res.resume(); | ||
fetchReject(new Error(`AvatarLocalCache request failed. Status Code: ${statusCode}`)); | ||
} | ||
|
||
let data = new Stream(); | ||
res.on("data", (chunk) => { | ||
data.push(chunk); | ||
}); | ||
|
||
res.on("end", function() { | ||
let promises = []; | ||
let img = sharp(data.read()).resize(this.defaultWidth); | ||
|
||
if(this.formats.indexOf("jpeg") > -1) { | ||
let jpgPromise = new Promise((resolve, reject) => { | ||
// http://sharp.pixelplumbing.com/en/stable/api-output/#jpeg | ||
img.jpeg({ | ||
quality: 100 | ||
}) | ||
.toFile(`${outputFileSlug}.jpg`, (err, info) => { | ||
if(err) { | ||
fetchReject(err); | ||
} else { | ||
imagemin([`${outputFileSlug}.jpg`], { | ||
plugins: [ | ||
imageminJpegtran() | ||
] | ||
}).then(files => { | ||
for(let file of files) { | ||
fs.writeFile(`${outputFileSlug}.jpg`, file.data, err => { | ||
if(err) { | ||
fetchReject(err); | ||
} else { | ||
resolve(`${outputFileSlug}.jpg`); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
promises.push(jpgPromise); | ||
} | ||
|
||
if(this.formats.indexOf("png") > -1) { | ||
let pngPromise = new Promise((resolve, reject) => { | ||
// http://sharp.pixelplumbing.com/en/stable/api-output/#png | ||
img.png() | ||
.toFile(`${outputFileSlug}.png`, (err, info) => { | ||
if(err) { | ||
fetchReject(err); | ||
} else { | ||
// console.log( `Wrote ${outputFileSlug}.png` ); | ||
|
||
imagemin([`${outputFileSlug}.png`], { | ||
plugins: [ | ||
imageminPngquant() | ||
] | ||
}).then(files => { | ||
for(let file of files) { | ||
fs.writeFile(`${outputFileSlug}.png`, file.data, err => { | ||
if(err) { | ||
fetchReject(err); | ||
} else { | ||
resolve(`${outputFileSlug}.png`); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
promises.push(pngPromise); | ||
} | ||
|
||
if(this.formats.indexOf("webp") > -1) { | ||
let webpPromise = new Promise((resolve, reject) => { | ||
// http://sharp.pixelplumbing.com/en/stable/api-output/#webp | ||
img.webp({ | ||
lossless: true | ||
}) | ||
.toFile(`${outputFileSlug}.webp`, (err, info) => { | ||
if(err) { | ||
fetchReject(err); | ||
} else { | ||
resolve(`${outputFileSlug}.webp`); | ||
} | ||
}); | ||
}); | ||
|
||
promises.push(webpPromise); | ||
} | ||
|
||
Promise.all(promises).then(function(files) { | ||
fetchResolve(files); | ||
}); | ||
}.bind(this)); | ||
}).end(); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = AvatarLocalCache; |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Avatar Sample Cache | ||
|
||
Saves a image URL for an avatar to the local file system (and optimizes the image). | ||
|
||
* Workaround for large avatar images on hosted services. | ||
* Image URLs won’t break in the future. | ||
|
||
## Usage | ||
|
||
```js | ||
const AvatarLocalCache = require("./AvatarLocalCache"); | ||
|
||
let cache = new AvatarLocalCache(); | ||
cache.fetchUrl("https://www.gravatar.com/avatar/10ca8fcb1f434f8929dca2a8867fb71d?default=404", "philhawksworth").then(function(files) { | ||
console.log( `Wrote ${files.join(", ")}.` ); | ||
}); | ||
``` | ||
|
||
Outputs: | ||
|
||
``` | ||
Wrote philhawksworth.jpg, philhawksworth.png, philhawksworth.webp. | ||
``` |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "avatar-local-cache", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "AvatarLocalCache.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"imagemin": "^7.0.0", | ||
"imagemin-jpegtran": "^6.0.0", | ||
"imagemin-pngquant": "^8.0.0", | ||
"sharp": "^0.23.0" | ||
} | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const AvatarLocalCache = require("./AvatarLocalCache"); | ||
|
||
let cache = new AvatarLocalCache(); | ||
cache.fetchUrl("https://www.gravatar.com/avatar/10ca8fcb1f434f8929dca2a8867fb71d?default=404", "philhawksworth").then(function(files) { | ||
console.log( `Wrote ${files.join(", ")}.` ); | ||
}); |