Class: FileNamingHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/file_naming_helper.rb

Class Method Summary collapse

Class Method Details

.sanitize_file_name(filename) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/file_naming_helper.rb', line 3

def self.sanitize_file_name(filename)
  base = File.basename(filename, File.extname(filename))
  ext = File.extname(filename)

  # Replace any non-word characters with a dash, strip extra dashes/spaces
  sanitized = base
    .gsub(/[^\w\s-]/, '')        # Remove anything not word, space, or dash
    .gsub(/\s+/, '-')            # Replace spaces with dashes
    .gsub(/-+/, '-')             # Squeeze repeated dashes
    .downcase                    # Optional: downcase

  "#{sanitized}#{ext}"
end