Skip to content

Commit 3f53e45

Browse files
committed
Raise if unknown mimetype
As part of this I've registered the 'wav' mimetype as some scratch assets are wav files.
1 parent 8d8418e commit 3f53e45

4 files changed

Lines changed: 19 additions & 2 deletions

File tree

config/initializers/mime_types.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
Mime::Type.register 'audio/vnd.wav', :wav

lib/scratch_asset_importer.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def initialize(asset_name, asset_base_url)
3434
def import
3535
create_scratch_asset
3636
save_to_editor_asset_bucket
37-
rescue StandardError => e
37+
rescue Faraday::Error => e
3838
Rails.logger.error("Failed to import asset #{asset_name}: #{e.message}")
3939
end
4040

@@ -84,7 +84,10 @@ def asset_key
8484

8585
def asset_content_type
8686
extension = File.extname(asset_name).delete('.')
87-
Mime::Type.lookup_by_extension(extension).to_s
87+
mime_type = Mime::Type.lookup_by_extension(extension)
88+
raise "Unknown content type for extension: #{extension}" unless mime_type
89+
90+
mime_type.to_s
8891
end
8992

9093
def s3_client
6.25 KB
Binary file not shown.

spec/lib/scratch_asset_importer_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@
105105
described_class.import_all(['123abc.png'], 'https://example.net/internalapi/asset/')
106106
expect(s3_client).not_to have_received(:put_object)
107107
end
108+
109+
it 'raises if asset is an unknown mimetype' do
110+
image = Rails.root.join('spec/fixtures/files/unknown_mime_type.xyz').read
111+
stub_request(:get, 'https://example.net/internalapi/asset/unknown_mime_type.xyz/get/').to_return(status: 200, body: image)
112+
113+
expect do
114+
described_class.import_all(['unknown_mime_type.xyz'], 'https://example.net/internalapi/asset/')
115+
end.to raise_error('Unknown content type for extension: xyz')
116+
117+
expect(s3_client).not_to have_received(:put_object)
118+
end
108119
end
109120
end
110121
end

0 commit comments

Comments
 (0)