Skip to content
This repository has been archived by the owner on Jun 5, 2022. It is now read-only.

Commit

Permalink
Initial commit. 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Aug 20, 2019
1 parent eb12bc2 commit c21edbc
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
135 changes: 135 additions & 0 deletions AvatarLocalCache.js
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;
23 changes: 23 additions & 0 deletions README.md
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.
```
18 changes: 18 additions & 0 deletions package.json
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"
}
}
6 changes: 6 additions & 0 deletions sample.js
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(", ")}.` );
});

0 comments on commit c21edbc

Please sign in to comment.