diff --git a/scripts/get-software.js b/scripts/get-software.js index 32630259469f..d24d2901990d 100644 --- a/scripts/get-software.js +++ b/scripts/get-software.js @@ -1,3 +1,4 @@ +const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); const axios = require('axios'); @@ -22,6 +23,13 @@ const absoluteUrl = (url, repo) => { } }; +const assetsImage = (u) => { + const hash = crypto.createHash('sha1').update(u, 'utf8').digest('hex'); + const ext = path.extname(u); + + return `${hash.slice(0, 2)}/${hash.slice(2)}${ext}`; +}; + const addSlug = (software) => ({ ...software, slug: software.id }); const addAliases = (software) => { const aliases = [normalizeRepoUrl(software.url), ...software.aliases.map(a => normalizeRepoUrl(a))]; @@ -58,7 +66,7 @@ function toElasticSearchBulkFile(software, filename) { software .map(software => addSlug(software)) .map(software => addPubliccodeDict(software)) - .map(software => ({ ...software, publiccode: { ...software.publiccode, logo: absoluteUrl(software.publiccode.logo, software.url) } })) + .map(software => ({ ...software, publiccode: { ...software.publiccode, logo: assetsImage(absoluteUrl(software.publiccode.logo, software.url)) } })) .forEach(s => { const metadata = { 'index': { @@ -90,18 +98,18 @@ async function run() { .map(software => addAliases(addSlug(software))) .map(software => addSlug(software)) .map(software => addPubliccodeDict(software)) - .map(software => ({ ...software, publiccode: { ...software.publiccode, logo: absoluteUrl(software.publiccode.logo, software.url) } })); + .map(software => ({ ...software, publiccode: { ...software.publiccode, logo: assetsImage(absoluteUrl(software.publiccode.logo, software.url)) } })); data.forEach(software => { - Object.keys(software.publiccode.description).forEach(lang => { + Object.keys(software.publiccode.description).forEach((lang) => { const desc = software.publiccode.description[lang]; - software.publiccode.description[lang].screenshots = desc.screenshots?.map(ss => absoluteUrl(ss, software.url)); - }) + software.publiccode.description[lang].screenshots = desc.screenshots?.map((ss) => assetsImage(absoluteUrl(ss, software.url))); + }); }); // Remove the "url" key as it's used by Jekyll to hold the generated page's URL // and Searchyll uses it to compare against when the ignore: options are set in _config.yml. - const jekyll = data.map(({ url, ...rest }) => rest) + const jekyll = data.map(({ url, ...rest }) => rest); fs.writeFileSync('_data/crawler/software.yml', yaml.dump(jekyll)); }