Skip to content

Commit

Permalink
Index is working, rejigged the URL regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
spikelynch committed Jul 26, 2019
1 parent cb50ae6 commit 4377788
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
3 changes: 0 additions & 3 deletions conf.d/ocfl.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ server {

location /staging/ {
set $ocfl_repo staging_ocfl;
set $ocfl_path staging;
js_content ocfl;
}


location /public/ {
set $ocfl_repo public_ocfl;
set $ocfl_path public;
js_content ocfl;
}

Expand Down
48 changes: 32 additions & 16 deletions js/ocfl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,42 @@ var fs = require('fs');
// ocfl(request)
//
// entry-point from nginx
//
// expects two parameters set on the request variables:
// ocfl_path - the part of the URL path before the OID in the incoming URL
// ocfl_repo - the nginx location mapped to the actual ocfl file root


// Note: the regexp here requires URLs like

// /REPO_NAME/OID.vN/path/to/content

// ie the REPO_NAME can't have '/' in it

function ocfl(req) {

var url_path = req.variables.ocfl_path;
var ocfl_repo = req.variables.ocfl_repo;
var ocfl_root = req.variables.ocfl_root;
var index_file = req.variables.ocfl_autoindex;

var pattern = new RegExp(url_path + '/([^/\\.]+)(\\.v\\d+)?/(.*)$');
var match = req.uri.match(pattern);
if( !match ) {
repository_index(req);
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;
} else if( !oidv ) {
repository_index(req, repo);
} else {
var oid = match[1];
var v = match[2];
var content = match[3] || index_file;

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[0];
var v = match[1];
var object = pairtree(oid);
var opath = [ ocfl_repo ].concat(object).join('/');

Expand All @@ -48,7 +62,8 @@ function ocfl(req) {

// see if this can return json or html

function repository_index(req) {
function repository_index(req, url_path) {

var ocfl_root = req.variables.ocfl_root;
var ocfl_repo = req.variables.ocfl_repo;
var repo_index = req.variables.ocfl_repo_index;
Expand All @@ -63,8 +78,8 @@ function repository_index(req) {

var html = "<html><body>";

Object.keys(js).sort().forEach((k) => {
html += '<p><a href="/' + k + '/">' + k + '</a></p>'
Object.keys(index).sort().forEach((k) => {
html += '<p><a href="/' + url_path + '/' + k + '/">' + k + '</a></p>'
});

html += '</body>';
Expand Down Expand Up @@ -115,9 +130,10 @@ function version(req, object, payload, version) {
function load_inventory(req, object) {
var ifile = object + 'inventory.json';
try {
req.log("Trying to read " + ifile);
var contents = fs.readFileSync(ifile);
var ijs = JSON.parse(contents);

return ijs;
} catch(e) {
req.error("Error reading " + ifile);
req.error(e);
Expand Down

0 comments on commit 4377788

Please sign in to comment.