Class: Lt::Google::Api::Drive
- Inherits:
-
Object
- Object
- Lt::Google::Api::Drive
- Defined in:
- lib/lt/google/api/drive.rb
Constant Summary collapse
- FOLDER_RE =
%r{/drive/(.*/)?folders/([^\/?]+)/?}- MIME_FILE =
'application/vnd.google-apps.document'- MIME_FOLDER =
'application/vnd.google-apps.folder'
Instance Attribute Summary collapse
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Class Method Summary collapse
Instance Method Summary collapse
- #copy(file_ids, folder_id) ⇒ Object
- #copy_files(folder_id, target_id) ⇒ Object
- #create_folder(name, parent_id = nil) ⇒ Object
- #fetch_folders(name, folder_id) ⇒ Object
-
#initialize(credentials) ⇒ Drive
constructor
A new instance of Drive.
- #list_file_ids_in(folder_id, mime_type: MIME_FILE, with_subfolders: true) ⇒ Object
Constructor Details
#initialize(credentials) ⇒ Drive
Returns a new instance of Drive.
27 28 29 30 |
# File 'lib/lt/google/api/drive.rb', line 27 def initialize(credentials) @service = ::Google::Apis::DriveV3::DriveService.new @service. = credentials end |
Instance Attribute Details
#service ⇒ Object (readonly)
Returns the value of attribute service.
11 12 13 |
# File 'lib/lt/google/api/drive.rb', line 11 def service @service end |
Class Method Details
.build(credentials) ⇒ Object
14 15 16 |
# File 'lib/lt/google/api/drive.rb', line 14 def build(credentials) new(credentials).service end |
.file_url_for(file_id) ⇒ Object
18 19 20 |
# File 'lib/lt/google/api/drive.rb', line 18 def file_url_for(file_id) "https://drive.google.com/open?id=#{file_id}" end |
.folder_id_for(url) ⇒ Object
22 23 24 |
# File 'lib/lt/google/api/drive.rb', line 22 def folder_id_for(url) url.match(FOLDER_RE)[2] end |
Instance Method Details
#copy(file_ids, folder_id) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lt/google/api/drive.rb', line 32 def copy(file_ids, folder_id) file_ids.each do |id| service.get_file(id, fields: 'name') do |f, err| if err.present? Rails.logger.error "Failed to get file with #{id}, #{err.}" else service.copy_file(id, Google::Apis::DriveV3::File.new(name: f.name, parents: [folder_id])) end end end folder_id end |
#copy_files(folder_id, target_id) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/lt/google/api/drive.rb', line 45 def copy_files(folder_id, target_id) new_files = list folder_id current_files = list target_id # delete old files not present on new version current_files.each do |file| next if new_files.detect { |f| f.name == file.name } service.delete_file(file.id) end new_files.each do |file| # skip if the file already exists next if current_files.detect { |f| f.name == file.name } # copy if it's a new file new_file = Google::Apis::DriveV3::File.new(name: file.name, parents: [target_id]) service.copy_file(file.id, new_file) end end |
#create_folder(name, parent_id = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lt/google/api/drive.rb', line 66 def create_folder(name, parent_id = nil) if parent_id.present? && (folders = fetch_folders(name, parent_id)).any? return folders.first.id end = ::Google::Apis::DriveV3::File.new( name: name, mime_type: MIME_FOLDER, parents: [parent_id] ) service.create_file().id end |
#fetch_folders(name, folder_id) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/lt/google/api/drive.rb', line 103 def fetch_folders(name, folder_id) service.list_files( q: "'#{folder_id}' in parents and name = '#{name}' and mimeType = '#{MIME_FOLDER}' and trashed = false", fields: 'files(id)' ).files end |
#list_file_ids_in(folder_id, mime_type: MIME_FILE, with_subfolders: true) ⇒ Object
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/lt/google/api/drive.rb', line 79 def list_file_ids_in(folder_id, mime_type: MIME_FILE, with_subfolders: true) [].tap do |result| page_token = nil loop do response = service.list_files( q: "'#{folder_id}' in parents", fields: 'files(id, mime_type), nextPageToken', page_token: page_token ) response.files.each do |f| case f.mime_type when mime_type then result << f.id when MIME_FOLDER result.concat(list_file_ids_in f.id, mime_type: mime_type) if with_subfolders end end page_token = response.next_page_token break if page_token.nil? end end.flatten end |