diff --git a/js/ocfl.js b/js/ocfl.js index b6a599a..dfb87c8 100644 --- a/js/ocfl.js +++ b/js/ocfl.js @@ -16,15 +16,12 @@ var fs = require('fs'); - +// parse the URI and either serve the index or an ocfl_object function ocfl(req) { - var ocfl_repo = req.variables.ocfl_repo; - var ocfl_root = req.variables.ocfl_root; var ocfl_solr = req.variables.ocfl_solr; - var index_file = req.variables.ocfl_autoindex; - + var parts = req.uri.split('/'); var repo = parts[1]; var oidv = parts[2]; @@ -36,168 +33,101 @@ function ocfl(req) { return; } - if( oidv ) { - req.error("solr oid lookup not done yet"); - req.return(440, "Resource not found"); - return; - } - - - // get pagination from req.variables.args - var query = "fl=name,path,uri_id&q=record_type_s:Dataset" - req.subrequest(ocfl_solr + '/select', { args: query }, ( res ) => { - send_html(req, '
' + res.responseBody + '
'); - }) - - -} - - + req.error("in ocfl: repo = " + repo); + if( oidv ) { + ocfl_object(req, repo, oidv, content) + } else { - - -// testbed for the solr handler - -var SOLR = "/solr/ocflcore/select"; - -// basic - just passes through - -function solr_bridge(req) { - req.warn("Routing subrequest to solr proxy via " + SOLR); - req.subrequest(SOLR, req.variables.args, (res) => { - req.return(res.status, res.responseBody); - }); + // get pagination from req.variables.args + var query = "fl=name,path,uri_id&q=record_type_s:Dataset" + req.subrequest(ocfl_solr + '/select', { args: query }, ( res ) => { + var solrJson = JSON.parse(res.responseBody); + var docs = solrJson['response']['docs']; + var start = solrJson['response']['start']; + send_html(req, render_index(repo, start, docs)); + }); + } } +// parse a versioned url_id, look it up in solr to find the path +// and then return the versioned ocfl content +function ocfl_object(req, repo, oidv, content) { + var pattern = new RegExp('^([^\\.]+)(\\.v\\d+)?$'); + var match = oidv.match(pattern); + if( !match ) { + req.error("Couldn't match oid " + oidv); + req.return(440, "Resource not found"); + return + } + var oid = match[1]; + var v = match[2]; - - -function ocfl_json(req) { - + var ocfl_solr = req.variables.ocfl_solr; var ocfl_repo = req.variables.ocfl_repo; var ocfl_root = req.variables.ocfl_root; var index_file = req.variables.ocfl_autoindex; - var parts = req.uri.split('/'); - var repo = parts[1]; - var oidv = parts[2]; - var content = parts.slice(3).join('/'); - - if( !repo ) { - req.error("Couldn't find match for " + req.uri); - req.return(440, "Resource not found"); - return; - } + var query = "fl=path&q=uri_id:" + oid; - load_index_json(req, ( index ) => { - if( !oidv ) { - send_html(req, render_index(index, repo)); - } else { + req.subrequest(ocfl_solr + '/select', { args: query }, ( res ) => { + var solrJson = JSON.parse(res.responseBody); + if( solrJson['response']['docs'].length === 1 ) { + var p = solrJson['response']['docs'][0]['path']; + var opath = [ ocfl_repo ].concat(p).join('/'); + + if( v ) { + v = v.substr(1); + } - var pattern = new RegExp('^([^\\.]+)(\\.v\\d+)?$'); - var match = oidv.match(pattern); - if( !match ) { - req.error("Couldn't match oid " + oidv); - req.return(440, "Resource not found"); - return + if( !content ) { + content = index_file; } - var oid = decodeURIComponent(match[1]); - var v = match[2]; - - var indexo = index_lookup(index, oid); - - if( indexo ) { - var opath = [ ocfl_repo ].concat(indexo['path']).join('/'); - if( v ) { - v = v.substr(1); - } - - if( !content ) { - content = index_file; - } - - var vpath = version(req, ocfl_root + '/' + opath, content, v); - if( vpath ) { - var newroute = '/' + opath + '/' + vpath; - req.warn("Remapped " + oid + " to " + newroute); - req.internalRedirect(newroute); - } else { - req.error("Version not found"); - req.return(440, "Version not found"); - } + + var vpath = version(req, ocfl_root + '/' + opath, content, v); + if( vpath ) { + var newroute = '/' + opath + '/' + vpath; + req.warn("Remapped " + oid + " to " + newroute); + req.internalRedirect(newroute); } else { - req.error("OID " + oid + " not found in index"); - req.return(440, "Object not found"); + req.error("Version not found"); + req.return(440, "Version not found"); } + } else { + req.error("OID " + oid + " not found in index"); + req.return(440, "Object not found"); } + }); } -function load_index_json(req, callback) { - var ocfl_root = req.variables.ocfl_root; - var ocfl_repo = req.variables.ocfl_repo; - var repo_index = req.variables.ocfl_repo_index; - var index_file = ocfl_root + '/' + ocfl_repo + '/' + repo_index; - - try { - var js = fs.readFileSync(index_file); - var index = JSON.parse(js); - callback(index); - return; - } catch(e) { - req.error("Error reading " + index_file); - req.error(e); - return null; - } -} +// pass this the repostory, the page start index and a list of objects +// with the properties url_id and name (name is an array) +function render_index(repo, start, links) { + var html = "

ocfl-nginx bridge v1.0.3

\n"; + html += "

Start: " + start + "

"; - -function index_lookup(index, id) { - var match = index.filter(i => i['uri_id'] === id); - if( match ) { - return match[0]; - } else { - return null; - } -} - -function render_index(index, url_path) { - - var html = "

ocfl-nginx bridge v1.0.2

\n"; - - index.forEach((e) => { - var entry = index_map(e); - var url = '/' + url_path + '/' + entry[0] + '/'; - html += '

' + entry[1] + '

\n' + links.forEach((l) => { + var url = '/' + repo + '/' + l['uri_id'] + '/'; + html += '

' + l['name'][0] + '

\n' }); html += '\n'; return html; -} - -// indexmap takes an index entry and returns a URL path (based on -// the id) and HTML for the link - -function index_map(entry) { - var html = entry['name']; - // if( html.length > MAX_INDEX_LENGTH ) { - // html = html.slice(0, MAX_INDEX_LENGTH) + '...'; - // } - return [ entry['uri_id'], html ]; } + function send_html(req, html) { req.status = 200; req.headersOut['Content-Type'] = "text/html; charset=utf-8";