class ADXToolkit::GdriveUploader
Upload file to gdrive
Public Class Methods
new(runner)
click to toggle source
# File lib/gdrive_uploader.rb, line 10 def initialize(runner) @system_runner = runner end
Public Instance Methods
check_file_validity(file_path)
click to toggle source
# 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')
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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