Skip to content

Commit

Permalink
Simple index commit
Browse files Browse the repository at this point in the history
  • Loading branch information
spikelynch committed Jul 26, 2019
1 parent 24bdbeb commit e6d5eab
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Put a proper test framework around versioning.

Expose versions to the web using a protocol such as memento

### Basic index

Provide an index (like nginx's auto-index) when pointing at the base of an ocfl repository


13 changes: 7 additions & 6 deletions conf.d/ocfl.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ js_include js/ocfl.js;

server {
listen 8080;
server_name localhost;
server_name 0.0.0.0;

set $ocfl_files /usr/share/nginx/html;
set $ocfl_index_file CATALOG.html;
set $ocfl_root /usr/share/nginx/html;
set $ocfl_autoindex CATALOG.html;
set $ocfl_repo_index index.json;

location /staging_ocfl/ {
autoindex on;
index CATALOG.html;
root $ocfl_files;
root $ocfl_root;
}

location /public_ocfl/ {
autoindex on;
index CATALOG.html;
root $ocfl_files;
root $ocfl_root;
}

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

location /public/ {
Expand Down
73 changes: 44 additions & 29 deletions js/ocfl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var fs = require('fs');


// ocfl(request)
//
// entry-point from nginx
Expand All @@ -12,39 +11,18 @@ var fs = require('fs');
// ocfl_repo - the nginx location mapped to the actual ocfl file root


function ocfl(req) {
var url_path = req.variables.ocfl_path;
var ocfl_repo = req.variables.ocfl_repo;
var index_file = req.variables.ocfl_index_file;
var pattern = new RegExp(url_path + '/([^/]+)/(.*)$');
var match = req.uri.match(pattern);
if( !match ) {
req.error("Match failed " + pattern);
req.return(500, "ocfl - url match failed");
} else {
var oid = match[1];
var content = match[2] || index_file;
var object = pairtree(oid);
var opath = [ ocfl_repo ].concat(object).join('/');
var newroute = '/' + opath + '/v1/content/' + content;
req.warn("Remapped " + oid + " to " + newroute);
req.internalRedirect(newroute);
}
}


// version with versions
function ocfl(req) {

function ocfl_versioned(req) {
var url_path = req.variables.ocfl_path;
var ocfl_repo = req.variables.ocfl_repo;
var ocfl_files = req.variables.ocfl_files;
var index_file = req.variables.ocfl_index_file;
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 ) {
req.error("Match failed " + pattern);
req.return(500, "ocfl - url match failed");
repository_index(req);
} else {
var oid = match[1];
var v = match[2];
Expand All @@ -55,7 +33,7 @@ function ocfl_versioned(req) {
if( v ) {
v = v.substr(1);
}
var vpath = version(req, ocfl_files + '/' + opath, content, v);
var vpath = version(req, ocfl_root + '/' + opath, content, v);
if( vpath ) {
var newroute = '/' + opath + '/' + vpath;
req.warn("Remapped " + oid + " to " + newroute);
Expand All @@ -68,6 +46,40 @@ function ocfl_versioned(req) {
}


// see if this can return json or html

function repository_index(req) {
var ocfl_repo = req.variables.ocfl_repo;
var repo_index = req.variables.ocfl_repo_index;
var index_route = '/' + ocfl_repo + '/' + repo_index;

// this could be a subrequest for an index.json which is
// generated from a database

try {
var js = fs.readFileSync(index_json);
var index = JSON.parse(js);

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

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

html += '</body>';
req.return(200, html);
} catch(e) {
req.error("Error reading " + index_json);
req.error(e);
return null;
}
}


}




function version(req, object, payload, version) {
var inv = load_inventory(req, object);
Expand Down Expand Up @@ -97,7 +109,8 @@ function load_inventory(req, object) {
var ifile = object + 'inventory.json';
try {
var contents = fs.readFileSync(ifile);
return JSON.parse(contents);
var ijs = JSON.parse(contents);

} catch(e) {
req.error("Error reading " + ifile);
req.error(e);
Expand All @@ -106,6 +119,8 @@ function load_inventory(req, object) {
}




// adapted from npm pairtree


Expand Down

0 comments on commit e6d5eab

Please sign in to comment.