Skip to content

Commit

Permalink
Fixes slow rehashing on image instance
Browse files Browse the repository at this point in the history
Fixes #171
  • Loading branch information
phuhl authored Jan 27, 2023
1 parent e51ad8e commit 11cc226
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion img.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ class Image {
}

getHash() {
if (this.computedHash) {
return this.computedHash;
}

let hash = createHash("sha256");

if(fs.existsSync(this.src)) {
Expand Down Expand Up @@ -342,7 +346,9 @@ class Image {
// replace with hash.digest('base64url')
let base64hash = hash.digest('base64').replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");

return base64hash.slice(0, this.options.hashLength);
const resultHash = base64hash.substring(0, this.options.hashLength);
this.computedHash = resultHash;
return resultHash;
}

getStat(outputFormat, width, height) {
Expand Down

0 comments on commit 11cc226

Please sign in to comment.