Class: ADXToolkit::GdriveUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/gdrive_uploader.rb

Overview

Upload file to gdrive

Instance Method Summary collapse

Constructor Details

#initialize(runner) ⇒ GdriveUploader

Returns a new instance of GdriveUploader.



10
11
12
# File 'lib/gdrive_uploader.rb', line 10

def initialize(runner)
  @system_runner = runner
end

Instance Method Details

#check_file_validity(file_path) ⇒ Object



14
15
16
# File 'lib/gdrive_uploader.rb', line 14

def check_file_validity(file_path)
  abort("Can't find file at #{file_path}") unless File.exist?(file_path)
end

#download(file_path: '.', file_name:, directory_id: 'root') ⇒ Object



53
54
55
56
57
58
59
# File 'lib/gdrive_uploader.rb', line 53

def download(file_path: '.', file_name:, directory_id: 'root')
  file_id = get_file_id(directory_id, file_name)
  abort "#{file_name} not found in directory id #{directory_id}" unless file_id

  command = "gdrive download -f -r --path \"#{file_path}\" \"#{file_id}\""
  @system_runner.run command
end

#get_file_id(directory_id, file_name_prefix) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/gdrive_uploader.rb', line 45

def get_file_id(directory_id, file_name_prefix)
  ## Get FILEID of the file with same file_name_prefix if exists
  command = "gdrive list -q \"name contains '#{file_name_prefix}' and '#{directory_id}' in parents\" "
  command += "--order \"modifiedTime desc\" -m 1 --no-header | awk '{print $1;}'"
  file_id = @system_runner.run_with_output command
  file_id
end

#update(file_path, file_name, file_id) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/gdrive_uploader.rb', line 28

def update(file_path, file_name, file_id)
  check_file_validity(file_path)

  # p "updating #{file_name} using file #{file_path} with file id #{file_id}"
  command = "gdrive update --name \"#{file_name}\" \"#{file_id}\" \"#{file_path}\""
  @system_runner.run command
end

#upload(file_path, file_name, directory_id) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/gdrive_uploader.rb', line 18

def upload(file_path, file_name, directory_id)
  check_file_validity(file_path)

  # p "uploading file #{file_path} with file_name #{file_name}"
  command = "gdrive upload --share -p \"#{directory_id}\" --name \"#{file_name}\" \"#{file_path}\""
  command += " |  grep -i https | cut -d' ' -f7"
  share_link = @system_runner.run_with_output command
  share_link
end

#upload_or_update(file_path, file_name, directory_id) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/gdrive_uploader.rb', line 36

def upload_or_update(file_path, file_name, directory_id)
  file_id = get_file_id(directory_id, file_name)
  if file_id.empty?
    upload(file_path, file_name, directory_id)
  else
    update(file_path, file_name, file_id)
  end
end