greek-manifests/src/Image.js
Nicolò P. b40073ca1e Add HIROXNIR (WIP)
TODO: manage fragment in canvas name (e.g. cr12_pz1)
2024-12-13 13:06:19 +01:00

122 lines
3.3 KiB
JavaScript

import IIIFResource from './IIIFResource.js';
/**
* @todo Move to common.js?!
*/
const splitter = {
HIROXNIR: splitHIROXNIR,
NIR: splitNIR,
DN: splitDNO,
DO: splitDNO,
HSI: splitHSI
};
function splitHIROXNIR(filename) {
let splitFilename = filename.split('_');
const papyrusNum = splitFilename[0].split('-')[1];
const baseFolder = `PHerc_${papyrusNum}`;
const subfolder = `PHerc_${papyrusNum}_HIROXNIR`;
return {baseFolder, subfolder};
}
function splitNIR(filename) {
let splitFilename = filename.split('_');
const papyrusNum = splitFilename[0].split('-')[2];
const baseFolder = `PHerc_${papyrusNum}`;
const subfolder = `PHerc_${papyrusNum}_${splitFilename[2].split('-')[0]}`;
return {baseFolder, subfolder};
}
/**
* @todo Redundant...
*/
function splitHSI(filename) {
let splitFilename = filename.split('_');
const papyrusNumb = splitFilename[0].split('-')[2];
const baseFolder = `PHerc_${papyrusNumb}`;
const subfolder = `PHerc_${papyrusNumb}_${splitFilename[2]}`;
return {baseFolder, subfolder};
}
function splitDNO(filename) {
let splitFilename = filename.split('_');
const baseFolder = splitFilename.slice(0,2).join('_');
const subfolder = splitFilename.slice(0,3).join('_');
return {baseFolder, subfolder};
}
/**
* @implements IIIFResource
*/
class Image {
#IIIF_API_VERSION = process.env.IIIF_API_VERSION;
id = '';
#context = `https://iiif.io/api/presentation/${this.#IIIF_API_VERSION}/context.json`;
#type = 'oa:Annotation';
#motivation = 'sc:painting';
#resType = 'dctypes:Image';
#format = 'image/jpeg';
height = 0;
width = 0;
service = {
"@context" : this.#context,
"@id" : '',
profile : `https://iiif.io/api/image/${this.#IIIF_API_VERSION}/level2.json`,
};
canvasId = '';
/**
*
* @param {string} canvasId The canvas IIIF id
*/
constructor(canvasId) {
this.canvasId = canvasId;
}
/**
* @param {number} height
* @param {number} width
*/
setSize(height, width) {
this.height = height;
this.width = width;
}
/**
* Generate IIIF id pointing to image
* server endpoint for this image
* @param {string} serviceURL The image server base URL
* @param {string} filename The image's complete filename
*/
generateID(serviceURL, filename) {
let splitFn = splitter[/((HIROX)?NIR|DO|DN|HSI)/.exec(filename)[0]];
const {baseFolder, subfolder} = splitFn(filename);
this.id = `${serviceURL}/${this.#IIIF_API_VERSION}/${baseFolder}%2F${subfolder}%2F${filename}/full/max/0/default.jpg`;
this.service['@id'] = this.id.replace(/\/full.*$/,'');
}
/**
* Object representation of this image
*
* @returns {Object}
*/
toObject() {
return {
"@context" : this.#context,
"@type" : this.#type,
motivation : this.#motivation,
resource : {
"@id" : this.id,
"@type" : this.#resType,
format: this.#format,
service : this.service,
height: this.height,
width: this.width,
},
on : this.canvasId
}
}
}
export default Image;