Skip to content

Commit

Permalink
AO3-6297: Stop using MimeMagic in downloads code (#4740)
Browse files Browse the repository at this point in the history
* --no-edit

* Update work_download_steps.rb

---------

Co-authored-by: Melissa J <gm.jmnzc@gmail.com>
Co-authored-by: Brian Austin <brianjaustin@gmail.com>
  • Loading branch information
3 people authored Apr 6, 2024
1 parent bed484d commit d40d751
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 35 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 10 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -727,4 +723,4 @@ RUBY VERSION
ruby 3.1.4p223

BUNDLED WITH
2.3.26
2.2.33
11 changes: 5 additions & 6 deletions app/models/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
18 changes: 7 additions & 11 deletions config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions features/step_definitions/work_download_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d40d751

Please sign in to comment.