Skip to content

Commit

Permalink
Download images concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
misenhower committed Nov 18, 2024
1 parent 6ba815a commit 34c2d65
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
17 changes: 15 additions & 2 deletions app/data/ImageProcessor.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import fs from 'fs/promises';
import path from 'path';
import mkdirp from 'mkdirp';
import pQueue from 'p-queue';
import prefixedConsole from '../common/prefixedConsole.mjs';
import { normalizeSplatnetResourcePath } from '../common/util.mjs';
import { exists } from '../common/fs.mjs';

const queue = new pQueue({ concurrency: 4 });

export default class ImageProcessor
{
destinationDirectory = 'dist';
Expand All @@ -15,17 +18,27 @@ export default class ImageProcessor
this.siteUrl = process.env.SITE_URL;
}

async process(url) {
async process(url, defer = true) {
// Normalize the path
let destination = this.normalize(url);

// Download the image if necessary
await this.maybeDownload(url, destination);
let job = () => this.maybeDownload(url, destination);
// defer ? queue.add(job) : await job();
if (defer) {
queue.add(job);
} else {
await job();
}

// Return the new public URL
return [destination, this.publicUrl(destination)];
}

static onIdle() {
return queue.onIdle();
}

normalize(url) {
return normalizeSplatnetResourcePath(url);
}
Expand Down
2 changes: 2 additions & 0 deletions app/data/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CoopUpdater from './updaters/CoopUpdater.mjs';
import FestivalUpdater from './updaters/FestivalUpdater.mjs';
import XRankUpdater from './updaters/XRankUpdater.mjs';
import StagesUpdater from './updaters/StagesUpdater.mjs';
import ImageProcessor from './ImageProcessor.mjs';

function updaters() {
return [
Expand Down Expand Up @@ -53,6 +54,7 @@ export async function update(config = 'default') {
}

if (canSync()) {
await ImageProcessor.onIdle();
await (new S3Syncer).upload();
}

Expand Down
2 changes: 2 additions & 0 deletions app/data/updaters/DataUpdater.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export default class DataUpdater
jsonpath.apply(data, expression, url => mapping[url]);
}

await ImageProcessor.onIdle();

return images;
}

Expand Down
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"masto": "^6.7.0",
"mkdirp": "^1.0.4",
"nxapi": "^1.4.0",
"p-queue": "^8.0.1",
"pinia": "^2.0.22",
"puppeteer-core": "^23.8.0",
"s3-sync-client": "^4.3.1",
Expand Down

0 comments on commit 34c2d65

Please sign in to comment.