Class: CanvasInlinePdf::Plugin::OverrideFilePreview
- Inherits:
-
Object
- Object
- CanvasInlinePdf::Plugin::OverrideFilePreview
- Includes:
- AbstractController::Callbacks::ClassMethods::_BeforeActionCallback
- Defined in:
- lib/canvas_inline_pdf/plugin/override_file_preview.rb,
sig/canvas_inline_pdf.rbs
Overview
Callback Class, used to override FilePreviewController#show and use this plugin if applicable, if not - it returns controll to the controller.
This should only happen IF we have the setting turned on. And, this should be removed once Canvas upstream changes their controller to allow for additional plugins beyond croc and canvasdoc.
Instance Method Summary collapse
-
#allowed?(controller, file) ⇒ Boolean
private methods.
-
#before(controller) ⇒ Object
Runs before the FilePreviewsController#show method is called If the file can be rendered inline, it will call a redirect which because the controller this is modifying uses an iframe: will render the pdf within the iframe.
- #enabled? ⇒ Boolean
- #mark_seen(controller, file) ⇒ Object
Instance Method Details
#allowed?(controller, file) ⇒ Boolean
private methods.
31 32 33 34 35 36 37 38 |
# File 'sig/canvas_inline_pdf.rbs', line 31 def allowed?(controller, file) current_user = controller.instance_variable_get(:@current_user) params = controller.params session = controller.session controller.send(:read_allowed, file, current_user, params, session) && controller.send(:download_allowed, file, current_user, params, session) end |
#before(controller) ⇒ Object
Runs before the FilePreviewsController#show method is called If the file can be rendered inline, it will call a redirect which because the controller this is modifying uses an iframe: will render the pdf within the iframe.
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 |
# File 'lib/canvas_inline_pdf/plugin/override_file_preview.rb', line 36 def before(controller) # Do nothing unless this setting is true. return unless enabled? context = controller.instance_variable_get(:@context) file = context..not_deleted.find_by(id: controller.params[:file_id]) # Continue to the FilePreviewsController#show action. return unless file.present? return unless allowed?(controller, file) return unless CanvasInlinePdf.previewable?(nil, file) # Per Canvas: mark item seen for module progression purposes mark_seen(controller, file) verifier = controller.params[:verifier] fallback_url = controller.send( :context_url, context, :context_file_download_url, file.id, download_frd: 1, verifier: verifier ) file_url = controller.send( :safe_domain_file_url, file, fallback_url: fallback_url, verifier: verifier ) # Ideally we would update FilePreviewController in instructure/canvas to # call this if it can be rendered inline. # controller.send(:render, layout: false, template: template, locals: { file_url: file_url }) controller.send(:redirect_to, file_url) end |
#enabled? ⇒ Boolean
87 88 89 |
# File 'lib/canvas_inline_pdf/plugin/override_file_preview.rb', line 87 def enabled? CanvasInlinePdf.override_file_preview? end |
#mark_seen(controller, file) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/canvas_inline_pdf/plugin/override_file_preview.rb', line 91 def mark_seen(controller, file) current_user = controller.instance_variable_get(:@current_user) # :nocov: file.context_module_action(current_user, :read) if current_user # :nocov: controller.send(:log_asset_access, file, "files", "files") end |