lib/ddr/utils.rb in ddr-models-2.6.2 vs lib/ddr/utils.rb in ddr-models-2.7.0.rc1

- old
+ new

@@ -1,79 +1,88 @@ require 'openssl' module Ddr::Utils - 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 default mime type if unable to determine otherwise + # @return [String] the mime type or default def self.mime_type_for(file, file_name=nil) 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 + mime_types.empty? ? Ddr::Models.default_mime_type : mime_types.first.content_type end - def self.file_or_path? file + def self.file_or_path?(file) file_path(file) rescue ArgumentError false end - def self.file_path? file + def self.file_path?(file) # length is a sanity check - file.is_a?(String) && (file.length < 1024) && File.exists?(file) + file.is_a?(String) && (file.length < 1024) && File.exist?(file) end - def self.file_path file + def self.file_path(file) if file.respond_to?(:path) File.absolute_path(file.path) elsif file_path?(file) file else - raise ArgumentError, "File argument is neither a File nor a path to an existing file." + raise ArgumentError, "Argument is neither a File nor a path to an existing file." end end - def self.file_name_for file - return file.original_filename if file.respond_to?(:original_filename) && file.original_filename.present? - File.basename(file_path(file)) rescue nil + 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 end def self.file_uri?(uri) return false unless uri URI.parse(uri).scheme == "file" end - def self.sanitize_filename file_name + def self.sanitize_filename(file_name) return unless file_name raise ArgumentError, "file_name argument must be a string" unless file_name.is_a?(String) raise ArgumentError, "file_name argument must not include path" if file_name.include?(File::SEPARATOR) file_name.gsub(/[^\w\.\-]/,"_") end # Return file path for URI string # Should reverse .path_to_uri # "file:/path/to/file" => "/path/to/file" - def self.path_from_uri(uri) - URI.unescape(URI.parse(uri).path) + # @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) 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) @@ -165,8 +174,12 @@ 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