Class: Nextcloud::Webdav::Directory

Inherits:
Nextcloud::WebdavApi show all
Includes:
Helpers, Properties
Defined in:
lib/nextcloud/webdav/directory.rb

Overview

WebDAV class for communicating with File/directory mgmt. service

Constant Summary

Constants inherited from Nextcloud::WebdavApi

Nextcloud::WebdavApi::DAV_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#add_meta, #doc_to_hash, #get_meta, #has_dav_errors, #parse_dav_response, #parse_with_meta, #path_from_href

Methods inherited from Api

#ocs, #request, #webdav

Constructor Details

#initialize(args) ⇒ Directory

Class initializer Can be initialized with WebdavApi’s directory method or with Nextcloud::Webdav::Directory.new(…credentials…)

Parameters:

  • args (Object, Hash)

    Can be instance of Api or credentials list (url, username, password)



19
20
21
22
23
24
25
26
27
28
# File 'lib/nextcloud/webdav/directory.rb', line 19

def initialize(args)
  if args.class == Nextcloud::WebdavApi
    @api = args
  else
    super
    @api = self
  end

  @path = "/files/#{@api.username}"
end

Instance Attribute Details

#directory=(value) ⇒ Array

Returns Used to store model instances when querying with find or favorites.

Returns:

  • (Array)

    Used to store model instances when querying with find or favorites



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/nextcloud/webdav/directory.rb', line 9

class Directory < WebdavApi
  include Helpers
  include Properties

  attr_accessor :directory

  # Class initializer
  # Can be initialized with WebdavApi's directory method or with Nextcloud::Webdav::Directory.new(...credentials...)
  #
  # @param args [Object,Hash] Can be instance of Api or credentials list (url, username, password)
  def initialize(args)
    if args.class == Nextcloud::WebdavApi
      @api = args
    else
      super
      @api = self
    end

    @path = "/files/#{@api.username}"
  end

  # Query a file, find contents of directory (including information about directory)
  #
  # @param path [String] Path of file or directory to search in
  # @return [Object,Hash] Hash of error or instance of Directory model class
  def find(path = "/")
    response = @api.request(:propfind, "#{@path}/#{path}", nil, RESOURCE)
    (has_dav_errors(response)) ? has_dav_errors(response) : directory(response)
  end

  # Create a directory
  #
  # @param path [String] Path of new directory relative to base
  # @return [Hash] Returns status
  def create(path)
    response = @api.request(:mkcol, "#{@path}/#{path}")
    parse_dav_response(response)
  end

  # Create a directory
  #
  # @param path [String] Path of new directory relative to base
  # @return [Hash] Returns status
  def destroy(path)
    response = @api.request(:delete, "#{@path}/#{path}")
    parse_dav_response(response)
  end

  # Download a file
  #
  # @param path [String] Path of file to download
  # @return [String] Returns file contents
  def download(path)
    @api.request(:get, "#{@path}/#{path}", nil, nil, nil, nil, true)
  end

  # Upload a file
  #
  # @param path [String] Path of new upload
  # @return contents [Hash] Returns status
  def upload(path, contents)
    response = @api.request(:put, "#{@path}/#{path}", nil, contents)
    parse_dav_response(response)
  end

  # Move a file
  #
  # @param source [String] Source path relative to base
  # @param destination [String] Destination path relative to base
  # @return [Hash] Returns status
  def move(source, destination)
    response = @api.request(:move, "#{@path}/#{source}", nil, nil, nil,
      destination = "#{@api.url}#{@path}/#{destination}")
    parse_dav_response(response)
  end

  # Copy a file
  #
  # @param source [String] Source path relative to base
  # @param destination [String] Destination path relative to base
  # @return [Hash] Returns status
  def copy(source, destination)
    response = @api.request(:copy, "#{@path}/#{source}", nil, nil, nil,
      destination = "#{@api.url}#{@path}/#{destination}")
    parse_dav_response(response)
  end

  # Make file/directory a favorite
  #
  # @param path [String] Path of file/directory (relative)
  # @return [Hash] Returns status
  def favorite(path)
    response = @api.request(:proppatch, "#{@path}/#{path}", nil, MAKE_FAVORITE)
    parse_dav_response(response)
  end

  # Unfavorite a file/directory
  #
  # @param path [String] Path of file/directory (relative)
  # @return [Hash] Returns status
  def unfavorite(path)
    response = @api.request(:proppatch, "#{@path}/#{path}", nil, UNFAVORITE)
    parse_dav_response(response)
  end

  # Shows favorite files/directories in given location
  #
  # @param path [String] Location to list favorites from
  # @return [Hash] Hash of error or array of Directory model classes
  def favorites(path = "/")
    response = @api.request(:REPORT, "#{@path}/#{path}", nil, FAVORITE)
    (has_dav_errors(response)) ? has_dav_errors(response) : directory(response, false)
  end

  private

  # Parses as turns file/directory response to model object
  #
  # @param response [Object] Nokogiri::XML::Document
  # @param skip_first [Boolean] Skip or not first element
  # @return [Object,Array] Returns Object if first element not skipped, array otherwise
  def directory(response, skip_first = true)
    response = doc_to_hash(response).try(:[], "multistatus").try(:[], "response")
    response = [response] if response.is_a? Hash

    return [] if response.nil?

    response.each_with_index do |h, index|

      prop = h["propstat"].try(:[], 0).try(:[], "prop") || h["propstat"]["prop"]

      params = {
          href: h["href"],
          lastmodified: prop["getlastmodified"],
          tag: prop["getetag"],
          resourcetype: prop["resourcetype"].nil? ? "file" : "collection",
          id: prop["id"],
          fileid: prop["fileid"],
          permissions: prop["permissions"],
          size: prop["size"],
          has_preview: prop["has_preview"] == "false" ? false : true,
          favorite: prop["favorite"] == "0" ? false : true,
          comments_href: prop["comments_href"],
          comments_count: prop["comments_count"],
          comments_unread: prop["comments_unread"],
          owner_id: prop["owner_id"],
          owner_display_name: prop["owner_display_name"],
          share_types: prop["share_types"]
      }

      if skip_first
        if index == 0
          @directory = Models::Directory.new(params)
        else
          @directory.add(params)
        end
      else
        @directory = [] if @directory.nil?
        @directory << Models::Directory.new(params.merge(skip_contents: true))
      end
    end
    @directory
  end
