Skip to content

Commit

Permalink
removed parallelised tasks to ensure determinism of thumbnail creation
Browse files Browse the repository at this point in the history
  • Loading branch information
edsilv committed Oct 5, 2018
1 parent 7d65fb2 commit 7121f71
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
15 changes: 7 additions & 8 deletions Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export class Canvas {

let hasPaintingAnnotation: boolean = false;

await Promise.all(customAnnotationFiles.map(async (file: string) => {

for (const file of customAnnotationFiles) {
let directoryName: string = dirname(file);
directoryName = directoryName.substr(directoryName.lastIndexOf('/'));
const name: string = basename(file, extname(file));
Expand Down Expand Up @@ -180,8 +180,8 @@ export class Canvas {
annotationJson.body.value = yml.value;
}

canvasJson.items[0].items.push(annotationJson);
}));
canvasJson.items[0].items.push(annotationJson);
}

if (!hasPaintingAnnotation) {
// for each jpg/pdf/mp4/obj in the canvas directory
Expand Down Expand Up @@ -216,8 +216,8 @@ export class Canvas {

private async _annotateFiles(canvasJson: any, files: string[]): Promise<void> {

await Promise.all(files.map(async (file: string) => {
for (let file of files) {

file = Utils.normaliseFilePath(file);
const extName: string = extname(file);

Expand Down Expand Up @@ -258,8 +258,7 @@ export class Canvas {
annotationJson.body.height = height;
}
}

}));
}
}

private async _getInfo(): Promise<void> {
Expand Down
28 changes: 19 additions & 9 deletions Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ export class Directory {
return Utils.compare(a, b);
});

await Promise.all(canvases.map(async (canvas: string) => {
// example of parallel processing
//await Promise.all(canvases.map(async (canvas: string) => { ... });

// dropped parallel processing in favour of "deterministic" results
// also, a lot of tasks in parallel can use too much memory:
// https://blog.lavrton.com/javascript-loops-how-to-handle-async-await-6252dd3c795
for (const canvas of canvases) {
console.log(chalk.green('creating canvas for: ') + canvas);
this.items.push(new Canvas(canvas, this));
}));
}

// directories not starting with an underscore
// these can be child manifests or child collections
Expand All @@ -74,14 +80,14 @@ export class Directory {
return Utils.compare(a, b);
});

await Promise.all(directories.map(async (directory: string) => {
for (const directory of directories) {
console.log(chalk.green('creating directory for: ') + directory);
const name: string = basename(directory);
const url: string = urljoin(this.url.href, name);
const newDirectory: Directory = new Directory(directory, url, this.generateThumbs, undefined, this);
await newDirectory.read();
this.directories.push(newDirectory);
}));
}

// if there are no canvas, manifest, or collection directories to read,
// but there are paintable files in the current directory,
Expand Down Expand Up @@ -158,7 +164,7 @@ export class Directory {

// for each child directory, add a collectionitem or manifestitem json boilerplate to items.

await Promise.all(this.directories.map(async (directory: Directory) => {
for (const directory of this.directories) {
let itemJson: any;

if (directory.isCollection) {
Expand All @@ -172,8 +178,8 @@ export class Directory {

await Utils.getThumbnail(itemJson, directory);

this.indexJson.items.push(itemJson);
}));
this.indexJson.items.push(itemJson);
}

// check for manifests.yml. if it exists, parse and add to items
const hasManifestsYml: boolean = await Utils.hasManifestsYml(this.directoryPath);
Expand Down Expand Up @@ -228,7 +234,9 @@ export class Directory {

// for each canvas, add canvas json

await Promise.all(this.items.map(async (canvas: Canvas, index: number) => {
let index: number = 0;

for (const canvas of this.items) {
const canvasJson: any = Utils.cloneJson(canvasBoilerplate);

canvasJson.id = urljoin(this.url.href, 'index.json/canvas', index);
Expand All @@ -238,7 +246,9 @@ export class Directory {

// add canvas to items
this.indexJson.items.push(canvasJson);
}));

index++;
}

this.indexJson.items.sort((a, b) => {
return Utils.compare(a.id, b.id);
Expand Down
14 changes: 10 additions & 4 deletions Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export class Utils {
// if debugging: jimp item.getitem is not a function
// generate thumbnail
if (json.items && json.items.length && json.items[0].items) {
console.log(chalk.green('generating thumbnail for: ') + fp);
// find an annotation with a painting motivation of type image.
const items: any[] = json.items[0].items;

Expand Down Expand Up @@ -214,9 +213,16 @@ export class Utils {
thumb.resize(config.thumbnails.width, Jimp.AUTO);
pathToThumb += image.getExtension();

thumb.write(pathToThumb, () => {
console.log(chalk.green('generated thumbnail for: ') + fp);
});
// a thumbnail may already exist at this path (when generating from a flat collection of images)
const thumbExists: boolean = await Utils.fileExists(pathToThumb);

if (!thumbExists) {
thumb.write(pathToThumb, () => {
console.log(chalk.green('generated thumbnail for: ') + fp);
});
} else {
console.log(chalk.green('found thumbnail for: ') + fp);
}

} else {
// placeholder img path
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "biiif",
"version": "0.3.13",
"version": "0.3.14",
"description": "A CLI to build IIIF collections",
"main": "index.js",
"repository": {
Expand All @@ -9,7 +9,7 @@
},
"scripts": {
"test": "tsc && mocha",
"testbuild": "node --nolazy --inspect-brk=5858 -e \"require('./index').build('collection', 'https://edsilv.github.io/biiif-test-manifests/sound-manifest')\""
"testbuild": "node --nolazy --inspect-brk=5858 -e \"require('./index').build('collection', 'https://edsilv.github.io/biiif-test-manifests/sort-files-numeric-manifest', true)\""
},
"engines": {
"node": ">=8.9.1",
Expand Down

0 comments on commit 7121f71

Please sign in to comment.