lib/ddr/utils.rb in ddr-models-2.11.0 vs lib/ddr/utils.rb in ddr-models-3.0.0.alpha.1

- old
+ new

@@ -1,54 +1,53 @@ require 'openssl' module Ddr::Utils - extend Deprecation - def self.digest content, algorithm + DEFAULT_MIME_TYPE = "application/octet-stream" + + def self.digest(content, algorithm) raise TypeError, "Algorithm must be a string: #{algorithm.inspect}" unless algorithm.is_a?(String) digest_class = OpenSSL::Digest.const_get(algorithm.sub("-", "").to_sym) digest_class.new(content).to_s rescue NameError => e raise ArgumentError, "Invalid algorithm: #{algorithm}" end # Return a mime type for the file, using the file_name if necessary # file can be a File object or file path (String) - # @return [String] the mime type or default + # Return default mime type if unable to determine otherwise def self.mime_type_for(file, file_name=nil) - Deprecation.warn(self, "`Ddr::Utils.mime_type_for` is deprecated and will be removed in ddr-models 3.0." \ - " Use `Ddr::Models::MediaType.call(file)` instead. (called from #{caller.first})") - Ddr::Models::MediaType.call(file_name || file) + return file.content_type if file.respond_to?(:content_type) # E.g., Rails uploaded file + path = file_name || file_path(file) rescue nil + mime_types = MIME::Types.of(path) rescue [] # MIME::Types.of blows up on nil + mime_types.empty? ? DEFAULT_MIME_TYPE : mime_types.first.content_type end def self.file_or_path?(file) file_path(file) rescue ArgumentError false end def self.file_path?(file) # length is a sanity check - file.is_a?(String) && (file.length < 1024) && File.exist?(file) + file.is_a?(String) && (file.length < 1024) && File.exists?(file) end def self.file_path(file) if file.respond_to?(:path) File.absolute_path(file.path) elsif file_path?(file) file else - raise ArgumentError, "Argument is neither a File nor a path to an existing file." + raise ArgumentError, "File argument is neither a File nor a path to an existing file." end end def self.file_name_for(file) - if file.respond_to?(:original_filename) && file.original_filename.present? - file.original_filename - else - File.basename file_path(file) - end + return file.original_filename if file.respond_to?(:original_filename) && file.original_filename.present? + File.basename(file_path(file)) rescue nil end def self.file_uri?(uri) return false unless uri URI.parse(uri).scheme == "file" @@ -62,27 +61,19 @@ end # Return file path for URI string # Should reverse .path_to_uri # "file:/path/to/file" => "/path/to/file" - # @param uri [String] The URI string to pathify - # @return [String] the file path - def self.path_from_uri(uri_string) - uri = URI.parse(uri_string) - unless uri.scheme == "file" - raise ArgumentError, "URI does not have the file: scheme." - end - URI.unescape(uri.path) + def self.path_from_uri(uri) + URI.unescape(URI.parse(uri).path) end # Return URI string for file path # Should reverse .path_from_uri # "/path/to/file" => "file:/path/to/file" - # @param path [String] the file path - # @return [String] the file: URI string def self.path_to_uri(path) - uri = URI.parse URI.escape(path) + uri = URI.parse(URI.escape(path)) uri.scheme = "file" uri.to_s end def self.ds_as_of_date_time(ds) @@ -174,12 +165,8 @@ dt.to_time.utc.iso8601 end def self.solr_dates(dts) dts.map { |dt| solr_date(dt) } - end - - class << self - alias_method :file_name, :file_name_for end end