-
Notifications
You must be signed in to change notification settings - Fork 3
/
application.rb
executable file
·119 lines (97 loc) · 2.62 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
#!/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
protect do
get '/' do
session[:current_user] = DSpaceCSV::Conf.users[auth.credentials.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
@map_file = DSpaceCSV.submit(session[:path], session["collection_id"], session["current_user"])
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"]
require 'ruby-debug'; debugger
haml :upload_finished
end
end
authorize do |username, password|
DSpaceCSV.authenticate(username, password)
end
run! if app_file == $0
end