From 3b770c69507ef139cd07f5335aefba33217d43ad Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 12 May 2022 23:23:17 -0300 Subject: preload, map and format sizes of additional files --- src/upd8.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'src/upd8.js') diff --git a/src/upd8.js b/src/upd8.js index b55ddda..12f1af3 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -173,6 +173,8 @@ import { OFFICIAL_GROUP_DIRECTORY } from './util/magic-constants.js'; +import FileSizePreloader from './file-size-preloader.js'; + const __dirname = path.dirname(fileURLToPath(import.meta.url)); const CACHEBUST = 8; @@ -1655,6 +1657,45 @@ async function main() { WD.officialAlbumData = WD.albumData.filter(album => album.groups.some(group => group.directory === OFFICIAL_GROUP_DIRECTORY)); WD.fandomAlbumData = WD.albumData.filter(album => album.groups.every(group => group.directory !== OFFICIAL_GROUP_DIRECTORY)); + const fileSizePreloader = new FileSizePreloader(); + + // File sizes of additional files need to be precalculated before we can + // actually reference 'em in site building, so get those loading right + // away. We actually need to keep track of two things here - the on-device + // file paths we're actually reading, and the corresponding on-site media + // paths that will be exposed in site build code. We'll build a mapping + // function between them so that when site code requests a site path, + // it'll get the size of the file at the corresponding device path. + const additionalFilePaths = [ + ...WD.albumData.flatMap(album => ( + [ + ...album.additionalFiles ?? [], + ...album.tracks.flatMap(track => track.additionalFiles ?? []) + ] + .flatMap(fileGroup => fileGroup.files) + .map(file => ({ + device: (path.join(mediaPath, urls + .from('media.root') + .toDevice('media.albumAdditionalFile', album.directory, file))), + media: (urls + .from('media.root') + .to('media.albumAdditionalFile', album.directory, file)) + })))), + ]; + + const getSizeOfAdditionalFile = mediaPath => { + const { device = null } = additionalFilePaths.find(({ media }) => media === mediaPath) || {}; + if (!device) return null; + return fileSizePreloader.getSizeOfPath(device); + }; + + logInfo`Preloading filesizes for ${additionalFilePaths.length} additional files...`; + + fileSizePreloader.loadPaths(...additionalFilePaths.map(path => path.device)); + await fileSizePreloader.waitUntilDoneLoading(); + + logInfo`Done preloading filesizes!`; + if (noBuild) return; // Makes writing a little nicer on CPU theoretically, 8ut also costs in @@ -2016,8 +2057,12 @@ async function main() { const pageFn = () => page({ ...bound, + language, - to + to, + urls, + + getSizeOfAdditionalFile, }); const content = writePage.html(pageFn, { -- cgit 1.3.0-6-gf8a5