Skip to content

Commit

Permalink
wip: use proxied image assets
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabio committed Aug 1, 2024
1 parent b0d6f98 commit ab1658d
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions scripts/get-software.js
Original file line number Diff line number Diff line change
@@ -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)) {
Expand All @@ -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))];
Expand Down Expand Up @@ -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': {
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit ab1658d

Please sign in to comment.