class Shrine::Storage::WebDAV

Public Class Methods

new(host:, prefix: nil, upload_options: {}, http_options: {}) click to toggle source
# File lib/shrine/storage/webdav.rb, line 8
def initialize(host:, prefix: nil, upload_options: {}, http_options: {})
  @host = host
  @prefix = prefix
  @prefixed_host = path(@host, @prefix)
  @upload_options = upload_options
  @http_options = http_options
end

Public Instance Methods

delete(id) click to toggle source
# File lib/shrine/storage/webdav.rb, line 40
def delete(id)
  http_client.delete(path(@prefixed_host, id))
end
exists?(id) click to toggle source
# File lib/shrine/storage/webdav.rb, line 35
def exists?(id)
  response = http_client.head(path(@prefixed_host, id))
  (200..299).cover?(response.code.to_i)
end
open(id) click to toggle source
# File lib/shrine/storage/webdav.rb, line 27
def open(id)
  Down::Http.open(path(@prefixed_host, id)) do |client|
    client.timeout(http_timeout) if http_timeout
    client.basic_auth(http_basic_auth) if http_basic_auth
    client
  end
end
upload(io, id, shrine_metadata: {}, **upload_options) click to toggle source
# File lib/shrine/storage/webdav.rb, line 16
def upload(io, id, shrine_metadata: {}, **upload_options)
  options = current_options(upload_options)
  mkpath_to_file(id) unless options[:create_full_put_path]
  put(id, io)
end
url(id, host: nil, **options) click to toggle source
# File lib/shrine/storage/webdav.rb, line 22
def url(id, host: nil, **options)
  base_url = path(host || @host, options[:prefix] || @prefix)
  path(base_url, id)
end

Private Instance Methods

create_prefix() click to toggle source
# File lib/shrine/storage/webdav.rb, line 72
def create_prefix
  mkpath(@host, @prefix) unless @prefix.nil? || @prefix.empty?
end
current_options(upload_options) click to toggle source
# File lib/shrine/storage/webdav.rb, line 46
def current_options(upload_options)
  options = {}
  options.update(@upload_options)
  options.update(upload_options)
end
http_basic_auth() click to toggle source
# File lib/shrine/storage/webdav.rb, line 102
def http_basic_auth
  @http_options.fetch(:basic_auth, false)
end
http_client() click to toggle source
# File lib/shrine/storage/webdav.rb, line 91
def http_client
  client = HTTP
  client = client.timeout(http_timeout) if http_timeout
  client = client.basic_auth(http_basic_auth) if http_basic_auth
  client
end
http_timeout() click to toggle source
# File lib/shrine/storage/webdav.rb, line 98
def http_timeout
  @http_options.fetch(:timeout, false)
end
mkpath(host, path) click to toggle source
# File lib/shrine/storage/webdav.rb, line 76
def mkpath(host, path)
  dirs = []
  path.split('/').each do |dir|
    dirs << "#{dirs[-1]}/#{dir}"
  end
  dirs.each do |dir|
    response = http_client.request(:mkcol, "#{host}#{dir}")
    unless (200..301).cover?(response.code.to_i)
      raise Error, "creation of directory #{host}#{dir} failed, the server response was #{response}"
    end
  end
end
mkpath_to_file(path_to_file) click to toggle source
# File lib/shrine/storage/webdav.rb, line 63
def mkpath_to_file(path_to_file)
  @prefix_created ||= create_prefix
  last_slash = path_to_file.rindex('/')
  if last_slash
    path = path_to_file[0..last_slash]
    mkpath(@prefixed_host, path)
  end
end
path(host, uri) click to toggle source
# File lib/shrine/storage/webdav.rb, line 59
def path(host, uri)
  (uri.nil? || uri.empty?) ? host : [host, uri].compact.join('/')
end
put(id, io) click to toggle source
# File lib/shrine/storage/webdav.rb, line 52
def put(id, io)
  uri = path(@prefixed_host, id)
  response = http_client.put(uri, body: io)
  return if (200..299).cover?(response.code.to_i)
  raise Error, "uploading of #{uri} failed, the server response was #{response}"
end