diff --git a/Gemfile b/Gemfile index 36595d2b2cf..9b906b676d4 100644 --- a/Gemfile +++ b/Gemfile @@ -115,8 +115,7 @@ gem 'connection_pool' gem 'dalli' gem 'kgio', '2.10.0' -# TODO: AO3-6297 Update the download code so we can remove mimemagic. -gem "mimemagic", "0.3.10" +gem "marcel", "1.0.2" # Library for helping run pt-online-schema-change commands: gem "departure", "~> 6.5" diff --git a/Gemfile.lock b/Gemfile.lock index ca00b349823..7fcdc79b48e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -351,11 +351,8 @@ GEM mime-types (3.4.1) mime-types-data (~> 3.2015) mime-types-data (3.2022.0105) - mimemagic (0.3.10) - nokogiri (~> 1) - rake - mini_mime (1.1.5) - mini_portile2 (2.8.5) + mini_mime (1.1.2) + mini_portile2 (2.8.2) minitest (5.17.0) mono_logger (1.1.2) multi_json (1.15.0) @@ -380,22 +377,21 @@ GEM net-ssh (>= 2.6.5, < 7.0.0) net-sftp (3.0.0) net-ssh (>= 5.0.0, < 7.0.0) - net-smtp (0.4.0) + net-smtp (0.5.0) net-protocol net-ssh (6.1.0) net-ssh-gateway (2.0.0) net-ssh (>= 4.0.0) netrc (0.11.0) - newrelic_rpm (9.7.1) + newrelic_rpm (8.16.0) nio4r (2.5.8) - nokogiri (1.16.2) - mini_portile2 (~> 2.8.2) + nokogiri (1.14.3) + mini_portile2 (~> 2.8.0) racc (~> 1.4) orm_adapter (0.5.0) parallel (1.23.0) - parser (3.3.0.5) + parser (3.1.0.0) ast (~> 2.4.1) - racc permit_yo (2.1.3) phraseapp-in-context-editor-ruby (1.4.0) i18n (>= 0.6) @@ -417,7 +413,7 @@ GEM pundit (2.1.1) activesupport (>= 3.0.0) raabro (1.4.0) - racc (1.7.3) + racc (1.7.1) rack (2.2.6.4) rack-attack (6.6.0) rack (>= 1.0, < 3) @@ -673,8 +669,8 @@ DEPENDENCIES listen (~> 3.3) lograge mail (>= 2.8) + marcel (= 1.0.2) mechanize - mimemagic (= 0.3.10) minitest mysql2 n_plus_one_control @@ -727,4 +723,4 @@ RUBY VERSION ruby 3.1.4p223 BUNDLED WITH - 2.3.26 + 2.2.33 diff --git a/app/models/download.rb b/app/models/download.rb index d7d2c799f4c..e05af505292 100644 --- a/app/models/download.rb +++ b/app/models/download.rb @@ -4,9 +4,7 @@ class Download def initialize(work, options = {}) @work = work @file_type = set_file_type(options.slice(:mime_type, :format)) - # TODO: Our current version of the mime-types gem doesn't include azw3, but - # the gem cannot be updated without updating rest-client - @mime_type = @file_type == "azw3" ? "application/x-mobi8-ebook" : MIME::Types.type_for(@file_type).first + @mime_type = Marcel::MimeType.for(extension: @file_type).to_s @include_draft_chapters = options[:include_draft_chapters] end @@ -40,16 +38,17 @@ def set_file_type(options) # Given a mime type, return a file extension def file_type_from_mime(mime) - ext = MimeMagic.new(mime.to_s).subtype - case ext + subtype = Marcel::Magic.new(mime.to_s).subtype + case subtype when "x-mobipocket-ebook" "mobi" when "x-mobi8-ebook" "azw3" else - ext + subtype end end + # The base name of the file (e.g., "War_and_Peace") def file_name diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb index bcd3e9d6106..4fac7566ca9 100644 --- a/config/initializers/mime_types.rb +++ b/config/initializers/mime_types.rb @@ -1,13 +1,9 @@ -# Be sure to restart your server when you modify this file. - -# Add new mime types for use in respond_to blocks: -# Mime::Type.register "text/richtext", :rtf - # for azw3 files -Mime::Type.register 'application/x-mobi8-ebook', :azw3 - -# for mobi files -Mime::Type.register 'application/x-mobipocket-ebook', :mobi - +Marcel::MimeType.extend "application/x-mobi8-ebook", extensions: %w[azw3] +Mime::Type.register "application/x-mobi8-ebook", :azw3 # for epub files -Mime::Type.register 'application/epub', :epub +Marcel::MimeType.extend "application/epub", extensions: %w[epub] +Mime::Type.register "application/epub", :epub + +# for mobi type (already present in marcel types) +Mime::Type.register "application/x-mobipocket-ebook", :mobi diff --git a/features/step_definitions/work_download_steps.rb b/features/step_definitions/work_download_steps.rb index 32e61e7cdb6..bf32c3e6981 100644 --- a/features/step_definitions/work_download_steps.rb +++ b/features/step_definitions/work_download_steps.rb @@ -11,7 +11,7 @@ end Then /^I should receive a file of type "(.*?)"$/ do |filetype| - mime_type = filetype == "azw3" ? "application/x-mobi8-ebook" : MIME::Types.type_for("foo.#{filetype}").first + mime_type = Marcel::MimeType.for(name: "foo.#{filetype}").to_s expect(page.response_headers['Content-Disposition']).to match(/filename=.+\.#{filetype}/) expect(page.response_headers['Content-Length'].to_i).to be_positive expect(page.response_headers['Content-Type']).to eq(mime_type) @@ -30,7 +30,7 @@ download = Download.new(work, format: filetype) filename = "#{download.file_name}.#{download.file_type}" - mime_type = filetype == "azw3" ? "application/x-mobi8-ebook" : MIME::Types.type_for(filename).first + mime_type = Marcel::MimeType.for(name: filename).to_s expect(page.response_headers['Content-Disposition']).to match(/filename="#{filename}"/) expect(page.response_headers['Content-Length'].to_i).to be_positive expect(page.response_headers['Content-Type']).to eq(mime_type)