-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Leveraging Marcel to better derive "mime_type"
Prior to this we were using a naive extension sniffer to determine a "mime_type" (which we used to decide on thumbnail dimensions). With this commit, we leverage Marcel (a Rails gem) that can detect mime based on the magic stings at the beginning of files or based on extensions and some lower "cost" processing (compared to `fits.sh`). Related to: - #30 - https://github.com/scientist-softserv/adventist-dl/issues/330
- Loading branch information
Showing
5 changed files
with
92 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
require 'marcel' | ||
|
||
module DerivativeRodeo | ||
module Services | ||
## | ||
# This module provides an interface for determining a mime-type. | ||
module MimeTypeService | ||
## | ||
# Hyrax has it's own compression of mime_types into conceptual types (as defined in | ||
# Hyrax::FileSetDerivativesService). This provides a somewhat conceptual overlap with that, | ||
# while also being more generalized. | ||
# | ||
# @param filename [String] | ||
# @return [Symbol] | ||
def self.hyrax_type(filename:) | ||
mime = mime_type(filename: filename) | ||
media_type, sub_type = mime.split("/") | ||
case media_type | ||
when "image", "audio", "text", "video" | ||
media_type.to_sym | ||
when "application" # The wild woolly weird world of all the things. | ||
# TODO: Do we need to worry about office documents? | ||
sub_type.to_sym | ||
else | ||
sub_type.to_sym | ||
end | ||
end | ||
|
||
## | ||
# Given a local :filename (e.g. downloaded and available on the server this is running), | ||
# return the mime_type of the file. | ||
# | ||
# @param filename [String] | ||
# @return [String] (e.g. "application/pdf", "text/plain") | ||
def self.mime_type(filename:) | ||
## | ||
# TODO: Does this attempt to read the whole file? That may create memory constraints. By | ||
# using Pathname (instead of File.read), we're letting Marcel do it's best mime magic. | ||
pathname = Pathname.new(filename) | ||
extension = filename.split(".")&.last&.downcase | ||
if extension | ||
# By including a possible extension, we can help nudge Marcel into making a more | ||
# Without extension, we will get a lot of "application/octet-stream" results. | ||
::Marcel::MimeType.for(pathname, extension: extension) | ||
else | ||
::Marcel::MimeType.for(pathname) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe DerivativeRodeo::Services::MimeTypeService do | ||
describe '.mime_type' do | ||
subject { described_class.mime_type(filename: filename) } | ||
{ | ||
__FILE__ => "text/x-ruby", | ||
Fixtures.path_for('4.1.07.tiff') => "image/tiff", | ||
Fixtures.path_for('tiff-no-ext') => "image/tiff", | ||
Fixtures.path_for('minimal-1-page.pdf') => "application/pdf", | ||
Fixtures.path_for('ndnp-sample1-txt.txt') => "text/plain" | ||
}.each do |given_filename, expected_mime_type| | ||
context "for #{File.basename(given_filename)}" do | ||
let(:filename) { given_filename } | ||
it { is_expected.to eq(expected_mime_type) } | ||
end | ||
end | ||
end | ||
|
||
describe '.hyrax_type' do | ||
subject { described_class.hyrax_type(filename: filename) } | ||
{ | ||
Fixtures.path_for('4.1.07.tiff') => :image, | ||
Fixtures.path_for('minimal-1-page.pdf') => :pdf, | ||
Fixtures.path_for('ndnp-sample1-txt.txt') => :text | ||
}.each do |given_filename, expected_hyrax_type| | ||
context "for #{File.basename(given_filename)}" do | ||
let(:filename) { given_filename } | ||
it { is_expected.to eq(expected_hyrax_type) } | ||
end | ||
end | ||
end | ||
end |
Binary file not shown.