Sha256: 7153570cfa41f8a1e1966b8a82b8e818e9b9a60deb1ab8b0dbb04f451404734c

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module FilenameCleaner
  class Utils
    DOT = '.'
    class << self
      # Sanitize filename that have the extension
      #
      # @param [String] filename the input filename with extension
      # @retyrn [String] the output file with special characters replaced.
      def sanitize_filename(filename, sep_char = nil)
        extension = File.extname(filename)

        if extension.empty?
          replace_dot!(sanitize(filename), sep_char)
        else
          name_only = File.basename(filename, ".*")
          name_only = replace_dot!(sanitize(name_only), sep_char)
          "#{name_only}#{extension}"
        end
      end

      # Clean the the input string to remove the special characters
      #
      # @param [String] filename input file
      # @return [String] the new file name with special characters replaced or removed.
      def sanitize(filename)
        # remove anything that is not letters, numbers, dash, underscore or space
        # Note: we intentionally ignore dot from the list
        filename.gsub!(/[^0-9A-Za-z\-_ ]/, DOT)

        # replace multiple occurrences of a given char with a dot
        ['-', '_', ' '].each do |c|
          filename.gsub!(/#{Regexp.quote(c)}+/, DOT)
        end

        # replace multiple occurrence of dot with one dot
        filename.gsub!(/#{Regexp.quote(DOT)}+/, DOT)

        filename
      end

      private

      # replace 'dot' string with a agiven string if any
      def replace_dot!(string, replace = nil)
        string.gsub!(/#{Regexp.quote(DOT)}+/, replace) if replace
        string
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
filename_cleaner-0.0.2 lib/filename_cleaner/utils.rb
filename_cleaner-0.0.1 lib/filename_cleaner/utils.rb