Add Image resource + fake controllers

This commit is contained in:
Nicolò P 2023-10-03 15:27:10 +02:00
parent e401f3253b
commit 2e39e6256d
6 changed files with 104 additions and 15 deletions

20
controllers/canvas.mjs Normal file
View File

@ -0,0 +1,20 @@
import Canvas from '../src/Canvas.js';
import Image from '../src/Image.js';
/**
* Generate a canvas object to serve
* @param {string} manifestId The corresponding manifest's id
* @param {int|string} name The canvas name
*/
export default function generateCanvas(manifestId, name) {
const IIIF_API_VERSION = process.env.IIIF_API_VERSION;
const BASE_URL = process.env.BASE_URL;
const canvas = new Canvas(IIIF_API_VERSION, BASE_URL);
canvas.generateID(manifestId, name);
const image = new Image(4000, 3000);
canvas.setThumbnail();
canvas.addImage(image);
return canvas.toObject();
}

24
controllers/manifest.mjs Normal file
View File

@ -0,0 +1,24 @@
import Manifest from '../src/Manifest.js';
import Sequence from '../src/Sequence.js';
import Canvas from '../src/Canvas.js';
import Image from '../src/Image.js';
/**
* Generate a manifest object to serve
* @param {string} manifestId
*/
export default function generateManifest(manifestId) {
const IIIF_API_VERSION = process.env.IIIF_API_VERSION;
const BASE_URL = process.env.BASE_URL;
const manifest = new Manifest(IIIF_API_VERSION, BASE_URL);
const sequence = new Sequence();
const canvas = new Canvas(IIIF_API_VERSION, BASE_URL);
const image = new Image(4000, 3000);
canvas.setThumbnail();
canvas.addImage(image);
sequence.addCanvas(canvas);
manifest.addSequence(sequence);
manifest.generateID(manifestId);
return manifest.toObject();
}

View File

@ -1,24 +1,19 @@
import Manifest from '../src/Manifest.js'; import generateManifest from '../controllers/manifest.mjs';
import Sequence from '../src/Sequence.js'; import generateCanvas from '../controllers/canvas.mjs';
import Canvas from '../src/Canvas.js';
import express from 'express'; import express from 'express';
let router = express.Router(); let router = express.Router();
/* GET manifest JSON */ /* GET manifest JSON */
router.get('/iiif/:manifestid/manifest', function(req, res, next) { router.get('/iiif/:manifestid/manifest', function(req, res) {
const IIIF_API_VERSION = process.env.IIIF_API_VERSION; const manifest = generateManifest(req.params.manifestid)
const BASE_URL = process.env.BASE_URL; res.json(manifest);
});
const manifest = new Manifest(IIIF_API_VERSION, BASE_URL); /* GET manifest JSON */
const sequence = new Sequence(); router.get('/iiif/:manifestid/canvas/:name', function(req, res) {
const canvas = new Canvas(IIIF_API_VERSION, BASE_URL); const canvas = generateCanvas(req.params.manifestid, req.params.name)
canvas.setThumbnail(); res.json(canvas);
sequence.addCanvas(canvas);
manifest.addSequence(sequence);
manifest.generateID(req.params.manifestid);
res.json(manifest.toObject());
}); });
export default router; export default router;

View File

@ -1,4 +1,5 @@
import IIIFResource from './IIIFResource.js'; import IIIFResource from './IIIFResource.js';
import Image from './Image.js';
/** /**
* @implements IIIFResource * @implements IIIFResource
*/ */
@ -29,6 +30,12 @@ class Canvas {
setLabel(label) { setLabel(label) {
this.label = label; this.label = label;
} }
/**
* @param {Image} image Add an image to the canvas
*/
addImage(image) {
this.images.push(image.toObject());
}
/** /**
* Add a thumbnail object * Add a thumbnail object
* @todo Support multiple thumbs? * @todo Support multiple thumbs?
@ -52,6 +59,7 @@ class Canvas {
"@id" : this.id, "@id" : this.id,
"@type" : this.type, "@type" : this.type,
"label" : this.label, "label" : this.label,
"images" : this.images,
"thumbnail" : this.thumbnail "thumbnail" : this.thumbnail
} }
} }

39
src/Image.js Normal file
View File

@ -0,0 +1,39 @@
import IIIFResource from './IIIFResource.js';
/**
* @implements IIIFResource
*/
class Image {
/**
* @var {string} id A URL pointing to the image resource
*/
id = '';
type = 'dctypes:Image';
format = 'image/jpeg';
height = 0;
width = 0;
service = {};
constructor(height, width) {
this.height = height;
this.width = width;
}
/**
* Object representation of
* image resource
* @returns {object}
*/
toObject() {
return {
resource : {
"@id" : this.id,
"@type" : this.type,
format: this.format,
service : this.service,
height: this.height,
width: this.width,
}
}
}
}
export default Image;

View File

@ -28,6 +28,9 @@ class Manifest {
this.id = `${this.BASE_URL}/iiif/${idParam}/manifest` ; this.id = `${this.BASE_URL}/iiif/${idParam}/manifest` ;
} }
/** /**
* Object representation of this
* manifest
*
* @todo Implement * @todo Implement
* @returns {object} * @returns {object}
*/ */