diff --git a/make-ocfl-index.js b/make-ocfl-index.js index 5cecc6813f5e1dfab0d38caecc300d62a586a956..b0d3cabb12ae2f0aa3694f0d7c1e5272d0decdea 100644 --- a/make-ocfl-index.js +++ b/make-ocfl-index.js @@ -72,8 +72,12 @@ 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']; diff --git a/package.json b/package.json index d3c0ed4d95cc9e15de56f24fdadd65f0709e1044..d1a1fd63307a99404bd760ecdd10550813ce64fa 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "jsonld": "^1.6.2", "nunjucks": "^3.2.0", "ocfl": "^1.0.3", - "ro-crate": "^1.0.1", + "ro-crate": "^1.2.8", + "uuid": "^3.3.3", "yargs": "^13.3.0" } } diff --git a/ro-crate-deposit.js b/ro-crate-deposit.js index 1dd487bccc515135fd4b8b16413e9c9028461457..2041b8cb9f6e1689f5a19900a32627090a878087 100644 --- a/ro-crate-deposit.js +++ b/ro-crate-deposit.js @@ -1,40 +1,59 @@ 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 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); + const rocrateFile = path.join(fromPath, "ro-crate-metadata.jsonld"); + try { + const jsonld = await fs.readJson(rocrateFile); + crate = new ROCrate(jsonld); crate.index(); - var dataset = crate.getRootDataset(); - const new_object1 = await repo.importNewObjectDir(dataset["@id"], fromPath); - console.log(new_object1); - } + const dataset = crate.getRootDataset(); + const local_id = uuid(); + crate.addIdentifier({name: repoName, identifier: local_id}); + await fs.writeJson(crate.getJson(), rocrateFile); + const ocflObject = await repo.importNewObjectDir(local_id, fromPath); + console.log(`Imported ${fromPath} ${dataset['title']} ${local_id}`); + } catch(e) { + console.log(`Error importing ${fromPath}`); + console.log(e); + } } } + + main();