forked from revskill10/CSV-To-DSpace-XML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.rb
executable file
·193 lines (159 loc) · 5.45 KB
/
application.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env ruby
require 'rack/timeout'
require 'sinatra'
require 'sinatra/basic_auth'
require 'sinatra/base'
require 'sinatra/flash'
require 'sinatra/redirect_with_flash'
require_relative "./environment"
class DSpaceCsvGui < Sinatra::Base
mime_type :csv, 'application/csv'
register Sinatra::Flash
register Sinatra::BasicAuth
helpers Sinatra::RedirectWithFlash
enable :sessions
use Rack::Timeout
Rack::Timeout.timeout = 9_000_000
helpers do
include Sinatra::RedirectWithFlash
include Rack::Utils
alias_method :h, :escape_html
def get_dir_structure(dir)
res = []
Dir.entries(dir).each do |e|
if e.match /^[\d]{4}/
res << [e, get_dir_content(File.join(dir, e))]
end
end
res
end
private
def get_dir_content(dir)
res = []
Dir.entries(dir).each do |e|
next if e.match /^[\.]{1,2}$/
res << [e, '']
if ['contents', 'dublin_core.xml'].include?(e)
res[-1][1] = open(File.join(dir, e), "r:utf-8").read
end
end
res
end
end
###########################################################################
# API
###########################################################################
def rest_request(params)
current_user = DSpaceCSV.api_key_authorization(params, request.path) || DSpaceCSV.password_authorization(params)
if current_user
if params["format"] == "xml"
content_type 'text/xml', :charset => 'utf-8'
elsif params["format"] == "json"
content_type 'application/json', :charset => 'utf-8'
else
content_type 'text/plain', :charset => 'utf-8'
end
RestClient.get(DSpaceCSV::Conf.dspace_repo + request.fullpath)
else
yield
end
end
get '/rest/users.:format' do
rest_request(params) { throw(:halt, [401, "Not authorized. Did you submit correct email/password, or API key/digest pair?\n"]) }
end
get '/rest/users/:id.:format' do
rest_request(params) { throw(:halt, [401, "Not authorized. Did you submit correct email and password?\n"]) }
end
get '/rest/items.:format' do
rest_request(params) { throw(:halt, [401, "Not authorized. Did you submit correct email and password?\n"]) }
end
get '/rest/items/:id.:format' do
rest_request(params) { throw(:halt, [401, "Not authorized. Did you submit correct email and password?\n"]) }
end
get '/rest/collections.:format' do
rest_request(params) { throw(:halt, [401, "Not authorized. Did you submit correct email and password?\n"]) }
end
get '/rest/collections/:id.:format' do
rest_request(params) { throw(:halt, [401, "Not authorized. Did you submit correct email and password?\n"]) }
end
get '/rest/communities/:id.:format' do
rest_request(params) { throw(:halt, [401, "Not authorized. Did you submit correct email and password?\n"]) }
end
get '/rest/communities.:format' do
rest_request(params) { throw(:halt, [401, "Not authorized. Did you submit correct email and password?\n"]) }
end
get '/rest/handle/:num1/:num2.:format' do
params["handle"] = "%s/%s" % [params["num1"], params["num2"]]
handle = Handle.where(:handle => params["handle"]).first
path = handle ? handle.path : nil
if path
redirect(request.fullpath.gsub(request.path_info, "%s.%s" % [path, params["format"]]), 303)
else
throw(:halt, [404, "Unknown handle %s" % params["handle"]])
end
end
#takes handles in the following format /handle.xml?handle=http://hdl.handle.net/123/123
get '/rest/handle.:format' do
handle = params[:handle] ? Handle.where(:handle => params["handle"].gsub("http://hdl.handle.net/", '')).first : nil
path = handle ? handle.path : nil
if path
redirect(request.fullpath.gsub(request.path_info, "%s.%s" % [path, params["format"]]), 303)
else
throw(:halt, [404, "Unknown handle %s" % params["handle"]])
end
end
protect do
get '/' do
session[:current_user] = Eperson.where(:email => auth.credentials.first).first
haml :index
end
get '/formatting-rules' do
erb :rules
end
get '/stsrepository-instructions' do
erb :sts
end
get '/extra-help' do
erb :help
end
get 'template.csv' do
content_type :csv
send_file 'template.csv'
end
post '/upload' do
begin
DSpaceCSV::Uploader.clean(1)
u = DSpaceCSV::Uploader.new(params)
e = DSpaceCSV::Expander.new(u)
t = DSpaceCSV::Transformer.new(e)
if t.errors.empty?
session[:path] = t.path
session[:collection_id] = params["collection_id"]
redirect '/upload_result', :warning => t.warnings[0]
else
redirect "/", :error => t.errors.join("<br/>")
end
rescue DSpaceCSV::CsvError => e
redirect "/", :error => e.message
rescue DSpaceCSV::UploadError => e
redirect "/", :error => e.message
end
end
post '/submit' do
dscsv= DSpaceCSV.new(session[:path], session[:collection_id], session[:current_user])
@map_file = dscsv.submit
redirect '/upload_finished?map_file=' + URI.encode(@map_file)
end
get '/upload_result' do
haml :upload_result
end
get '/upload_finished' do
@map_file = params["map_file"]
haml :upload_finished
end
end
authorize do |username, password|
!!DSpaceCSV.password_authorization({"email" => username, "password" => password})
end
run! if app_file == $0
end