canvas_inline_pdf 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d64a14c65fd164b204839a5046ded7e82655a5604d2ffd6c39a40db3a1dce6d5
4
+ data.tar.gz: af62de215edf6cb477b5441ef749836ddd7b977430f2b96894821771ef3b478f
5
+ SHA512:
6
+ metadata.gz: 9a94efbe7e8cefba7c4ccccf869f24e21251460ac6f90e05313cd7ab484f021a7acce8da529c4ad0e460017c1a173fcef5c20726e92aec2c7cfc0917084fe1b1
7
+ data.tar.gz: 61267ecec7180e71965f0c0a3d0dfc00cd0e0d9f657eb34d467e26f2738cd84dcd5cfb94d652e33abd63387d501704103b9c79824a592d6d52ad34e8153331cd
@@ -0,0 +1,20 @@
1
+ <%
2
+ =begin %>
3
+
4
+ <%= fields_for :settings do |f| %>
5
+ <table style="width: 500px;" class="formtable">
6
+ <tr>
7
+ <td colspan="2">
8
+ <%= mt :description, "Enabling Override File Preview will replace file previews (for pdfs) with this plugin." %>
9
+ </td>
10
+ </tr>
11
+ <tr>
12
+ <td><%= f.blabel :override_file_preview, :en => "Override File Preview" %></td>
13
+ <td>
14
+ <%= f.check_box :override_file_preview, { checked: Canvas::Plugin.value_to_boolean(settings[:override_file_preview]) }, 'true', 'false' %>
15
+ </td>
16
+ </tr>
17
+ </table>
18
+ <% end %>
19
+ <%
20
+ =end %>
@@ -0,0 +1,9 @@
1
+ <% provide :head, include_common_stylesheets %>
2
+ <%= render :partial => "layouts/head" %>
3
+ <body style="margin:0; padding: 0; background: transparent;">
4
+ <iframe
5
+ id="file_content"
6
+ src="<%= file_url %>"
7
+ style="width: 100%; height: 400px;" title="<%= t('File Content') %>">
8
+ </iframe>
9
+ </body>
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanvasInlinePdf
4
+ module Plugin
5
+ # Modifies the +inline_content?+ method of <tt>Attachment</tt>
6
+ # so that Canvas will render it inline as well.
7
+ # This is relevant for when students look at pdfs within the Modules section.
8
+ module AttachmentExtension
9
+ def inline_content?
10
+ CanvasInlinePdf.previewable?(nil, self) || super # steep:ignore
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (C) 2025 - present Ameelio
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ # this software and associated documentation files (the “Software”), to deal in the
7
+ # Software without restriction, including without limitation the rights to use,
8
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
9
+ # Software, and to permit persons to whom the Software is furnished to do so,
10
+ # subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+
15
+ # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ module CanvasInlinePdf
23
+ module Plugin
24
+ # Callback Class, used to override FilePreviewController#show
25
+ # and use this plugin if applicable, if not - it returns controll
26
+ # to the controller.
27
+ #
28
+ # This should only happen IF we have the setting turned on.
29
+ # And, this should be removed once Canvas upstream changes their controller
30
+ # to allow for additional plugins beyond croc and canvasdoc.
31
+ class OverrideFilePreview
32
+ # Runs before the FilePreviewsController#show method is called
33
+ # If the file can be rendered inline, it will call a redirect
34
+ # which because the controller this is modifying uses an iframe:
35
+ # will render the pdf within the iframe.
36
+ def before(controller)
37
+ # Do nothing unless this setting is true.
38
+ return unless enabled?
39
+
40
+ context = controller.instance_variable_get(:@context)
41
+
42
+ file = context.attachments.not_deleted.find_by(id: controller.params[:file_id])
43
+
44
+ # Continue to the FilePreviewsController#show action.
45
+ return unless file.present?
46
+ return unless allowed?(controller, file)
47
+ return unless CanvasInlinePdf.previewable?(nil, file)
48
+
49
+ # Per Canvas: mark item seen for module progression purposes
50
+ mark_seen(controller, file)
51
+
52
+ verifier = controller.params[:verifier]
53
+
54
+ fallback_url = controller.send(
55
+ :context_url,
56
+ context,
57
+ :context_file_download_url,
58
+ file.id,
59
+ download_frd: 1,
60
+ verifier: verifier
61
+ )
62
+
63
+ file_url = controller.send(
64
+ :safe_domain_file_url,
65
+ file,
66
+ fallback_url: fallback_url,
67
+ verifier: verifier
68
+ )
69
+
70
+ # Ideally we would update FilePreviewController in instructure/canvas to
71
+ # call this if it can be rendered inline.
72
+ # controller.send(:render, layout: false, template: template, locals: { file_url: file_url })
73
+ controller.send(:redirect_to, file_url)
74
+ end
75
+
76
+ private
77
+
78
+ def allowed?(controller, file)
79
+ current_user = controller.instance_variable_get(:@current_user)
80
+ params = controller.params
81
+ session = controller.session
82
+
83
+ controller.send(:read_allowed, file, current_user, params, session) &&
84
+ controller.send(:download_allowed, file, current_user, params, session)
85
+ end
86
+
87
+ def enabled?
88
+ CanvasInlinePdf.override_file_preview?
89
+ end
90
+
91
+ def mark_seen(controller, file)
92
+ current_user = controller.instance_variable_get(:@current_user)
93
+
94
+ # :nocov:
95
+ file.context_module_action(current_user, :read) if current_user
96
+ # :nocov:
97
+
98
+ controller.send(:log_asset_access, file, "files", "files")
99
+ end
100
+
101
+ def template
102
+ File.join('canvas_inline_pdf', 'preview')
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2025 - present Ameelio
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ # this software and associated documentation files (the “Software”), to deal in the
6
+ # Software without restriction, including without limitation the rights to use,
7
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8
+ # Software, and to permit persons to whom the Software is furnished to do so,
9
+ # subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+
14
+ # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+
21
+ require "canvas_inline_pdf/plugin/attachment_extension"
22
+ # require "canvas_inline_pdf/plugin/override_file_preview"
23
+
24
+ module CanvasInlinePdf
25
+ # Canvas LMS Integration and registration.
26
+ module Plugin
27
+ def enabled?
28
+ plugin = Plugin.find
29
+
30
+ plugin.enabled? == true
31
+ end
32
+
33
+ # def override_file_preview?
34
+ # plugin = Plugin.find
35
+ #
36
+ # plugin&.enabled? && plugin.settings[:override_file_preview] == "true"
37
+ # end
38
+
39
+ def register_plugin
40
+ Plugin.register
41
+ end
42
+
43
+ @cfg = {
44
+ author: "Ameelio",
45
+ description: "Allows users to view PDFs inline within File Preview.",
46
+ name: "Inline PDF",
47
+ hide_from_users: false,
48
+ settings_partial: "canvas_inline_pdf/plugin_settings",
49
+ settings: {
50
+ # override_file_preview: false
51
+ },
52
+ version: CanvasInlinePdf::VERSION
53
+ }
54
+
55
+ @name = :canvas_inline_pdf
56
+
57
+ # ########################################
58
+ # As singleton methods, these never get
59
+ # added when include or extend are called.
60
+ # ########################################
61
+ def self.find
62
+ Canvas::Plugin.find(@name)
63
+ end
64
+
65
+ def self.register
66
+ Canvas::Plugin.register(@name, :previews, @cfg)
67
+
68
+ ::Attachment.prepend(AttachmentExtension)
69
+
70
+ # ::FilePreviewsController.before_action(
71
+ # OverrideFilePreview.new,
72
+ # on: :show
73
+ # )
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (C) 2025 - present Ameelio
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ # this software and associated documentation files (the “Software”), to deal in the
7
+ # Software without restriction, including without limitation the rights to use,
8
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
9
+ # Software, and to permit persons to whom the Software is furnished to do so,
10
+ # subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+
15
+ # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ module CanvasInlinePdf
23
+ # @see https://github.com/instructure/canvas-lms/blob/master/app/controllers/file_previews_controller.rb
24
+ # @note Called by FilePreviewsController#show in Canvas LMS
25
+ module Preview
26
+ # Returns true if attachment is a PDF or TXT file.
27
+ # @see https://github.com/instructure/canvas-lms/blob/master/app/models/attachment.rb
28
+ def previewable?(_account, attachment)
29
+ CanvasInlinePdf.enabled? && attachment&.content_type.to_s == "application/pdf"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CanvasInlinePdf
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (C) 2025 - present Ameelio
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ # this software and associated documentation files (the “Software”), to deal in the
7
+ # Software without restriction, including without limitation the rights to use,
8
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
9
+ # Software, and to permit persons to whom the Software is furnished to do so,
10
+ # subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+
15
+ # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ require "canvas_inline_pdf/version"
22
+
23
+ require "canvas_inline_pdf/plugin"
24
+ require "canvas_inline_pdf/preview"
25
+
26
+ # Canvas Plugin to render PDFS inline.
27
+ module CanvasInlinePdf
28
+ extend Preview
29
+ extend Plugin
30
+
31
+ # :nocov:
32
+ if defined?(Rails)
33
+ # This registers the plugin and adds a callback to FilePreviewsController
34
+ # It also adds the plugin_settings partial to the view path.
35
+ # @see https://edgeapi.rubyonrails.org/classes/Rails/Engine.html
36
+ class Engine < ::Rails::Engine
37
+
38
+ config.to_prepare do
39
+ CanvasInlinePdf.register_plugin
40
+ end
41
+ end
42
+ end
43
+ # :nocov:
44
+
45
+ private_constant :Plugin
46
+ private_constant :Preview
47
+ end
@@ -0,0 +1,46 @@
1
+ module CanvasInlinePdf
2
+ VERSION: String
3
+
4
+ extend Preview
5
+ extend Plugin
6
+
7
+ module Plugin
8
+ interface _CanvasPlugin
9
+ def enabled?: () -> bool
10
+ def settings: () -> Hash[Symbol, untyped]
11
+ end
12
+
13
+ self.@cfg : Hash[Symbol, untyped]
14
+ self.@name : Symbol
15
+
16
+ def self.find: () -> _CanvasPlugin
17
+ def self.register: () -> void
18
+
19
+ def enabled?: () -> bool
20
+ def register_plugin: () -> void
21
+ def override_file_preview?: () -> bool
22
+
23
+ module AttachmentExtension
24
+ def inline_content?: () -> bool
25
+ end
26
+
27
+ class OverrideFilePreview
28
+ include ::AbstractController::Callbacks::ClassMethods::_BeforeActionCallback
29
+
30
+ # private methods.
31
+ def allowed?: (ActionController::Base, untyped) -> bool
32
+
33
+ def enabled?: () -> bool
34
+
35
+ def mark_seen: (ActionController::Base, untyped) -> untyped
36
+ end
37
+ end
38
+
39
+ module Preview
40
+ interface _HasContent
41
+ def content_type: () -> String
42
+ end
43
+
44
+ def previewable?: (nil, _HasContent) -> bool
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canvas_inline_pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ameelio
8
+ - Jason Kenney
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2025-06-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Canvas LMS Plugin to allow inline file preview for PDFS.
15
+ email:
16
+ - jasonk@ameelio.org
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - app/views/canvas_inline_pdf/_plugin_settings.html.erb
22
+ - app/views/canvas_inline_pdf/preview.html.erb
23
+ - lib/canvas_inline_pdf.rb
24
+ - lib/canvas_inline_pdf/plugin.rb
25
+ - lib/canvas_inline_pdf/plugin/attachment_extension.rb
26
+ - lib/canvas_inline_pdf/plugin/override_file_preview.rb
27
+ - lib/canvas_inline_pdf/preview.rb
28
+ - lib/canvas_inline_pdf/version.rb
29
+ - sig/canvas_inline_pdf.rbs
30
+ homepage: https://github.com/Ameelio/canvas_inline_pdf
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://github.com/Ameelio/canvas_inline_pdf
35
+ source_code_uri: https://github.com/Ameelio/canvas_inline_pdf
36
+ changelog_uri: https://github.com/Ameelio/canvas_inline_pdf/CHANGELOG.md
37
+ github_repo: ssh://github.com/Ameelio/canvas_inline_pdf
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.3.27
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Inline PDF Plugin for Canvas LMS
57
+ test_files: []