-
Notifications
You must be signed in to change notification settings - Fork 11
/
web.rb
289 lines (251 loc) · 9.3 KB
/
web.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
require 'ruby-filemagic'
require 'fileutils'
###
# Configuration
###
def boolean_env env_var
ENV[env_var] and ENV[env_var].downcase == 'true'
end
if ENV['MU_APPLICATION_FILE_STORAGE_PATH'] and ENV['MU_APPLICATION_FILE_STORAGE_PATH'].start_with?('/')
log.fatal "MU_APPLICATION_FILE_STORAGE_PATH (#{ENV['MU_APPLICATION_FILE_STORAGE_PATH']}) must be relative"
exit
else
FileUtils.mkdir_p "/share/#{ENV['MU_APPLICATION_FILE_STORAGE_PATH']}"
end
configure do
set :relative_storage_path, (ENV['MU_APPLICATION_FILE_STORAGE_PATH'] || '').chomp('/')
set :storage_path, "/share/#{(ENV['MU_APPLICATION_FILE_STORAGE_PATH'] || '')}".chomp('/')
set :file_resource_base, (ENV['FILE_RESOURCE_BASE'] || '')
set :validate_readable_metadata, boolean_env('VALIDATE_READABLE_METADATA')
end
file_magic = FileMagic.new(FileMagic::MAGIC_MIME)
###
# Vocabularies
###
DC = RDF::Vocab::DC
NFO = RDF::Vocabulary.new('http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#')
NIE = RDF::Vocabulary.new('http://www.semanticdesktop.org/ontologies/2007/01/19/nie#')
DBPEDIA = RDF::Vocabulary.new('http://dbpedia.org/ontology/')
###
# Supporting functions
###
def get_file_info file_uuid
query = " SELECT ?uri ?name ?format ?size ?extension FROM <#{graph}> WHERE {"
query += " ?uri <#{MU_CORE.uuid}> #{Mu::sparql_escape_string(file_uuid)} ;"
query += " <#{NFO.fileName}> ?name ;"
query += " <#{DC.format}> ?format ;"
query += " <#{DBPEDIA.fileExtension}> ?extension ;"
query += " <#{NFO.fileSize}> ?size ."
query += " }"
Mu::query(query)
end
class UnauthorizedError < StandardError; end
###
# POST /files
# Upload a new file. Results in 2 new nfo:FileDataObject resources: one representing
# the uploaded file and one representing the persisted file on disk generated from the upload.
#
# Accepts multipart/form-data with a 'file' parameter containing the file to upload
#
# Returns 201 on successful upload of the file
# 400 if X-Rewrite header is missing
# if file param is missing
###
post '/files/?' do
rewrite_url = Mu::Helpers::rewrite_url_header(request)
Mu::Helpers::error('X-Rewrite-URL header is missing.') if rewrite_url.nil?
Mu::Helpers::error('File parameter is required.') if params['file'].nil?
begin
tempfile = params['file'][:tempfile]
upload_resource_uuid = Mu::generate_uuid()
upload_resource_name = params['file'][:filename]
upload_resource_uri = "#{settings.file_resource_base}#{upload_resource_uuid}"
file_format = file_magic.file(tempfile.path)
file_extension = upload_resource_name.split('.').last
file_size = File.size(tempfile.path)
file_resource_uuid = Mu::generate_uuid()
file_resource_name = "#{file_resource_uuid}.#{file_extension}"
file_resource_uri = file_to_shared_uri(file_resource_name)
now = DateTime.now
physical_file_path = "#{settings.storage_path}/#{file_resource_name}"
FileUtils.copy(tempfile.path, physical_file_path)
query = " INSERT DATA {"
query += " GRAPH <#{Mu::graph}> {"
query += " #{Mu::sparql_escape_uri(upload_resource_uri)} a <#{NFO.FileDataObject}> ;"
query += " <#{NFO.fileName}> #{upload_resource_name.sparql_escape} ;"
query += " <#{MU_CORE.uuid}> #{upload_resource_uuid.sparql_escape} ;"
query += " <#{DC.format}> #{file_format.sparql_escape} ;"
query += " <#{NFO.fileSize}> #{Mu::sparql_escape_int(file_size)} ;"
query += " <#{DBPEDIA.fileExtension}> #{file_extension.sparql_escape} ;"
query += " <#{DC.created}> #{now.sparql_escape} ;"
query += " <#{DC.modified}> #{now.sparql_escape} ."
query += " #{Mu::sparql_escape_uri(file_resource_uri)} a <#{NFO.FileDataObject}> ;"
query += " <#{NIE.dataSource}> #{Mu::sparql_escape_uri(upload_resource_uri)} ;"
query += " <#{NFO.fileName}> #{file_resource_name.sparql_escape} ;"
query += " <#{MU_CORE.uuid}> #{file_resource_uuid.sparql_escape} ;"
query += " <#{DC.format}> #{file_format.sparql_escape} ;"
query += " <#{NFO.fileSize}> #{Mu::sparql_escape_int(file_size)} ;"
query += " <#{DBPEDIA.fileExtension}> #{file_extension.sparql_escape} ;"
query += " <#{DC.created}> #{now.sparql_escape} ;"
query += " <#{DC.modified}> #{now.sparql_escape} ."
query += " }"
query += " }"
Mu::update(query)
if settings.validate_readable_metadata && get_file_info(file_resource_uuid).empty?
raise UnauthorizedError.new "Could not read metadata of file."
else
content_type 'application/vnd.api+json'
status 201
{
data: {
type: 'files',
id: upload_resource_uuid,
attributes: {
name: upload_resource_name,
format: file_format,
size: file_size,
extension: file_extension
}
},
links: {
self: "#{rewrite_url.chomp '/'}/#{upload_resource_uuid}"
}
}.to_json
end
rescue UnauthorizedError => e
log.warn "#{e} Cleaning up."
File.delete physical_file_path if File.exist? physical_file_path
status 403
rescue SPARQL::Client::MalformedQuery, SPARQL::Client::ClientError, SPARQL::Client::ServerError => e
log.warn "Something went wrong while upload file. Cleaning up. #{e}"
File.delete physical_file_path if File.exist? physical_file_path
status 500
end
end
###
# GET /files/:id
# Get metadata of the file with the given id
#
# Returns 200 containing the file with the specified id
# 404 if a file with the given id cannot be found
###
get '/files/:id' do
rewrite_url = Mu::rewrite_url_header(request)
error('X-Rewrite-URL header is missing.') if rewrite_url.nil?
result = get_file_info query(params['id'])
return status 404 if result.empty?
result = result.first
content_type 'application/vnd.api+json'
status 200
{
data: {
type: 'files',
id: params['id'],
attributes: {
name: result[:name].value,
format: result[:format].value,
size: result[:size].value,
extension: result[:extension].value
}
},
links: {
self: rewrite_url
}
}.to_json
end
###
# GET /files/:id/download?name=foo.pdf
#
# @param name [string] Optional name of the downloaded file
#
# Returns 200 with the file content as attachment
# 404 if a file with the given id cannot be found
# 500 if the file is available in the database but not on disk
###
get '/files/:id/download' do
query = " SELECT ?fileUrl FROM <#{Mu::graph}> WHERE {"
query += " ?uri <#{MU_CORE.uuid}> #{Mu::sparql_escape_string(params['id'])} ."
query += " ?fileUrl <#{NIE.dataSource}> ?uri ."
query += " }"
result = Mu::query(query)
return status 404 if result.empty?
url = result.first[:fileUrl].value
path = shared_uri_to_path(url)
filename = params['name']
filename ||= File.basename(path)
if params['content-disposition'] and params['content-disposition'].casecmp? 'inline'
disposition = 'inline'
else
disposition = 'attachment'
end
if File.file?(path)
send_file path, disposition: disposition, filename: filename
else
Mu::Helpers::error("Could not find file in path. Check if the physical file is available on the server and if this service has the right mountpoint.", 500)
end
end
###
# DELETE /files/:id
# Delete a file and its metadata
#
# Returns 204 on successful removal of the file and metadata
# 404 if a file with the given id cannot be found
###
delete '/files/:id' do
query = " SELECT ?uri ?fileUrl FROM <#{Mu::graph}> WHERE {"
query += " ?uri <#{MU_CORE.uuid}> #{Mu::sparql_escape_string(params['id'])} ."
query += " ?fileUrl <#{NIE.dataSource}> ?uri ."
query += " }"
result = Mu::query(query)
return status 404 if result.empty?
# NOTE: this is split in two queries because it's lighter on the
# triplestore. Ideally mu-auth can split these queries in smarter and
# lighter way to alleviate the triplestore when the data lives in more
# than one graph.
delete_query = "
DELETE WHERE {
GRAPH <#{Mu::graph}> {
<#{result.first[:uri]}> a <#{NFO.FileDataObject}> ;
<#{NFO.fileName}> ?upload_name ;
<#{MU_CORE.uuid}> ?upload_id ;
<#{DC.format}> ?upload_format ;
<#{DBPEDIA.fileExtension}> ?upload_extension ;
<#{NFO.fileSize}> ?upload_size ;
<#{DC.created}> ?upload_created ;
<#{DC.modified}> ?upload_modified .
}
}
;
DELETE WHERE {
GRAPH <#{Mu::graph}> {
<#{result.first[:fileUrl]}> a <#{NFO.FileDataObject}> ;
<#{NIE.dataSource}> <#{result.first[:uri]}> ;
<#{NFO.fileName}> ?fileName ;
<#{MU_CORE.uuid}> ?id ;
<#{DC.format}> ?format ;
<#{DBPEDIA.fileExtension}> ?extension ;
<#{NFO.fileSize}> ?size ;
<#{DC.created}> ?created ;
<#{DC.modified}> ?modified .
}
}
"
Mu::update(delete_query)
url = result.first[:fileUrl].value
path = shared_uri_to_path(url)
File.delete path if File.exist? path
status 204
end
###
# Helpers
###
def shared_uri_to_path(uri)
uri.sub('share://', '/share/')
end
def file_to_shared_uri(file_name)
if settings.relative_storage_path and not settings.relative_storage_path.empty?
return "share://#{settings.relative_storage_path}/#{file_name}"
else
return "share://#{file_name}"
end
end