Skip to content

Commit

Permalink
Add correct mime type to a/v derivatives
Browse files Browse the repository at this point in the history
By saving mime type passed to ValkyriePersistDerivatives
advances gh-6294

Co-authored-by: Eliot Jordan <eliotjordan@users.noreply.github.com>
Co-authored-by: Shaun Ellis <sdellis@users.noreply.github.com>
Co-authored-by: Trey Pendragon <tpendragon@users.noreply.github.com>
  • Loading branch information
4 people authored and hackartisan committed Sep 6, 2023
1 parent 3d5942a commit 3fe3ab0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
12 changes: 6 additions & 6 deletions app/services/hyrax/file_set_derivatives_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class FileSetDerivativesService
attr_reader :file_set
delegate :mime_type, to: :file_set

# @param file_set [Hyrax::FileSet] At least for this class, it must have #uri and #mime_type
# @param file_set [Hyrax::FileSet, Hyrax::FileMetadata] At least for this class, it must have #uri and #mime_type
def initialize(file_set)
@file_set = file_set
end
Expand Down Expand Up @@ -91,15 +91,15 @@ def create_office_document_derivatives(filename)

def create_audio_derivatives(filename)
Hydra::Derivatives::AudioDerivatives.create(filename,
outputs: [{ label: 'mp3', format: 'mp3', url: derivative_url('mp3') },
{ label: 'ogg', format: 'ogg', url: derivative_url('ogg') }])
outputs: [{ label: 'mp3', format: 'mp3', url: derivative_url('mp3'), mime_type: 'audio/mpeg' },
{ label: 'ogg', format: 'ogg', url: derivative_url('ogg'), mime_type: 'audio/ogg' }])
end

def create_video_derivatives(filename)
Hydra::Derivatives::VideoDerivatives.create(filename,
outputs: [{ label: :thumbnail, format: 'jpg', url: derivative_url('thumbnail') },
{ label: 'webm', format: 'webm', url: derivative_url('webm') },
{ label: 'mp4', format: 'mp4', url: derivative_url('mp4') }])
outputs: [{ label: :thumbnail, format: 'jpg', url: derivative_url('thumbnail'), mime_type: 'image/jpeg' },
{ label: 'webm', format: 'webm', url: derivative_url('webm'), mime_type: 'video/webm' },
{ label: 'mp4', format: 'mp4', url: derivative_url('mp4'), mime_type: 'video/mp4' }])
end

def create_image_derivatives(filename)
Expand Down
2 changes: 1 addition & 1 deletion app/services/hyrax/valkyrie_persist_derivatives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def self.call(stream,

filename = filename(directives)
Hyrax.logger.debug "Uploading derivative for FileSet #{file_set.id} as #{filename}"
uploader.upload(io: tmpfile, filename: filename, file_set: file_set, use: file_metadata(directives))
uploader.upload(io: tmpfile, filename: filename, file_set: file_set, use: file_metadata(directives), mime_type: directives[:mime_type])
end

# The filepath will look something like
Expand Down
3 changes: 2 additions & 1 deletion app/services/hyrax/valkyrie_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ def initialize(storage_adapter: Hyrax.storage_adapter)
@storage_adapter = storage_adapter
end

def upload(filename:, file_set:, io:, use: Hyrax::FileMetadata::Use::ORIGINAL_FILE, user: nil)
def upload(filename:, file_set:, io:, use: Hyrax::FileMetadata::Use::ORIGINAL_FILE, user: nil, mime_type: nil)
streamfile = storage_adapter.upload(file: io, original_filename: filename, resource: file_set)
file_metadata = Hyrax::FileMetadata(streamfile)
file_metadata.file_set_id = file_set.id
file_metadata.pcdm_use = [use]
file_metadata.recorded_size = [io.size]
file_metadata.mime_type = mime_type if mime_type

if use == Hyrax::FileMetadata::Use::ORIGINAL_FILE
# Set file set label.
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/hyrax_file_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
mime_type { 'image/png' }
end

trait :audio_file do
mime_type { 'audio/x-wave' }
end

trait :video_file do
mime_type { 'video/mp4' }
end

trait :with_file do
transient do
file { FactoryBot.create(:uploaded_file) }
Expand Down
33 changes: 33 additions & 0 deletions spec/services/hyrax/file_set_derivatives_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,38 @@
it "can get derivative mime type arrays" do
expect(Hyrax.config.derivative_mime_type_mappings.values.map(&:class).uniq).to contain_exactly(Array)
end

describe "#create_derivatives" do
context "when given an audio file" do
let(:valid_file_set) do
FactoryBot.valkyrie_create(:hyrax_file_metadata, :audio_file, file_set_id: SecureRandom.uuid)
end

it "passes a mime-type to the audio derivatives service" do
allow(Hydra::Derivatives::AudioDerivatives).to receive(:create)
described_class.new(valid_file_set).create_derivatives('foo')
expect(Hydra::Derivatives::AudioDerivatives).to have_received(:create).with('foo', outputs: contain_exactly(hash_including(mime_type: 'audio/mpeg'), hash_including(mime_type: 'audio/ogg')))
end
end

context "when given a video file" do
let(:valid_file_set) do
FactoryBot.valkyrie_create(:hyrax_file_metadata, :video_file, file_set_id: SecureRandom.uuid)
end

it "passes a mime-type to the video derivatives service" do
allow(Hydra::Derivatives::VideoDerivatives).to receive(:create)
described_class.new(valid_file_set).create_derivatives('foo')
expect(Hydra::Derivatives::VideoDerivatives).to have_received(:create).with(
'foo',
outputs: contain_exactly(
hash_including(mime_type: 'video/mp4'),
hash_including(mime_type: 'video/webm'),
hash_including(mime_type: 'image/jpeg')
)
)
end
end
end
end
end
13 changes: 13 additions & 0 deletions spec/services/hyrax/valkyrie_persist_derivatives_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
.to change { Hyrax.custom_queries.find_files(file_set: file_set) }
.from(be_empty)
end

context "when given a mime_type directive" do
let(:directives) do
{ url: "file:///app/samvera/hyrax-webapp/derivatives/#{id}-webm.webm",
mime_type: 'video/webm' }
end

it 'adds the mime_type to the file metadata' do
described_class.call(stream, directives)
files = Hyrax.custom_queries.find_files(file_set: file_set)
expect(files.first.mime_type).to eq 'video/webm'
end
end
end

describe '.fileset_for_directives' do
Expand Down

0 comments on commit 3fe3ab0

Please sign in to comment.