Sha256: 59573b51579c37c64b477ba19cd1174e7fab78ae9cebc1bac87574a14f27271a

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

module FilenameCleaner
  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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
filename_cleaner-0.1.0 lib/filename_cleaner/filename_cleaner.rb
filename_cleaner-0.0.3 lib/filename_cleaner/filename_cleaner.rb