Complete reverse lookup + refactoring
This commit is contained in:
@@ -28,7 +28,7 @@ import { authors, COPYRIGHT } from '../constants.js';
|
|||||||
* @property {String} subfolder
|
* @property {String} subfolder
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const namePos = {
|
export const namePos = {
|
||||||
hiroxnir: 1,
|
hiroxnir: 1,
|
||||||
nir: 1,
|
nir: 1,
|
||||||
visr: 1,
|
visr: 1,
|
||||||
@@ -45,17 +45,18 @@ const namePos = {
|
|||||||
* @see ManifestBuilder.getImageName()
|
* @see ManifestBuilder.getImageName()
|
||||||
* @param {String} segment
|
* @param {String} segment
|
||||||
*/
|
*/
|
||||||
const normaliseSegment = (segment) => {
|
export const normaliseSegment = (segment) => {
|
||||||
segment.replaceAll(/^([a-z]+)?0*(\d+)/ig, '$1$2');
|
return segment.replaceAll(/^([a-z]+)?0*(\d+)/ig, '$1$2');
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Normalize a full canvas name
|
* Normalize a full canvas name
|
||||||
* @param {String} name
|
* @param {String} name
|
||||||
*/
|
*/
|
||||||
const normaliseName = (name) => {
|
export const normaliseName = (name) => {
|
||||||
// Account for separate parts in name
|
// Account for separate parts in name
|
||||||
// and rejoin them after normalisation
|
// and rejoin them after normalisation
|
||||||
name.split('&').map(normaliseSegment).join('&');
|
// also replace any "slipping" file extension
|
||||||
|
return name.split('&').map(normaliseSegment).join('&').replace(/\.[a-z\d]+$/ig,'');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -203,30 +204,6 @@ export function getCanvasName(imgFilename, technique) {
|
|||||||
|
|
||||||
return normaliseName(canvasName);
|
return normaliseName(canvasName);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Get image name for given canvas
|
|
||||||
* @param {Canvas} canvas The Canvas object
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
async function getImageName(canvas) {
|
|
||||||
const images = await getImageList(canvas.resourceId);
|
|
||||||
let adjustedCanvasName = canvas.name;
|
|
||||||
|
|
||||||
// Adjust canvas name for HSI with PCA...
|
|
||||||
if (/pc(1|3)/.test(canvas.name)) {
|
|
||||||
adjustedCanvasName = canvas.name.replace(
|
|
||||||
/pc((1|3))/,
|
|
||||||
function (match, group1) {
|
|
||||||
return `_HSI_PC${group1}`;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return images.filter(i => {
|
|
||||||
const segment = i.split('_')[namePos[canvas.technique]];
|
|
||||||
return normaliseName(segment) === adjustedCanvasName;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Generate canvas label from canvasName
|
* Generate canvas label from canvasName
|
||||||
* @todo Verify for HSI filenames
|
* @todo Verify for HSI filenames
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
import Manifest from '../iiif/Manifest.js';
|
||||||
|
import { normaliseName, normaliseSegment, namePos } from './FilenameParser.js';
|
||||||
/**
|
/**
|
||||||
* Handles filesystem for image repository
|
* Handles filesystem for image repository
|
||||||
* @module ImageRepository
|
* @module ImageRepository
|
||||||
@@ -59,3 +61,34 @@ export async function getParamsFromFolders () {
|
|||||||
|
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* This extacts a segment to be used
|
||||||
|
* in getImageName(), accounting for the
|
||||||
|
* HSI special case
|
||||||
|
* @param {string} filename The image filename
|
||||||
|
* @param {string} technique
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
function getMatchableSegment(filename, technique) {
|
||||||
|
const parts = filename.split('_');
|
||||||
|
if (technique === 'hsi') {
|
||||||
|
const name = normaliseSegment(parts[1]);
|
||||||
|
const pca = parts[3].replace(/\.[a-z\d]+$/i, '').toLowerCase();
|
||||||
|
return `${name}${pca}`;
|
||||||
|
}
|
||||||
|
return normaliseName(parts[namePos[technique]]);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get image name for given canvas
|
||||||
|
* @param {string} name The Canvas name
|
||||||
|
* @param {Manifest} manifest The Manifest object
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
export async function getImageName(name, manifest) {
|
||||||
|
const images = await getImageList(manifest.resourceId);
|
||||||
|
let adjustedCanvasName = name;
|
||||||
|
|
||||||
|
return images.filter(i => {
|
||||||
|
return getMatchableSegment(i, manifest.technique) === adjustedCanvasName;
|
||||||
|
})[0];
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ import Canvas from '../iiif/Canvas.js';
|
|||||||
import Image from '../iiif/Image.js';
|
import Image from '../iiif/Image.js';
|
||||||
import ManifestMetadata from '../iiif/Metadata.js';
|
import ManifestMetadata from '../iiif/Metadata.js';
|
||||||
import { parse, getCanvasLabel, getCanvasName } from './FilenameParser.js';
|
import { parse, getCanvasLabel, getCanvasName } from './FilenameParser.js';
|
||||||
import { getImageList } from './ImageRepository.js';
|
import { getImageList, getImageName } from './ImageRepository.js';
|
||||||
import { TECH_NAMES } from '../constants.js';
|
import { TECH_NAMES } from '../constants.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -141,27 +141,6 @@ async function populateCanvases (manifest, images) {
|
|||||||
|
|
||||||
return manifest;
|
return manifest;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Get image name for given canvas
|
|
||||||
* @param {String} name The canvas name
|
|
||||||
* @param {String} manifestId The manifest (resource) id
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
async function getImageName(name, manifestId) {
|
|
||||||
const images = await getImageList(manifestId);
|
|
||||||
|
|
||||||
// Adjust canvas name for HSI with PCA...
|
|
||||||
if (/pc(1|3)/.test(name)) {
|
|
||||||
name = name.replace(
|
|
||||||
/pc((1|3))/,
|
|
||||||
function (match, group1) {
|
|
||||||
return `_HSI_PC${group1}`;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return images.filter(i => i.includes(name))[0];
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @param {Manifest} manifest The Manifest object
|
* @param {Manifest} manifest The Manifest object
|
||||||
* @param {string} imgFilename
|
* @param {string} imgFilename
|
||||||
|
|||||||
Reference in New Issue
Block a user