luan/carrierwave-ftp

View on GitHub
lib/carrierwave/storage/ftp/ex_ftp_mixin.rb

Summary

Maintainability
A
25 mins
Test Coverage
module ExFTPMixin
  def mkdir_p(dir)
    parts = dir.split('/')
    growing_path = resolve_root(parts)
    parts.each do |part|
      next if part == ''
      growing_path = growing_path == '' ? part : File.join(growing_path, part)
      mkdir_once(growing_path)
    end
  end

  def mkdir_once(dir)
    mkdir(dir)
    chdir(dir)
  rescue Net::FTPPermError, Net::FTPTempError
    nil
  end

  def resolve_root(path)
    path.first == '~' ? '' : '/'
  end
end