Class: Tumugi::Plugin::GoogleDrive::FileSystem
- Inherits:
-
Object
- Object
- Tumugi::Plugin::GoogleDrive::FileSystem
- Defined in:
- lib/tumugi/plugin/google_drive/file_system.rb
Constant Summary collapse
- MIME_TYPE_FOLDER =
'application/vnd.google-apps.folder'
Instance Method Summary collapse
- #copy(src_file_id, dest_name, dest_parents: nil) ⇒ Object
- #directory?(file_id) ⇒ Boolean
- #download(file_id, download_path: nil, mode: 'r', mime_type: nil, &block) ⇒ Object
- #exist?(file_id) ⇒ Boolean
- #generate_file_id ⇒ Object
- #get_file_metadata(file_id, fields: "*") ⇒ Object
- #google_drive_document?(mime_type) ⇒ Boolean
-
#initialize(config) ⇒ FileSystem
constructor
A new instance of FileSystem.
- #list_files(query:, order_by: "name", spaces: "drive", fields: "next_page_token, files(id, name, parents, mime_type)", page_size: 100, page_token: nil) ⇒ Object
- #mkdir(name, folder_id: nil, parents: nil) ⇒ Object
- #move(src_file_id, dest_name, dest_parents: nil) ⇒ Object
- #put_string(contents, name, content_type: 'text/plain', file_id: nil, parents: nil) ⇒ Object
- #remove(file_id) ⇒ Object
- #upload(media, name, content_type: nil, file_id: nil, parents: nil, mime_type: nil) ⇒ Object
Constructor Details
#initialize(config) ⇒ FileSystem
Returns a new instance of FileSystem.
13 14 15 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 13 def initialize(config) save_config(config) end |
Instance Method Details
#copy(src_file_id, dest_name, dest_parents: nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 62 def copy(src_file_id, dest_name, dest_parents: nil) dest_parents = [dest_parents] if dest_parents.is_a?(String) = Google::Apis::DriveV3::File.new({ name: dest_name, parents: dest_parents }) file = client.copy_file(src_file_id, , options: ) wait_until { exist?(file.id) } file rescue process_error($!) end |
#directory?(file_id) ⇒ Boolean
49 50 51 52 53 54 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 49 def directory?(file_id) file = (file_id) file.mime_type == MIME_TYPE_FOLDER rescue process_error($!) end |
#download(file_id, download_path: nil, mode: 'r', mime_type: nil, &block) ⇒ Object
95 96 97 98 99 100 101 102 103 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 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 95 def download(file_id, download_path: nil, mode: 'r', mime_type: nil, &block) if !exist?(file_id) raise Tumugi::FileSystemError.new("#{file_id} does not exist") end if download_path.nil? download_path = Tempfile.new('tumugi_google_drive_file_system').path end file = (file_id) if google_drive_document?(file.mime_type) # If file is Google Drive Document, use export_file # https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents if mime_type.nil? raise Tumugi::FileSystemError.new("mime_type is required because file #{file_id} is Google Drive Document") end client.export_file(file_id, mime_type, download_dest: download_path, options: ) else # If file is not Google Drive Document, use get_file # https://developers.google.com/drive/v3/web/manage-downloads#downloading_a_file client.get_file(file_id, download_dest: download_path, options: ) end wait_until { File.exist?(download_path) } if block_given? File.open(download_path, mode, &block) else File.open(download_path, mode) end file rescue process_error($!) end |
#exist?(file_id) ⇒ Boolean
17 18 19 20 21 22 23 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 17 def exist?(file_id) (file_id, fields: 'id') true rescue => e return false if e.respond_to?(:status_code) && e.status_code == 404 process_error(e) end |
#generate_file_id ⇒ Object
130 131 132 133 134 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 130 def generate_file_id client.generate_file_ids(count: 1, options: ).ids.first rescue process_error($!) end |
#get_file_metadata(file_id, fields: "*") ⇒ Object
144 145 146 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 144 def (file_id, fields: "*") client.get_file(file_id, fields: fields, options: ) end |
#google_drive_document?(mime_type) ⇒ Boolean
148 149 150 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 148 def google_drive_document?(mime_type) !!(mime_type && mime_type.start_with?("application/vnd.google-apps.")) end |
#list_files(query:, order_by: "name", spaces: "drive", fields: "next_page_token, files(id, name, parents, mime_type)", page_size: 100, page_token: nil) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 136 def list_files(query:, order_by: "name", spaces: "drive", fields: "next_page_token, files(id, name, parents, mime_type)", page_size: 100, page_token: nil) # https://developers.google.com/drive/v3/reference/files/list # https://developers.google.com/drive/v3/web/search-parameters client.list_files(q: query, order_by: order_by, spaces: spaces, fields: fields, page_size: page_size, page_token: page_token) rescue process_error($!) end |
#mkdir(name, folder_id: nil, parents: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 35 def mkdir(name, folder_id: nil, parents: nil) = Google::Apis::DriveV3::File.new({ name: name, mime_type: MIME_TYPE_FOLDER, parents: parents, id: folder_id }) file = client.create_file(, fields: "*", options: ) wait_until { exist?(file.id) } file rescue process_error($!) end |
#move(src_file_id, dest_name, dest_parents: nil) ⇒ Object
56 57 58 59 60 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 56 def move(src_file_id, dest_name, dest_parents: nil) file = copy(src_file_id, dest_name, dest_parents: dest_parents) remove(src_file_id) file end |
#put_string(contents, name, content_type: 'text/plain', file_id: nil, parents: nil) ⇒ Object
90 91 92 93 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 90 def put_string(contents, name, content_type: 'text/plain', file_id: nil, parents: nil) media = StringIO.new(contents) upload(media, name, content_type: content_type, file_id: file_id, parents: parents) end |
#remove(file_id) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 25 def remove(file_id) return unless exist?(file_id) client.delete_file(file_id, options: ) wait_until { !exist?(file_id) } file_id rescue process_error($!) end |
#upload(media, name, content_type: nil, file_id: nil, parents: nil, mime_type: nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/tumugi/plugin/google_drive/file_system.rb', line 75 def upload(media, name, content_type: nil, file_id: nil, parents: nil, mime_type: nil) parents = [parents] if parents.is_a?(String) = Google::Apis::DriveV3::File.new({ id: file_id, name: name, parents: parents, mime_type: mime_type }) file = client.create_file(, fields: "*", upload_source: media, content_type: content_type, options: ) wait_until { exist?(file.id) } file rescue process_error($!) end |