-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.js
27 lines (25 loc) · 823 Bytes
/
writer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const fs = require('fs-extra')
const path = require('path')
module.exports = class ManifestWriter {
constructor(iiifConfig) {
this.baseURI = iiifConfig.baseURI
this.outputRoot = iiifConfig.dirs.outputRoot
}
/**
* Write an IIIF manifest to the file system
*
* @param {Object} manifest IIIF manifest
*/
write(manifest) {
if (!manifest) return { errors: ['Error writing manifest. Manifest is undefined.'] }
const uri = new URL(manifest.id)
const outputPath = path.join(this.outputRoot, uri.pathname)
try {
fs.ensureDirSync(path.parse(outputPath).dir)
fs.writeJsonSync(outputPath, manifest, { spaces: 2 })
return { messages: [`Generated manifest ${outputPath}`] }
} catch(error) {
return { errors: [`Failed to write manifest. ${error}`] }
}
}
}