Class: Service::WebDAVService

Inherits:
Service
  • Object
show all
Defined in:
lib/active_storage/service/web_dav_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ WebDAVService



7
8
9
10
11
# File 'lib/active_storage/service/web_dav_service.rb', line 7

def initialize(args)
  @path = args[:url]
  return @webdav = args[:net_dav] if args[:net_dav]
  @webdav = Net::DAV.new args[:url]
end

Instance Method Details

#delete(key) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/active_storage/service/web_dav_service.rb', line 48

def delete(key)
  instrument :delete, key: key do
    begin
      @webdav.delete path_for key
    rescue StandardError
      # Ignore files already deleted
    end
  end
end

#delete_prefixed(prefix) ⇒ Object

Delete files at keys starting with the prefix.



59
60
61
62
63
64
65
66
67
# File 'lib/active_storage/service/web_dav_service.rb', line 59

def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    files = prefixed_filenames prefix
    files.each do |filename|
      url = path_for filename
      @webdav.delete url
    end
  end
end

#download(key, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_storage/service/web_dav_service.rb', line 69

def download(key, &block)
  full_path = path_for key
  if block_given?
    instrument :streaming_download, key: key do
      @webdav.get(full_path, &block)
    end
  else
    instrument :download, key: key do
      @webdav.get(full_path)
    end
  end
end

#download_chunk(key, range) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/active_storage/service/web_dav_service.rb', line 82

def download_chunk(key, range)
  instrument :download_chunk, key: key, range: range do
    range_end = range.exclude_end? ? range.end - 1 : range.end
    range_for_dav = "bytes=#{range.begin}-#{range_end}"

    full_path = path_for key
    @webdav.get(full_path, 'Range' => range_for_dav)
  end
end

#exist?(key) ⇒ Boolean



40
41
42
43
44
45
46
# File 'lib/active_storage/service/web_dav_service.rb', line 40

def exist?(key)
  instrument :exist, key: key do |payload|
    answer = @webdav.exists? path_for key
    payload[:exist] = answer
    answer
  end
end

#upload(key, io, checksum: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/active_storage/service/web_dav_service.rb', line 13

def upload(key, io, checksum: nil)
  instrument :upload, key: key, checksum: checksum do
    begin
      full_path = path_for key
      answer = @webdav.put(full_path, io, File.size(io))
    rescue StandardError
      raise ActiveStorage::IntegrityError
    end
  end
end

#url(key, expires_in:, disposition:, filename:, content_type:) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/active_storage/service/web_dav_service.rb', line 24

def url(key, expires_in:, disposition:, filename:, content_type:)
  instrument :url, key: key do |payload|
    full_path = path_for key
    payload[:url] = full_path
    full_path
  end
end

#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/active_storage/service/web_dav_service.rb', line 32

def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
  instrument :url, key: key do |payload|
    full_path = path_for key
    payload[:url] = full_path
    full_path
  end
end