Class: CloudDrive::Node
- Inherits:
-
Object
- Object
- CloudDrive::Node
- Defined in:
- lib/clouddrive/node.rb
Instance Method Summary collapse
- #build_node_path(node) ⇒ Object
- #create_directory_path(path) ⇒ Object
- #create_new_folder(name, parent_id = nil) ⇒ Object
-
#exists?(remote_file, local_file = nil) ⇒ Boolean
If given a local file, the MD5 will be compared as well.
- #find_by_id(id) ⇒ Object
- #find_by_md5(hash) ⇒ Object
- #find_by_name(name) ⇒ Object
- #find_by_path(path) ⇒ Object
- #get_path_array(path) ⇒ Object
- #get_path_string(path) ⇒ Object
- #get_root ⇒ Object
-
#initialize(account) ⇒ Node
constructor
A new instance of Node.
- #upload_dir(src_path, dest_root, show_progress = false) ⇒ Object
- #upload_file(src_path, dest_path) ⇒ Object
Constructor Details
#initialize(account) ⇒ Node
Returns a new instance of Node.
11 12 13 |
# File 'lib/clouddrive/node.rb', line 11 def initialize(account) @account = account end |
Instance Method Details
#build_node_path(node) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/clouddrive/node.rb', line 15 def build_node_path(node) path = [] loop do path.push node["name"] break if node.has_key?('isRoot') && node['isRoot'] == true results = find_by_id(node["parents"][0]) if results[:success] == false raise "No parent node found with ID #{node["parents"][0]}" end node = results[:data] break if node.has_key?('isRoot') && node['isRoot'] === true end path = path.reverse path.join('/') end |
#create_directory_path(path) ⇒ Object
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 |
# File 'lib/clouddrive/node.rb', line 70 def create_directory_path(path) retval = { :success => true, :data => {} } path = get_path_array(path) previous_node = get_root match = nil path.each_with_index do |folder, index| xary = path.slice(0, index + 1) if (match = find_by_path(xary.join('/'))) === nil result = create_new_folder(folder, previous_node["id"]) if result[:success] === false return result end match = result[:data] end previous_node = match end if match == nil retval[:data] = previous_node else retval[:data] = match end retval end |
#create_new_folder(name, parent_id = nil) ⇒ Object
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 |
# File 'lib/clouddrive/node.rb', line 36 def create_new_folder(name, parent_id = nil) if parent_id == nil parent_id = get_root['id'] end body = { :name => name, :parents => [ parent_id ], :kind => "FOLDER" } retval = { :success => false, :data => [] } RestClient.post( "#{@account.}nodes", body.to_json, :Authorization => "Bearer #{@account.access_token}" ) do |response, request, result| data = JSON.parse(response.body) retval[:data] = data if response.code === 201 retval[:success] = true @account.save_node(data) end end retval end |
#exists?(remote_file, local_file = nil) ⇒ Boolean
If given a local file, the MD5 will be compared as well
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 |
# File 'lib/clouddrive/node.rb', line 104 def exists?(remote_file, local_file = nil) if (file = find_by_path(remote_file)) == nil if local_file != nil if (file = find_by_md5(Digest::MD5.file(local_file).to_s)) != nil path = build_node_path(file) return { :success => true, :data => { "message" => "File with same MD5 exists at #{path}: #{file.to_json}" } } end end return { :success => false, :data => { "message" => "File #{remote_file} does not exist" } } end retval = { :success => true, :data => { "message" => "File #{remote_file} exists" } } if local_file != nil if file["contentProperties"] != nil && file["contentProperties"]["md5"] != nil if Digest::MD5.file(local_file).to_s != file["contentProperties"]["md5"] retval[:data]["message"] = "File #{remote_file} exists butuum doesn't match" else retval[:data]["message"] = "File #{remote_file} exists and is identical" end else retval[:data]["message"] = "File #{remote_file} exists, but no checksum is available" end end retval end |
#find_by_id(id) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/clouddrive/node.rb', line 147 def find_by_id(id) retval = { :success => false, :data => {} } results = @account.db.execute("SELECT raw_data FROM nodes WHERE id = ?;", id) if results.empty? return retval end if results.count > 1 raise "Multiple nodes with same ID found: #{results[:data].to_json}" end { :success => true, :data => JSON.parse(results[0][0]) } end |
#find_by_md5(hash) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/clouddrive/node.rb', line 168 def find_by_md5(hash) results = @account.db.execute("SELECT raw_data FROM nodes WHERE md5 = ?;", hash) if results.empty? return nil end if results.count > 1 raise "Multiple nodes with same MD5: #{results.to_json}" end JSON.parse(results[0][0]) end |
#find_by_name(name) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/clouddrive/node.rb', line 182 def find_by_name(name) retval = [] results = @account.db.execute("SELECT raw_data FROM nodes WHERE name = ?;", name) if results.empty? return retval end results.each do |result| retval.push(JSON.parse(result[0])) end retval end |
#find_by_path(path) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/clouddrive/node.rb', line 196 def find_by_path(path) path = path.gsub(/\A\//, '') path = path.gsub(/\/$/, '') path_info = Pathname.new(path) found_nodes = find_by_name(path_info.basename.to_s) if found_nodes.empty? return nil end match = nil found_nodes.each do |node| if build_node_path(node) == path match = node end end match end |
#get_path_array(path) ⇒ Object
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/clouddrive/node.rb', line 216 def get_path_array(path) return path if path.kind_of?(Array) path = path.split('/') path.reject! do |value| value.empty? end path end |
#get_path_string(path) ⇒ Object
227 228 229 230 231 |
# File 'lib/clouddrive/node.rb', line 227 def get_path_string(path) path = path.join '/' if path.kind_of?(Array) path.chomp end |
#get_root ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/clouddrive/node.rb', line 233 def get_root results = find_by_name('root') if results.empty? raise "No node by the name of 'root' found in database" end results.each do |node| if node.has_key?('isRoot') && node['isRoot'] === true return node end end nil end |
#upload_dir(src_path, dest_root, show_progress = false) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/clouddrive/node.rb', line 248 def upload_dir(src_path, dest_root, show_progress = false) src_path = File.(src_path) dest_root = get_path_array(dest_root) dest_root.push(get_path_array(src_path).last) dest_root = get_path_string(dest_root) retval = [] Find.find(src_path) do |file| # Skip root directory, no need to make it next if file == src_path || File.directory?(file) path_info = Pathname.new(file) remote_dest = path_info.dirname.sub(src_path, dest_root).to_s result = upload_file(file, remote_dest) if show_progress == true if result[:success] == true puts "Successfully uploaded file #{file}: #{result[:data].to_json}" else puts "Failed to uploaded file #{file}: #{result[:data].to_json}" end end retval.push(result) # Since uploading a directory can take a while (depending on number/size of files) # we will check if we need to renew our authorization after each file upload to # make sure our authentication doesn't expire. if (Time.new.to_i - @account.token_store["last_authorized"]) > 60 result = @account. if result[:success] === false raise "Failed to renew authorization: #{result[:data].to_json}" end end end retval end |
#upload_file(src_path, dest_path) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/clouddrive/node.rb', line 288 def upload_file(src_path, dest_path) retval = { :success => false, :data => [] } path_info = Pathname.new(src_path) dest_path = get_path_string(get_path_array(dest_path)) dest_folder = create_directory_path(dest_path) result = exists?("#{dest_path}/#{path_info.basename}", src_path) if result[:success] == true retval[:data] = result[:data] return retval end body = { :metadata => { :kind => 'FILE', :name => path_info.basename, :parents => [ dest_folder["id"] ] }.to_json, :content => File.new(File.(src_path), 'rb') } RestClient.post("#{@account.content_url}nodes", body, :Authorization => "Bearer #{@account.access_token}") do |response, request, result| retval[:data] = JSON.parse(response.body) if response.code === 201 retval[:success] = true @account.save_node(retval[:data]) end end retval end |