ministryofjustice/atet

View on GitHub
app/services/filename_cleaner.rb

Summary

Maintainability
A
0 mins
Test Coverage
module FilenameCleaner
  def for(attachment, underscore: false)
    file_path = attachment&.dig('filename')
    return unless file_path

    underscore ? underscore_filename_for(file_path) : filename_for(file_path)
  end

  private

  # Jadu API disallows filenames with non alphanumeric
  # characters so we replace them all with underscores.

  def underscore_filename_for(path)
    File.basename(path, '.*').
      gsub(/[^a-zA-Z0-9]/, '_').
      concat(File.extname(path))
  end

  def filename_for(path)
    File.basename(path)
  end

  module_function :for, :underscore_filename_for, :filename_for
end