diff --git a/scripts/get-software.js b/scripts/get-software.js index 32630259469f..12f4918d9576 100644 --- a/scripts/get-software.js +++ b/scripts/get-software.js @@ -1,8 +1,11 @@ +const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); const axios = require('axios'); const yaml = require('js-yaml'); +const ASSETS_HOST = "assets.developers.italia.it"; + const normalizeRepoUrl = (url) => url.toLowerCase().replace(/.git$/, ''); const absoluteUrl = (url, repo) => { if (!url || (/^http(s)?:\/\//i).test(url)) { @@ -22,6 +25,17 @@ const absoluteUrl = (url, repo) => { } }; +const assetsImage = (url) => { + if (!url) { + return url; + } + + const hash = crypto.createHash('sha1').update(url, 'utf8').digest('hex'); + const ext = path.extname(url); + + return `https://${ASSETS_HOST}/${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 +72,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 +104,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)); }