-
Notifications
You must be signed in to change notification settings - Fork 51
/
media_objects_controller.rb
369 lines (321 loc) · 12.8 KB
/
media_objects_controller.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Copyright 2011-2015, The Trustees of Indiana University and Northwestern
# University. Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# --- END LICENSE_HEADER BLOCK ---
require 'avalon/controller/controller_behavior'
class MediaObjectsController < ApplicationController
include Avalon::Workflow::WorkflowControllerBehavior
include Avalon::Controller::ControllerBehavior
include ConditionalPartials
# before_filter :enforce_access_controls
before_filter :inject_workflow_steps, only: [:edit, :update], unless: proc{|c| request.format.json?}
before_filter :load_player_context, only: [:show, :show_progress]
def self.is_editor ctx
ctx.current_ability.is_editor_of?(ctx.instance_variable_get('@mediaobject').collection)
end
def self.is_lti_session ctx
ctx.user_session.present? && ctx.user_session[:lti_group].present?
end
is_editor_or_not_lti = proc { |ctx| self.is_editor(ctx) || !self.is_lti_session(ctx) }
is_editor_or_lti = proc { |ctx| (Avalon::Authentication::Providers.any? {|p| p[:provider] == :lti } &&self.is_editor(ctx)) || self.is_lti_session(ctx) }
add_conditional_partial :share, :share, partial: 'share_resource', if: is_editor_or_not_lti
add_conditional_partial :share, :embed, partial: 'embed_resource', if: is_editor_or_not_lti
add_conditional_partial :share, :lti_url, partial: 'lti_url', if: is_editor_or_lti
# Catch exceptions when you try to reference an object that doesn't exist.
# Attempt to resolve it to a close match if one exists and offer a link to
# the show page for that item. Otherwise ... nothing!
rescue_from ActiveFedora::ObjectNotFoundError do |exception|
render '/errors/unknown_pid', status: 404
end
def can_embed?
params[:action] == 'show'
end
def new
collection = Admin::Collection.find(params[:collection_id])
authorize! :read, collection
@mediaobject = MediaObjectsController.initialize_media_object(user_key)
@mediaobject.workflow.origin = 'web'
@mediaobject.collection = collection
@mediaobject.save(:validate => false)
redirect_to edit_media_object_path(@mediaobject)
end
# POST /media_objects
def create
@mediaobject = MediaObject.new
update_mediaobject
end
# PUT /media_objects/avalon:1.json
def json_update
begin
@mediaobject = MediaObject.find(params[:id])
update_mediaobject
rescue ActiveFedora::ObjectNotFoundError
render json: {errors: ["Mediaobject not found for #{params[:id]}"]}, status: 404
end
end
def update_mediaobject
begin
collection = Admin::Collection.find(params[:collection_id])
rescue ActiveFedora::ObjectNotFoundError
render json: {errors: ["Collection not found for #{params[:collection_id]}"]}, status: 422
return
end
@mediaobject.collection = collection
@mediaobject.avalon_uploader = 'REST API'
populate_from_catalog = !!params[:import_bib_record]
if populate_from_catalog and Avalon::BibRetriever.configured?
begin
@mediaobject.descMetadata.populate_from_catalog!(Array(params[:fields][:bibliographic_id]).first,
Array(params[:fields][:bibliographic_id_label]).first)
rescue Exception => e
render json: {errors: ['Bib import failed', e.message]}, status: 422
return
end
else
@mediaobject.update_datastream(:descMetadata, params[:fields]) if params.has_key?(:fields) and params[:fields].respond_to?(:has_key?)
end
error_messages = []
if !@mediaobject.save
error_messages += ['Failed to create media object:']+@mediaobject.errors.full_messages
elsif params[:files].respond_to?('each')
params[:files].each do |file_spec|
master_file = MasterFile.new(file_spec.except :structure)
master_file.mediaobject = @mediaobject
master_file.structuralMetadata.content = file_spec[:structure] if file_spec[:structure].present?
master_file.label = file_spec[:label] if file_spec[:label].present?
if master_file.save
@mediaobject.parts += [master_file]
else
error_messages += ["Problem saving MasterFile for #{file_spec[:file_location] rescue "<unknown>"}:"]+master_file.errors.full_messages
@mediaobject.destroy
break
end
end
@mediaobject.parts_with_order = @mediaobject.parts
if !@mediaobject.save
error_messages += ['Failed to create media object:']+@mediaobject.errors.full_messages
@mediaobject.destroy
end
end
if error_messages.empty?
render json: {id: @mediaobject.pid}, status: 200
else
render json: {errors: error_messages}, status: 422
@mediaobject.destroy
end
end
def custom_edit
authorize! :update, @mediaobject
if ['preview', 'structure', 'file-upload'].include? @active_step
@masterFiles = load_master_files
end
if 'preview' == @active_step
@currentStream = params[:content] ? set_active_file(params[:content]) : @masterFiles.first
@token = @currentStream.nil? ? "" : StreamToken.find_or_create_session_token(session, @currentStream.pid)
@currentStreamInfo = @currentStream.nil? ? {} : @currentStream.stream_details(@token, default_url_options[:host])
if (not @masterFiles.empty? and @currentStream.blank?)
@currentStream = @masterFiles.first
flash[:notice] = "That stream was not recognized. Defaulting to the first available stream for the resource"
end
end
if 'access-control' == @active_step
@groups = @mediaobject.local_read_groups
@users = @mediaobject.read_users
@virtual_groups = @mediaobject.virtual_read_groups
@ip_groups = @mediaobject.ip_read_groups
@visibility = @mediaobject.visibility
@addable_groups = Admin::Group.non_system_groups.reject { |g| @groups.include? g.name }
@addable_courses = Course.all.reject { |c| @virtual_groups.include? c.context_id }
end
end
def custom_update
authorize! :update, @mediaobject
flash[:notice] = @notice
end
def index
respond_to do |format|
format.json {
paginate json: MediaObject.all
}
end
end
def show
respond_to do |format|
format.html do
authorize! :read, @mediaobject
if (not @masterFiles.empty? and @currentStream.blank?) then
redirect_to media_object_path(@mediaobject.pid), flash: { notice: 'That stream was not recognized. Defaulting to the first available stream for the resource' }
else
render
end
end
format.js do
render json: @currentStreamInfo
end
format.json do
begin
render json: MediaObject.find(params[:id]).to_json
rescue ActiveFedora::ObjectNotFoundError
render json: {errors: ["Media Object not found for #{params[:id]}"]}, status: 404
end
end
end
end
def show_progress
authorize! :read, @mediaobject
overall = { :success => 0, :error => 0 }
result = Hash[
@masterFiles.collect { |mf|
mf_status = {
:status => mf.status_code,
:complete => mf.percent_complete.to_i,
:success => mf.percent_succeeded.to_i,
:error => mf.percent_failed.to_i,
:operation => mf.operation,
:message => mf.error.try(:sub,/^.+:/,'')
}
if mf.status_code == 'FAILED'
mf_status[:error] = 100-mf_status[:success]
overall[:error] += 100
else
overall[:success] += mf_status[:complete]
end
[mf.pid, mf_status]
}
]
overall.each { |k,v| overall[k] = [0,[100,v.to_f/@masterFiles.length.to_f].min].max.floor }
if overall[:success]+overall[:error] > 100
overall[:error] = 100-overall[:success]
end
result['overall'] = overall
respond_to do |format|
format.any(:xml, :json) { render request.format.to_sym => result }
end
end
def destroy
errors = []
success_count = 0
Array(params[:id]).each do |id|
media_object = MediaObject.find(id)
if can? :destroy, media_object
media_object.destroy
success_count += 1
else
errors += [ "#{media_object.title} (#{params[:id]}) permission denied" ]
end
end
message = "#{success_count} #{'media object'.pluralize(success_count)} successfully deleted."
message += "These objects were not deleted:</br> #{ errors.join('<br/> ') }" if errors.count > 0
redirect_to params[:previous_view]=='/bookmarks'? '/bookmarks' : root_path, flash: { notice: message }
end
# Sets the published status for the object. If no argument is given then
# it will just toggle the state.
def update_status
status = params[:status]
errors = []
success_count = 0
Array(params[:id]).each do |id|
media_object = MediaObject.find(id)
if cannot? :update, media_object
errors += ["#{media_object.title} (#{id}) (permission denied)."]
else
case status
when 'publish'
media_object.publish!(user_key)
# additional save to set permalink
media_object.save( validate: false )
success_count += 1
when 'unpublish'
if can? :unpublish, media_object
media_object.publish!(nil)
success_count += 1
else
errors += ["#{media_object.title} (#{id}) (permission denied)."]
end
end
end
end
message = "#{success_count} #{'media object'.pluralize(success_count)} successfully #{status}ed."
message += "These objects were not #{status}ed:</br> #{ errors.join('<br/> ') }" if errors.count > 0
redirect_to :back, flash: {notice: message.html_safe}
end
# Sets the published status for the object. If no argument is given then
# it will just toggle the state.
def tree
@mediaobject = MediaObject.find(params[:id])
authorize! :inspect, @mediaobject
respond_to do |format|
format.html {
render 'tree', :layout => !request.xhr?
}
format.json {
result = { @mediaobject.pid => {} }
@mediaobject.parts_with_order.each do |mf|
result[@mediaobject.pid][mf.pid] = mf.derivatives.collect(&:pid)
end
render :json => result
}
end
end
def self.initialize_media_object( user_key )
mediaobject = MediaObject.new( avalon_uploader: user_key )
mediaobject
end
def build_context
params.merge!({mediaobject: model_object, user: user_key, ability: current_ability})
end
def set_session_quality
session[:quality] = params[:quality] if params[:quality].present?
render nothing: true
end
protected
def load_master_files
@mediaobject.parts_with_order
end
def load_player_context
return if request.format.json? and !params.has_key? :content
@mediaobject = MediaObject.find(params[:id])
authorize! :read, @mediaobject
if params[:part]
index = params[:part].to_i-1
if index < 0 or index > @mediaobject.section_pid.length
raise ActiveFedora::ObjectNotFoundError
end
params[:content] = @mediaobject.section_pid[index]
end
@masterFiles = load_master_files
@currentStream = params[:content] ? set_active_file(params[:content]) : @masterFiles.first
@token = @currentStream.nil? ? "" : StreamToken.find_or_create_session_token(session, @currentStream.pid)
# This rescue statement seems a bit dodgy because it catches *all*
# exceptions. It might be worth refactoring when there are some extra
# cycles available.
@currentStreamInfo = @currentStream.nil? ? {} : @currentStream.stream_details(@token, default_url_options[:host])
@currentStreamInfo['t'] = params[:t] # add MediaFragment from params
end
# The goal of this method is to determine which stream to provide to the interface
#
# for immediate playback. Eventually this might be replaced by an AJAX call but for
# now to update the stream you must do a full page refresh.
#
# If the stream is not a member of that media object or does not exist at all then
# return a nil value that needs to be handled appropriately by the calling code
# block
def set_active_file(file_pid = nil)
unless (@mediaobject.parts.blank? or file_pid.blank?)
@mediaobject.parts.each do |part|
return part if part.pid == file_pid
end
end
# If you haven't dropped out by this point return an empty item
nil
end
end