end

Instance Method Details

#copy(source, destination) ⇒ Hash

Copy a file

Parameters:

  • source (String)

    Source path relative to base

  • destination (String)

    Destination path relative to base

Returns:

  • (Hash)

    Returns status



90
91
92
93
94
# File 'lib/nextcloud/webdav/directory.rb', line 90

def copy(source, destination)
  response = @api.request(:copy, "#{@path}/#{source}", nil, nil, nil,
    destination = "#{@api.url}#{@path}/#{destination}")
  parse_dav_response(response)
end

#create(path) ⇒ Hash

Create a directory

Parameters:

  • path (String)

    Path of new directory relative to base

Returns:

  • (Hash)

    Returns status



43
44
45
46
# File 'lib/nextcloud/webdav/directory.rb', line 43

def create(path)
  response = @api.request(:mkcol, "#{@path}/#{path}")
  parse_dav_response(response)
end

#destroy(path) ⇒ Hash

Create a directory

Parameters:

  • path (String)

    Path of new directory relative to base

Returns:

  • (Hash)

    Returns status



52
53
54
55
# File 'lib/nextcloud/webdav/directory.rb', line 52

def destroy(path)
  response = @api.request(:delete, "#{@path}/#{path}")
  parse_dav_response(response)
end

#download(path) ⇒ String

Download a file

Parameters:

  • path (String)

    Path of file to download

Returns:

  • (String)

    Returns file contents



61
62
63
# File 'lib/nextcloud/webdav/directory.rb', line 61

def download(path)
  @api.request(:get, "#{@path}/#{path}", nil, nil, nil, nil, true)
end

#favorite(path) ⇒ Hash

Make file/directory a favorite

Parameters:

  • path (String)

    Path of file/directory (relative)

Returns:

  • (Hash)

    Returns status



100
101
102
103
# File 'lib/nextcloud/webdav/directory.rb', line 100

def favorite(path)
  response = @api.request(:proppatch, "#{@path}/#{path}", nil, MAKE_FAVORITE)
  parse_dav_response(response)
end

#favorites(path = "/") ⇒ Hash

Shows favorite files/directories in given location

Parameters:

  • path (String) (defaults to: "/")

    Location to list favorites from

Returns:

  • (Hash)

    Hash of error or array of Directory model classes



118
119
120
121
# File 'lib/nextcloud/webdav/directory.rb', line 118

def favorites(path = "/")
  response = @api.request(:REPORT, "#{@path}/#{path}", nil, FAVORITE)
  (has_dav_errors(response)) ? has_dav_errors(response) : directory(response, false)
end

#find(path = "/") ⇒ Object, Hash

Query a file, find contents of directory (including information about directory)

Parameters:

  • path (String) (defaults to: "/")

    Path of file or directory to search in

Returns:

  • (Object, Hash)

    Hash of error or instance of Directory model class



34
35
36
37
# File 'lib/nextcloud/webdav/directory.rb', line 34

def find(path = "/")
  response = @api.request(:propfind, "#{@path}/#{path}", nil, RESOURCE)
  (has_dav_errors(response)) ? has_dav_errors(response) : directory(response)
end

#move(source, destination) ⇒ Hash

Move a file

Parameters:

  • source (String)

    Source path relative to base

  • destination (String)

    Destination path relative to base

Returns:

  • (Hash)

    Returns status



79
80
81
82
83
# File 'lib/nextcloud/webdav/directory.rb', line 79

def move(source, destination)
  response = @api.request(:move, "#{@path}/#{source}", nil, nil, nil,
    destination = "#{@api.url}#{@path}/#{destination}")
  parse_dav_response(response)
end

#unfavorite(path) ⇒ Hash

Unfavorite a file/directory

Parameters:

  • path (String)

    Path of file/directory (relative)

Returns:

  • (Hash)

    Returns status



109
110
111
112
# File 'lib/nextcloud/webdav/directory.rb', line 109

def unfavorite(path)
  response = @api.request(:proppatch, "#{@path}/#{path}", nil, UNFAVORITE)
  parse_dav_response(response)
end

#upload(path, contents) ⇒ Object

Upload a file

Parameters:

  • path (String)

    Path of new upload



69
70
71
72
# File 'lib/nextcloud/webdav/directory.rb', line 69

def upload(path, contents)
  response = @api.request(:put, "#{@path}/#{path}", nil, contents)
  parse_dav_response(response)
end