class ActiveStorage::Service::FtpService

Attributes

config[R]

Public Class Methods

new(**config) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 11
def initialize(**config)
  @config = config
end

Public Instance Methods

delete(key) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 58
def delete(key)
  instrument :delete, key: key do
    begin
      connection do |ftp|
        ftp.chdir(::File.dirname path_for(key))
        ftp.delete(::File.basename path_for(key))
      end
    rescue
      # Ignore files already deleted
    end
  end
end
delete_prefixed(prefix) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 71
def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    connection do |ftp|
      ftp.chdir(path_for(prefix))
      ftp.list.each do |file|
        ftp.delete(file.split.last)
      end
    end
  end
end
download(key) { |data| ... } click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 31
def download(key)
  if block_given?
    instrument :streaming_download, key: key do
      open(http_url_for(key)) do |file|
        while data = file.read(64.kilobytes)
          yield data
        end
      end
    end
  else
    instrument :download, key: key do
      open(http_url_for(key)) do |file|
        file.read
      end
    end
  end
end
download_chunk(key, range) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 49
def download_chunk(key, range)
  instrument :download_chunk, key: key, range: range do
    open(http_url_for(key)) do |file|
        file.seek range.begin
        file.read range.size
    end
  end
end
exist?(key) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 82
def exist?(key)
  instrument :exist, key: key do |payload|
    response = request_head(key)
    answer = response.code.to_i == 200
    payload[:exist] = answer
    answer
  end
end
headers_for_direct_upload(key, content_type:, **) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 119
def headers_for_direct_upload(key, content_type:, **)
  {"Content-Type" => content_type}
end
upload(key, io, checksum: nil, **) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 15
def upload(key, io, checksum: nil, **)
  instrument :upload, key: key, checksum: checksum do
    connection do |ftp|
      path_for(key).tap do |path|
        ftp.mkdir_p(::File.dirname path)
        ftp.chdir(::File.dirname path)
        ftp.storbinary("STOR #{File.basename(key)}", io, Net::FTP::DEFAULT_BLOCKSIZE)
        if ftp_chmod
          ftp.sendcmd("SITE CHMOD #{ftp_chmod.to_s(8)} #{path_for(key)}")
        end
      end
    end
    ensure_integrity_of(key, checksum) if checksum
  end
end
url(key, expires_in:, filename:, disposition:, content_type:) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 91
def url(key, expires_in:, filename:, disposition:, content_type:)
  instrument :url, key: key do |payload|
    generated_url = http_url_for(key)
    payload[:url] = generated_url
    generated_url
  end
end
url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 99
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
  instrument :url, key: key do |payload|
    verified_token_with_expiration = ActiveStorage.verifier.generate(
        {
            key: key,
            content_type: content_type,
            content_length: content_length,
            checksum: checksum
        },
        {expires_in: expires_in,
         purpose: :blob_token}
    )

    generated_url = url_helpers.update_rails_disk_service_url(verified_token_with_expiration, host: current_host)
    payload[:url] = generated_url
    generated_url

  end
end

Private Instance Methods

connection() { |ftp| ... } click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 198
def connection
  ftp = ExFTP.new
  ftp.connect(ftp_host, ftp_port)
  begin
    ftp.passive = ftp_passive
    ftp.login(ftp_user, ftp_passwd)
    yield ftp
  ensure
    ftp.quit
  end
end
current_host() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 147
def current_host
  ActiveStorage::Current.host
end
ensure_integrity_of(key, checksum) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 135
def ensure_integrity_of(key, checksum)
  response = request_head(key)
  unless "#{response['Content-MD5']}==" == checksum
    delete key
    raise ActiveStorage::IntegrityError
  end
end
folder_for(key) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 131
def folder_for(key)
  [key[0..1], key[2..3]].join("/")
end
ftp_chmod() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 194
def ftp_chmod
  config.fetch(:ftp_chmod, 0600)
end
ftp_folder() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 182
def ftp_folder
  config.fetch(:ftp_folder)
end
ftp_host() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 166
def ftp_host
  config.fetch(:ftp_host)
end
ftp_passive() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 190
def ftp_passive
  config.fetch(:ftp_passive)
end
ftp_passwd() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 178
def ftp_passwd
  config.fetch(:ftp_passwd)
end
ftp_port() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 170
def ftp_port
  config.fetch(:ftp_port)
end
ftp_url() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 186
def ftp_url
  config.fetch(:ftp_url)
end
ftp_user() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 174
def ftp_user
  config.fetch(:ftp_user)
end
http_url_for(key) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 158
def http_url_for(key)
  ([ftp_url, folder_for(key), key].join('/'))
end
inferred_content_type() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 162
def inferred_content_type
  SanitizedFile.new(path).content_type
end
request_head(key) click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 151
def request_head(key)
  uri = URI(http_url_for(key))
  request = Net::HTTP.new(uri.host, uri.port)
  request.use_ssl = uri.scheme == 'https'
  request.request_head(uri.path)
end
url_helpers() click to toggle source
# File lib/active_storage/service/ftp_service.rb, line 143
def url_helpers
  @url_helpers ||= Rails.application.routes.url_helpers
end