Skip to content

Instantly share code, notes, and snippets.

@cbeer
Created June 11, 2011 15:12
Show Gist options
  • Save cbeer/1020652 to your computer and use it in GitHub Desktop.
Save cbeer/1020652 to your computer and use it in GitHub Desktop.
Apache mod_rewrite script handler that maps Fedora URI requests to filesystem paths (as possible)
#!/usr/bin/env ruby
require 'rubygems'
require 'rubydora'
require 'trollop'
opts = Trollop::options do
version "fedora-rewrite-map 0.0.1 (c) 2011 Chris Beer"
banner <<-EOS
Usage:
fedora-rewrite-map [options]
EOS
opt :user, "Fedora User", :default => 'fedoraAdmin'
opt :password, "Fedora Password", :default => 'fedora'
opt :url, "Fedora Url", :default => 'http://localhost:8983/fedora'
opt :path, "Path to the Fedora Datastream file store", :default => './fedora/data/datastreams/'
end
@repository = Rubydora::Repository.new :url => opts[:url], :user => opts[:user], :password => opts[:password]
FEDORA_DATASTREAM_STORE_PATH = File.expand_path(opts[:path])
def convert_uri_to_filesystem_path uri
pid, dsid = uri.split('/')
return nil unless pid and dsid
ds = @repository.find(pid).datastreams[dsid]
return nil unless ds and not ds.new?
location = ds.profile["dsLocation"]
case ds.profile["dsLocationType"]
when "INTERNAL_ID"
path_on_filesystem(location)
else
location
end
end
def path_on_filesystem location
%x[find #{FEDORA_DATASTREAM_STORE_PATH} -name #{location.gsub(':', '_')}].split("\n").first.strip
end
while uri = gets
puts "#{convert_uri_to_filesystem_path(uri.strip)}\n" rescue "\n"
end
#!/bin/sh
./fedora-rewrite-map # --user [...] --password [...] etc
RewriteEngine On
RewriteMap fedora-map prg:/path/to/fedora-rewrite-map-init
RewriteCond %{REQUEST_URI} Proxy # map just URIs with Proxy in them..
RewriteRule ^/fedora/get/([a-zA-Z0-9/:\-\.]+)$ ${fedora-map:$1} [QSA] # map fedora requests to the fedora mapping script.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment