activestorage-office-previewer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +151 -0
  5. data/LICENSE.md +20 -0
  6. data/README.md +24 -0
  7. data/Rakefile +10 -0
  8. data/activestorage-office-previewer.gemspec +25 -0
  9. data/lib/active_storage/previewer/office_previewer.rb +57 -0
  10. data/lib/activestorage-office-previewer.rb +7 -0
  11. data/test/database/setup.rb +6 -0
  12. data/test/dummy/.browserslistrc +1 -0
  13. data/test/dummy/.gitignore +36 -0
  14. data/test/dummy/.ruby-version +1 -0
  15. data/test/dummy/README.md +24 -0
  16. data/test/dummy/Rakefile +6 -0
  17. data/test/dummy/app/assets/config/manifest.js +2 -0
  18. data/test/dummy/app/assets/images/.keep +0 -0
  19. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  20. data/test/dummy/app/channels/application_cable/channel.rb +4 -0
  21. data/test/dummy/app/channels/application_cable/connection.rb +4 -0
  22. data/test/dummy/app/controllers/application_controller.rb +2 -0
  23. data/test/dummy/app/controllers/concerns/.keep +0 -0
  24. data/test/dummy/app/helpers/application_helper.rb +2 -0
  25. data/test/dummy/app/javascript/channels/consumer.js +6 -0
  26. data/test/dummy/app/javascript/channels/index.js +5 -0
  27. data/test/dummy/app/javascript/packs/application.js +17 -0
  28. data/test/dummy/app/jobs/application_job.rb +7 -0
  29. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  30. data/test/dummy/app/models/application_record.rb +3 -0
  31. data/test/dummy/app/models/concerns/.keep +0 -0
  32. data/test/dummy/app/views/layouts/application.html.erb +15 -0
  33. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  34. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  35. data/test/dummy/babel.config.js +72 -0
  36. data/test/dummy/config.ru +5 -0
  37. data/test/dummy/config/application.rb +13 -0
  38. data/test/dummy/config/cable.yml +10 -0
  39. data/test/dummy/config/credentials.yml.enc +1 -0
  40. data/test/dummy/config/database.yml +25 -0
  41. data/test/dummy/config/environment.rb +5 -0
  42. data/test/dummy/config/environments/development.rb +62 -0
  43. data/test/dummy/config/environments/production.rb +112 -0
  44. data/test/dummy/config/environments/test.rb +48 -0
  45. data/test/dummy/config/initializers/application_controller_renderer.rb +8 -0
  46. data/test/dummy/config/initializers/assets.rb +14 -0
  47. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  48. data/test/dummy/config/initializers/content_security_policy.rb +30 -0
  49. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  50. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  51. data/test/dummy/config/initializers/inflections.rb +16 -0
  52. data/test/dummy/config/initializers/mime_types.rb +4 -0
  53. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  54. data/test/dummy/config/locales/en.yml +33 -0
  55. data/test/dummy/config/puma.rb +38 -0
  56. data/test/dummy/config/routes.rb +3 -0
  57. data/test/dummy/config/spring.rb +6 -0
  58. data/test/dummy/config/storage.yml +34 -0
  59. data/test/dummy/config/webpack/development.js +5 -0
  60. data/test/dummy/config/webpack/environment.js +3 -0
  61. data/test/dummy/config/webpack/production.js +5 -0
  62. data/test/dummy/config/webpack/test.js +5 -0
  63. data/test/dummy/config/webpacker.yml +96 -0
  64. data/test/dummy/db/migrate/20191204133417_create_active_storage_tables.active_storage.rb +27 -0
  65. data/test/dummy/db/seeds.rb +7 -0
  66. data/test/dummy/lib/assets/.keep +0 -0
  67. data/test/dummy/lib/tasks/.keep +0 -0
  68. data/test/dummy/log/.keep +0 -0
  69. data/test/dummy/postcss.config.js +12 -0
  70. data/test/dummy/public/404.html +67 -0
  71. data/test/dummy/public/422.html +67 -0
  72. data/test/dummy/public/500.html +66 -0
  73. data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
  74. data/test/dummy/public/apple-touch-icon.png +0 -0
  75. data/test/dummy/public/favicon.ico +0 -0
  76. data/test/dummy/public/robots.txt +1 -0
  77. data/test/dummy/storage/.keep +0 -0
  78. data/test/dummy/test/application_system_test_case.rb +5 -0
  79. data/test/dummy/test/channels/application_cable/connection_test.rb +11 -0
  80. data/test/dummy/test/controllers/.keep +0 -0
  81. data/test/dummy/test/fixtures/.keep +0 -0
  82. data/test/dummy/test/fixtures/files/.keep +0 -0
  83. data/test/dummy/test/helpers/.keep +0 -0
  84. data/test/dummy/test/integration/.keep +0 -0
  85. data/test/dummy/test/mailers/.keep +0 -0
  86. data/test/dummy/test/models/.keep +0 -0
  87. data/test/dummy/test/system/.keep +0 -0
  88. data/test/dummy/test/test_helper.rb +13 -0
  89. data/test/dummy/tmp/.keep +0 -0
  90. data/test/dummy/vendor/.keep +0 -0
  91. data/test/fixtures/files/hello.docx +0 -0
  92. data/test/fixtures/files/hello.pptx +0 -0
  93. data/test/fixtures/files/hello.xlsx +0 -0
  94. data/test/office_previewer_test.rb +54 -0
  95. data/test/test_helper.rb +16 -0
  96. metadata +333 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a33977bb24c7a89382dbab79fc7c2711b8c2a78e9fad5ee6e772219f261f5f67
4
+ data.tar.gz: '0593c3cc8b6d01d48994b8d02d7447f96b485b41d15e8d80b33eed01bd6b1c8e'
5
+ SHA512:
6
+ metadata.gz: a8a4248278c81b4631bf453d0ecd74b153e12ecc58df1d1a250451c0d9eab31700eb66e02b7cd033bab538062e0465fc77f16687293c78e1e5371d3afe677e27
7
+ data.tar.gz: 307b7c2b958bc914a0d3f9a28b789a6a2301111832d1522439c7170e748a4081cf1f5d3aa1720b66f228f44c9f940dffc101042ea1748f26d674def3cd7a65df
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,151 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activestorage-office-previewer (0.1.0)
5
+ activestorage (>= 6.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (6.0.1)
11
+ actionpack (= 6.0.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailbox (6.0.1)
15
+ actionpack (= 6.0.1)
16
+ activejob (= 6.0.1)
17
+ activerecord (= 6.0.1)
18
+ activestorage (= 6.0.1)
19
+ activesupport (= 6.0.1)
20
+ mail (>= 2.7.1)
21
+ actionmailer (6.0.1)
22
+ actionpack (= 6.0.1)
23
+ actionview (= 6.0.1)
24
+ activejob (= 6.0.1)
25
+ mail (~> 2.5, >= 2.5.4)
26
+ rails-dom-testing (~> 2.0)
27
+ actionpack (6.0.1)
28
+ actionview (= 6.0.1)
29
+ activesupport (= 6.0.1)
30
+ rack (~> 2.0)
31
+ rack-test (>= 0.6.3)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
34
+ actiontext (6.0.1)
35
+ actionpack (= 6.0.1)
36
+ activerecord (= 6.0.1)
37
+ activestorage (= 6.0.1)
38
+ activesupport (= 6.0.1)
39
+ nokogiri (>= 1.8.5)
40
+ actionview (6.0.1)
41
+ activesupport (= 6.0.1)
42
+ builder (~> 3.1)
43
+ erubi (~> 1.4)
44
+ rails-dom-testing (~> 2.0)
45
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
46
+ activejob (6.0.1)
47
+ activesupport (= 6.0.1)
48
+ globalid (>= 0.3.6)
49
+ activemodel (6.0.1)
50
+ activesupport (= 6.0.1)
51
+ activerecord (6.0.1)
52
+ activemodel (= 6.0.1)
53
+ activesupport (= 6.0.1)
54
+ activestorage (6.0.1)
55
+ actionpack (= 6.0.1)
56
+ activejob (= 6.0.1)
57
+ activerecord (= 6.0.1)
58
+ marcel (~> 0.3.1)
59
+ activesupport (6.0.1)
60
+ concurrent-ruby (~> 1.0, >= 1.0.2)
61
+ i18n (>= 0.7, < 2)
62
+ minitest (~> 5.1)
63
+ tzinfo (~> 1.1)
64
+ zeitwerk (~> 2.2)
65
+ builder (3.2.3)
66
+ byebug (11.0.1)
67
+ concurrent-ruby (1.1.5)
68
+ crass (1.0.5)
69
+ erubi (1.9.0)
70
+ globalid (0.4.2)
71
+ activesupport (>= 4.2.0)
72
+ i18n (1.7.0)
73
+ concurrent-ruby (~> 1.0)
74
+ loofah (2.4.0)
75
+ crass (~> 1.0.2)
76
+ nokogiri (>= 1.5.9)
77
+ mail (2.7.1)
78
+ mini_mime (>= 0.1.1)
79
+ marcel (0.3.3)
80
+ mimemagic (~> 0.3.2)
81
+ method_source (0.9.2)
82
+ mimemagic (0.3.3)
83
+ mini_magick (4.9.5)
84
+ mini_mime (1.0.2)
85
+ mini_portile2 (2.4.0)
86
+ minitest (5.13.0)
87
+ nio4r (2.5.2)
88
+ nokogiri (1.10.6)
89
+ mini_portile2 (~> 2.4.0)
90
+ rack (2.0.7)
91
+ rack-test (1.1.0)
92
+ rack (>= 1.0, < 3)
93
+ rails (6.0.1)
94
+ actioncable (= 6.0.1)
95
+ actionmailbox (= 6.0.1)
96
+ actionmailer (= 6.0.1)
97
+ actionpack (= 6.0.1)
98
+ actiontext (= 6.0.1)
99
+ actionview (= 6.0.1)
100
+ activejob (= 6.0.1)
101
+ activemodel (= 6.0.1)
102
+ activerecord (= 6.0.1)
103
+ activestorage (= 6.0.1)
104
+ activesupport (= 6.0.1)
105
+ bundler (>= 1.3.0)
106
+ railties (= 6.0.1)
107
+ sprockets-rails (>= 2.0.0)
108
+ rails-dom-testing (2.0.3)
109
+ activesupport (>= 4.2.0)
110
+ nokogiri (>= 1.6)
111
+ rails-html-sanitizer (1.3.0)
112
+ loofah (~> 2.3)
113
+ railties (6.0.1)
114
+ actionpack (= 6.0.1)
115
+ activesupport (= 6.0.1)
116
+ method_source
117
+ rake (>= 0.8.7)
118
+ thor (>= 0.20.3, < 2.0)
119
+ rake (13.0.1)
120
+ sprockets (4.0.0)
121
+ concurrent-ruby (~> 1.0)
122
+ rack (> 1, < 3)
123
+ sprockets-rails (3.2.1)
124
+ actionpack (>= 4.0)
125
+ activesupport (>= 4.0)
126
+ sprockets (>= 3.0.0)
127
+ sqlite3 (1.4.1)
128
+ thor (0.20.3)
129
+ thread_safe (0.3.6)
130
+ tzinfo (1.2.5)
131
+ thread_safe (~> 0.1)
132
+ websocket-driver (0.7.1)
133
+ websocket-extensions (>= 0.1.0)
134
+ websocket-extensions (0.1.4)
135
+ zeitwerk (2.2.2)
136
+
137
+ PLATFORMS
138
+ ruby
139
+
140
+ DEPENDENCIES
141
+ activestorage-office-previewer!
142
+ bundler (~> 1.17)
143
+ byebug
144
+ mini_magick (~> 4.9.5)
145
+ minitest (~> 5.13.0)
146
+ rails (>= 6.0.0)
147
+ rake (~> 13.0.0)
148
+ sqlite3 (~> 1.4.1)
149
+
150
+ BUNDLED WITH
151
+ 1.17.2
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2019 Basecamp, LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,24 @@
1
+ # Active Storage previewer for Microsoft Office files
2
+
3
+ Use [LibreOffice](https://www.libreoffice.org/) to generate previews for Microsoft Word documents, Microsoft PowerPoint presentations, and Microsoft Excel spreadsheets in [Active Storage](https://guides.rubyonrails.org/active_storage_overview.html).
4
+
5
+ ## Requirements
6
+
7
+ * Rails 6.0 or newer
8
+ * [LibreOffice](https://www.libreoffice.org/) 6.2 or newer
9
+
10
+ ## Usage
11
+
12
+ Add the `activestorage-office-previewer` gem to your app’s Gemfile and run `bundle install`:
13
+
14
+ ```ruby
15
+ gem "activestorage-office-previewer"
16
+ ```
17
+
18
+ Generate previews of Microsoft Office files using Active Storage’s [preview feature](https://guides.rubyonrails.org/active_storage_overview.html#previewing-files).
19
+
20
+ ## License
21
+
22
+ This gem is released under the terms of the MIT license.
23
+
24
+ LibreOffice is released under the terms of the [Mozilla Public License, version 2.0](https://www.libreoffice.org/about-us/licenses/).
@@ -0,0 +1,10 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ t.libs << "test"
5
+ t.libs << "lib"
6
+ t.test_files = FileList["test/**/*_test.rb"]
7
+ t.warning = false
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "activestorage-office-previewer"
3
+ s.version = "0.1.0"
4
+ s.authors = "George Claghorn"
5
+ s.email = "george@basecamp.com"
6
+ s.summary = "Active Storage previewer for Microsoft Office files based on LibreOffice"
7
+ s.homepage = "https://github.com/basecamp/activestorage-office-previewer"
8
+ s.license = "MIT"
9
+
10
+ s.required_ruby_version = ">= 2.6.0"
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- test/*`.split("\n")
14
+
15
+ s.add_dependency "activestorage", ">= 6.0.0"
16
+
17
+ s.add_development_dependency "bundler", "~> 1.17"
18
+ s.add_development_dependency "rake", "~> 13.0.0"
19
+ s.add_development_dependency "minitest", "~> 5.13.0"
20
+ s.add_development_dependency "mini_magick", "~> 4.9.5"
21
+ s.add_development_dependency "sqlite3", "~> 1.4.1"
22
+ s.add_development_dependency "byebug"
23
+
24
+ s.add_development_dependency "rails", ">= 6.0.0"
25
+ end
@@ -0,0 +1,57 @@
1
+ require "active_storage/previewer"
2
+
3
+ class ActiveStorage::Previewer::OfficePreviewer < ActiveStorage::Previewer
4
+ ACCEPTABLE_CONTENT_TYPES = [
5
+ "application/msword", # .doc
6
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document", # .docx
7
+
8
+ "application/vnd.ms-powerpoint", # .ppt
9
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation", # .pptx
10
+
11
+ "application/vnd.ms-excel", # .xls
12
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" # .xlsx
13
+ ]
14
+
15
+ class << self
16
+ def accept?(blob)
17
+ blob.content_type.in?(ACCEPTABLE_CONTENT_TYPES) && soffice_exists?
18
+ end
19
+
20
+ def soffice_exists?
21
+ if @soffice_exists.nil?
22
+ @soffice_exists = system(soffice_path, "--version", out: File::NULL, err: File::NULL)
23
+ else
24
+ @soffice_exists
25
+ end
26
+ end
27
+
28
+ def soffice_path
29
+ ActiveStorage.paths[:soffice] || "soffice"
30
+ end
31
+ end
32
+
33
+ def preview
34
+ download_blob_to_tempfile do |input|
35
+ draw_poster_image_from input do |output|
36
+ yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png"
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+ def draw_poster_image_from(file, &block)
43
+ system soffice_path, "--headless", "--invisible", "--convert-to", "png",
44
+ "--outdir", tmpdir, file.path, out: File::NULL, err: File::NULL
45
+ consume "#{tmpdir}/#{File.basename(file.path, ".*")}.png", &block
46
+ end
47
+
48
+ def consume(path, &block)
49
+ File.open(path, "rb", &block)
50
+ ensure
51
+ File.delete(path)
52
+ end
53
+
54
+ def soffice_path
55
+ self.class.soffice_path
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ require "active_storage"
2
+ require "active_storage/previewer/office_previewer"
3
+
4
+ if defined?(Rails)
5
+ require "active_storage/engine"
6
+ Rails.application.config.active_storage.previewers += [ ActiveStorage::Previewer::OfficePreviewer ]
7
+ end
@@ -0,0 +1,6 @@
1
+ require "active_record"
2
+
3
+ ActiveRecord::Base.establish_connection \
4
+ adapter: "sqlite3", database: ":memory:", migrations_paths: File.expand_path("../dummy/db/migrate", __dir__)
5
+
6
+ ActiveRecord::Base.connection.migration_context.migrate
@@ -0,0 +1 @@
1
+ defaults
@@ -0,0 +1,36 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+ /db/*.sqlite3-*
14
+
15
+ # Ignore all logfiles and tempfiles.
16
+ /log/*
17
+ /tmp/*
18
+ !/log/.keep
19
+ !/tmp/.keep
20
+
21
+ # Ignore uploaded files in development.
22
+ /storage/*
23
+ !/storage/.keep
24
+
25
+ /public/assets
26
+ .byebug_history
27
+
28
+ # Ignore master key for decrypting credentials and more.
29
+ /config/master.key
30
+
31
+ /public/packs
32
+ /public/packs-test
33
+ /node_modules
34
+ /yarn-error.log
35
+ yarn-debug.log*
36
+ .yarn-integrity
@@ -0,0 +1 @@
1
+ 2.6.2
@@ -0,0 +1,24 @@
1
+ # README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,2 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../stylesheets .css
File without changes
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
6
+ * vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Channel < ActionCable::Channel::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Connection < ActionCable::Connection::Base
3
+ end
4
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end