-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from Niels-IO/newRemoteImageURLNames
Use Hash for remote Image URLs
- Loading branch information
Showing
12 changed files
with
1,345 additions
and
668 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
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
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,16 +1,30 @@ | ||
module.exports = function urlToFilename(url: string) { | ||
// Remove the protocol from the URL | ||
let filename = url.replace(/^(https?|ftp):\/\//, ""); | ||
|
||
// Replace special characters with underscores | ||
filename = filename.replace(/[/\\:*?"<>|#%]/g, "_"); | ||
|
||
// Remove control characters | ||
// eslint-disable-next-line no-control-regex | ||
filename = filename.replace(/[\x00-\x1F\x7F]/g, ""); | ||
try { | ||
const parsedUrl = new URL(url); | ||
const extension = parsedUrl.pathname.split(".").pop(); | ||
if (extension) { | ||
return hashAlgorithm(url).toString().concat(".", extension); | ||
} | ||
return hashAlgorithm(url).toString(); | ||
} catch (error) { | ||
console.error("Error parsing URL", url, error); | ||
} | ||
}; | ||
|
||
// Trim any leading or trailing spaces | ||
filename = filename.trim(); | ||
// Credits to https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js | ||
// This is a hash function that is used to generate a hash from the image URL | ||
const hashAlgorithm = (str: string, seed = 0) => { | ||
let h1 = 0xdeadbeef ^ seed, | ||
h2 = 0x41c6ce57 ^ seed; | ||
for (let i = 0, ch; i < str.length; i++) { | ||
ch = str.charCodeAt(i); | ||
h1 = Math.imul(h1 ^ ch, 2654435761); | ||
h2 = Math.imul(h2 ^ ch, 1597334677); | ||
} | ||
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507); | ||
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909); | ||
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507); | ||
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909); | ||
|
||
return filename; | ||
return 4294967296 * (2097151 & h2) + (h1 >>> 0); | ||
}; |
Oops, something went wrong.