Method: CloudDrive::Node#upload_file

Defined in:
lib/clouddrive/node.rb

#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.expand_path(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