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 b37f8da commit db4f557
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions scripts/get-software.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const axios = require('axios');
Expand All @@ -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))];
Expand Down Expand Up @@ -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': {
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit db4f557

Please sign in to comment.