Module: Axlsx::MimeTypeUtils
- Defined in:
- lib/axlsx/util/mime_type_utils.rb
Overview
This module defines some utils related with mime type detection
Constant Summary collapse
- EXTENSION_FALLBACKS =
Extension to MIME type mapping for Windows fallback
{ '.jpg' => 'image/jpeg', '.jpeg' => 'image/jpeg', '.png' => 'image/png', '.gif' => 'image/gif' }.freeze
Class Method Summary collapse
-
.get_mime_type(v) ⇒ String
Detect a file mime type.
-
.get_mime_type_from_uri(v) ⇒ String
Detect a file mime type from URI.
Class Method Details
.get_mime_type(v) ⇒ String
Detect a file mime type
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/axlsx/util/mime_type_utils.rb', line 18 def get_mime_type(v) mime_type = Marcel::MimeType.for(Pathname.new(v)) # Windows fallback: Marcel sometimes returns application/octet-stream for valid image files if mime_type == 'application/octet-stream' && windows_platform? extension = File.extname(v).downcase # Verify it's actually an image by checking the file header if EXTENSION_FALLBACKS.key?(extension) && File.exist?(v) && valid_image_file?(v, extension) mime_type = EXTENSION_FALLBACKS[extension] end end mime_type end |
.get_mime_type_from_uri(v) ⇒ String
Detect a file mime type from URI
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/axlsx/util/mime_type_utils.rb', line 36 def get_mime_type_from_uri(v) uri = URI.parse(v) unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) raise URI::InvalidURIError, "Only HTTP/HTTPS URIs are supported" end response = UriUtils.fetch_headers(uri) mime_type = response&.[]('content-type')&.split(';')&.first&.strip if mime_type.nil? || mime_type.empty? raise ArgumentError, "Unable to determine MIME type from response" end mime_type end |