diff --git a/make-ocfl-index.js b/make-ocfl-index.js index 0fef97fddea91c0e2949e8b8ecbbaf8cb0f818a3..b0d3cabb12ae2f0aa3694f0d7c1e5272d0decdea 100644 --- a/make-ocfl-index.js +++ b/make-ocfl-index.js @@ -10,6 +10,7 @@ var Utils = require("ro-crate").Utils var ROCrate = require('ro-crate').ROCrate; var utils = new Utils(); +const ID_NAMESPACE = 'ocfl:paradisec.org.au'; // This is an asynchronous library so you need to call using await, or use promises @@ -17,6 +18,7 @@ var utils = new Utils(); async function main() { // OCFL repo - exsists? const repoPath = argv.repo ? argv.repo : "ocfl_demo"; + const id_namespace = argv.id ? argv.id : ID_NAMESPACE; // Make or open a database // Get or open an OCFL repo var repo = new OCFLRepository(); @@ -41,12 +43,13 @@ async function main() { crate = new ROCrate(json); crate.index(); var dataset = crate.getRootDataset() - console.log(dataset['@id'], dataset['description'], p); + var identifier = getIdentifier(dataset, id_namespace); + console.log(identifier, dataset.name, p); var newItem = { - "@id": dataset["@id"], + "@id": identifier, + uri_id: identifier, name: utils.asArray(dataset.name)[0], description: utils.asArray(dataset.description)[0], - datePublished: utils.asArray(dataset.name)[0], path: object.path } index.push(newItem); @@ -67,5 +70,25 @@ async function main() { } } + + +// this needs to be indirect + + +function getIdentifier(dataset, idns) { + if( dataset['identifier'] ) { + console.log + const m = dataset['identifier'].filter((i) => i['name'] === idns); + if( m.length > 0 ) { + return m[0]['@id']; + } else { + console.log("Couldn't resolve identifier"); + return undefined; + } + } +} + + + main(); diff --git a/package.json b/package.json index 0abea9a3a950f1946caefdae014cd72d189923d3..9e0afa158de9e1a90ab1e6f0f751ed98a07fd742 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,11 @@ "license": "GPL-3.0", "dependencies": { "fs-extra": "^8.1.0", + "jsonld": "^1.6.2", + "nunjucks": "^3.2.0", "ocfl": "^1.0.3", - "ro-crate": "^1.0.1", + "ro-crate": "^1.2.10", + "uuid": "^3.3.3", "yargs": "^13.3.0" } } diff --git a/ro-crate-deposit.js b/ro-crate-deposit.js index 1dd487bccc515135fd4b8b16413e9c9028461457..493f7f76398130c6ac3df2e072f3b34f943fe01b 100644 --- a/ro-crate-deposit.js +++ b/ro-crate-deposit.js @@ -1,40 +1,78 @@ const path = require('path'); const fs = require('fs-extra'); -const ocfl = require("ocfl"); -console.log(ocfl); +const ocfl = require('ocfl'); const OCFLRepository = require('ocfl').Repository; -var argv = require('yargs').argv; -var ROCrate = require('ro-crate').ROCrate; +const uuid = require('uuid/v4'); +const argv = require('yargs').argv; +const ROCrate = require('ro-crate').ROCrate; +async function connectRepo(repoPath) { + const repo = new OCFLRepository(); + try { + const stat = await fs.stat(repoPath); + if( stat.isDirectory() ) { + await repo.load(repoPath); + return repo; + } else { + console.error(`${repoPath} is not a directory`); + process.exit(-1); + } + } catch(e) { + await fs.mkdir(repoPath); + await repo.create(repoPath); + return repo; + } +} + + +async function checkin(repo, repoName, rocratePath) { + const rocrateFile = path.join(rocratePath, "ro-crate-metadata.jsonld"); + try { + const jsonld = await fs.readJson(rocrateFile); + const crate = new ROCrate(jsonld); + crate.index(); + const dataset = crate.getRootDataset(); + + console.log("Ingesting ro-crate " + dataset['name']); + + const existingId = crate.getNamedIdentifier(repoName); + + if( existingId ) { + console.log(`Local identifier found ${repoName}/${existingId}`); + await repo.importNewObjectDir(existingId, rocratePath); + console.log(`Updated ${existingId}`); + } else { + const newId = uuid(); + console.log(`Minting new local identifier ${repoName}/${newId}`); + await repo.importNewObjectDir(newId, rocratePath); + console.log(`Imported ${rocratePath} ${dataset['name']} ${newId}`); + crate.addIdentifier({name: repoName, identifier: newId}); + await fs.writeJson(rocrateFile, crate.getJson(), {spaces: 2}); + await repo.importNewObjectDir(newId, rocratePath); + console.log(`Updated ${rocratePath} ${newId} metadata with identifier`); + } + } catch(e) { + console.log(`Error importing ${rocratePath}`); + console.log(e); + } +} + + async function main() { - // OCFL repo - exsists? - const repoPath = argv.repo ? argv.repo : "ocfl_demo"; - console.log(argv._); - // Get or open an OCFL repo - var repo = new OCFLRepository(); - // TODO - Load as an OCFL object to get ID use instead of fromPath below + + const repoPath = argv.repo || "ocfl_demo"; + const repoName = argv.name || "ocfl_demo"; + const repo = await connectRepo(repoPath); - if (fs.existsSync(repoPath)) { - var init = await repo.load(repoPath); - } else { - fs.mkdirSync(repoPath); - init = await repo.create(repoPath) - } - for (let fromPath of argv._) { - var rocrateFile = path.join(fromPath, "ro-crate-metadata.jsonld"); - if (fs.existsSync(rocrateFile)){ - var json = JSON.parse(fs.readFileSync(rocrateFile)); - crate = new ROCrate(json); - crate.index(); - var dataset = crate.getRootDataset(); - const new_object1 = await repo.importNewObjectDir(dataset["@id"], fromPath); - console.log(new_object1); - } + for (let rocratePath of argv._) { + await checkin(repo, repoName, rocratePath); } } + + main();