Class: EasyGoogleDrive::Drive
- Inherits:
-
Object
- Object
- EasyGoogleDrive::Drive
- Defined in:
- lib/easy-google-drive/file_api.rb,
lib/easy-google-drive/file_base.rb
Direct Known Subclasses
Constant Summary collapse
- OOB_URI =
'urn:ietf:wg:oauth:2.0:oob'- APPLICATION_NAME =
'Google Apps Script Execution API Ruby Quickstart'- CLIENT_SECRETS_PATH =
'client_secret.json'- CREDENTIALS_PATH =
File.join(Dir.home, '.credentials', "script-ruby-quickstart.yaml")
- SCOPE =
'https://www.googleapis.com/auth/drive'
Instance Method Summary collapse
- #authorize ⇒ Object
- #cd(target) ⇒ Object
- #directory(target, path, list) ⇒ Object
- #get(src, dst) ⇒ Object
- #get_folderid(target, file_list) ⇒ Object
- #help ⇒ Object
-
#initialize ⇒ Drive
constructor
A new instance of Drive.
- #list(path, file_list) ⇒ Object
- #list_files(target, path, list) ⇒ Object
- #ls(path = nil) ⇒ Object
- #mkdir(name) ⇒ Object
- #pwd ⇒ Object
- #rm(fname) ⇒ Object
- #rmdir(name) ⇒ Object
- #root(path, file_list) ⇒ Object
- #send(src, dst) ⇒ Object
- #shared(path, list) ⇒ Object
Constructor Details
#initialize ⇒ Drive
Returns a new instance of Drive.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/easy-google-drive/file_base.rb', line 36 def initialize @drive = Google::Apis::DriveV3 @service = @drive::DriveService.new @service..application_name = APPLICATION_NAME @service. = @current_file_list=[] @root_path=[] root(@root_path,@current_file_list) return end |
Instance Method Details
#authorize ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/easy-google-drive/file_base.rb', line 49 def FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH)) client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH) token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH) = Google::Auth::UserAuthorizer.new( client_id, SCOPE, token_store) user_id = 'default' credentials = .get_credentials(user_id) if credentials.nil? url = .( base_url: OOB_URI) puts "Open the following URL in the browser and enter the " + "resulting code after authorization" puts url code = gets credentials = .get_and_store_credentials_from_code( user_id: user_id, code: code, base_url: OOB_URI) end credentials end |
#cd(target) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/easy-google-drive/file_api.rb', line 38 def cd(target) path_split = target.split("/") path_split.each do |folder| if directory(folder,@root_path,@current_file_list) == false return false end end puts "success to change directory:: " pwd() end |
#directory(target, path, list) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/easy-google-drive/file_base.rb', line 211 def directory(target,path,list) if target == ".." then if(path.length > 1) then path.pop() list(path,list) else puts "root folder" return false end elsif target == "~" then root(path,list) elsif target == "$" shared(path,list) elsif target == "." list(path,list) else if list == [] then list(path,list) end newfolder = get_folderid(target,list) if newfolder != nil then path.push({name:newfolder.name,id:newfolder.id}) list(path,list) else puts "folder not find:: "+target return false end end return true end |
#get(src, dst) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/easy-google-drive/file_api.rb', line 3 def get(src,dst) if dst == "" then puts "please enter dst file name" end tmp_list = [] tmp_path = [] tmp_path.push(@root_path.last) target_list = list_files(src,tmp_path,tmp_list) if target_list == [] then puts "cannot file file" else target_list.each do |file| if file.mime_type != 'application/vnd.google-apps.folder' then @service.get_file(file.id,{download_dest: dst}) puts "success to get file::" + file.name + "," + file.id return end end end end |
#get_folderid(target, file_list) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/easy-google-drive/file_base.rb', line 159 def get_folderid(target,file_list) file_list.each do |file| if file.mime_type == "application/vnd.google-apps.folder" then if file.name == target then return file end end end return nil end |
#help ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/easy-google-drive/file_api.rb', line 160 def help puts "EasyGoogleDrive help" puts "" puts "EasyGoogleDrive::File.cd(""directory"")" puts "EasyGoogleDrive::File.cd(""~"")" puts " move to root folder" puts "EasyGoogleDrive::File.cd(""$"")" puts " move to shared folder" puts "EasyGoogleDrive::File.cd("".."")" puts " move to upper folder" puts "" puts "EasyGoogleDrive::File.ls()" puts " list file and folder in current folder" puts "" puts "EasyGoogleDrive::File.get(""src"",""dst"")" puts " copy src file in google drive to dst in local file" puts " src: source file name in gllgle drive" puts " dst: destination file name in local file" puts "" puts "EasyGoogleDrive::File.send(""src"",""dst"")" puts " copy src file in local drive to dst in google drive" puts " src: source file name in local drive" puts " dst: destination file name in google file" puts "" end |
#list(path, file_list) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/easy-google-drive/file_base.rb', line 191 def list(path,file_list) current_folder = path.last qmsg = sprintf("""'%s' in parents and trashed=false""",current_folder[:id]) file_list.clear page_token = nil begin response = @service.list_files( q: qmsg, spaces: 'drive', fields: "nextPageToken, files(id, name, parents,kind,trashed, mimeType)", page_token: page_token) for file in response.files # Process change file_list.push(file) end page_token = response.next_page_token end while !page_token.nil? return end |
#list_files(target, path, list) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/easy-google-drive/file_base.rb', line 115 def list_files(target,path,list) if target == nil then list.clear @current_file_list.each do |file| list.push(file) end return list end target_split = target.split("/") if target_split == nil then if target == "~" or target == "$" then directory(target,path,list) return else list.clear @current_file_list.each do |file| if file.name == target then list.push(file) end end end else target_file = target_split.last target_split.pop() end tmp_list = [] if target_split == [] then list(path,tmp_list) else target_split.each do |folder| if directory(folder,path,tmp_list) == false then return false end end end list.clear tmp_list.each do |file| if file.name == target_file or target_file == "*" then list.push(file) end end return list end |
#ls(path = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/easy-google-drive/file_api.rb', line 49 def ls(path=nil) tmp_list = [] tmp_path = [] tmp_path.push(@root_path.last) list = list_files(path,tmp_path,tmp_list) if list == false or list == [] then puts "file not found" else # when target is folder, display inside of target if list.length == 1 and list[0].mime_type == "application/vnd.google-apps.folder" then directory(list[0].name, tmp_path, list) end list.each do |file| if file.mime_type == "application/vnd.google-apps.folder" then puts TermColor.parse("<blue>"+file.name+"</blue>") else puts TermColor.parse(file.name) end end end end |
#mkdir(name) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/easy-google-drive/file_api.rb', line 25 def mkdir(name) parents = [] parents.push(@root_path.last[:id]) = { name: name, parents: parents, mime_type: 'application/vnd.google-apps.folder', } @service.create_file(, fields: 'id') puts "success to create folder:: " + name list(@root_path,@current_file_list) end |
#pwd ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/easy-google-drive/file_api.rb', line 147 def pwd() path = "" @root_path.each do |folder| if folder[:name] == "root" then path = "~" elsif folder[:name] == "shared" path = "$" else path = path + "/" + folder[:name] end end puts path end |
#rm(fname) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/easy-google-drive/file_api.rb', line 96 def rm(fname) tmp_list = [] tmp_path = [] tmp_path.push(@root_path.last) list = list_files(fname,tmp_path,tmp_list) list.each do |file| if file.mime_type != "application/vnd.google-apps.folder" then @service.delete_file(file.id) puts "success to remove file:: " + file.name + ","+ file.id end end list(@root_path,@current_file_list) end |
#rmdir(name) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/easy-google-drive/file_api.rb', line 71 def rmdir(name) list = [] tmp_path = [] tmp_path.push(@root_path.last) list_files(name,tmp_path,list) # filtering only folder list.each do |file| if file.mime_type != "application/vnd.google-apps.folder" then list.delete(file) end end # check inside of folder list.each do |folder| list2=[] tmp_path2 = [] tmp_path2.push({name:folder.name,id:folder.id}) list(tmp_path2,list2) if list2 == [] then @service.delete_file(folder[:id]) puts "success to remove folder:: "+folder[:id] else puts "cannot remove folder:: "+folder[:id] end end end |
#root(path, file_list) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/easy-google-drive/file_base.rb', line 71 def root(path,file_list) # file list page_token = nil ref_file = [] begin response = @service.list_files( q: "name='gdrive.dat' and trashed = false", spaces: 'drive', fields: "nextPageToken, files(id, name, parents,kind,mimeType)", page_token: page_token) for file in response.files ref_file.push(file) end page_token = response.next_page_token end while !page_token.nil? if ref_file != [] then current_folder_id = ref_file[0].parents else puts "ref file not found" = Google::Apis::DriveV3::File.new( name: "gdrive.dat", mine_type: 'application/vnd.google-apps.unknown', ) @service.create_file(, fields:'id') begin response = @service.list_files( q: "name='gdrive.dat'", spaces: 'drive', fields: "nextPageToken, files(id, name, parents,kind,mimeType)", page_token: page_token) for file in response.files ref_file.push(file) end page_token = response.next_page_token end while !page_token.nil? current_folder_id = ref_file[0].parents end path.clear path.push({name:"root",id:current_folder_id[0]}) list(path,file_list) return end |
#send(src, dst) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/easy-google-drive/file_api.rb', line 110 def send(src,dst) # change directory tmp_list = [] tmp_path = [] tmp_path.push(@root_path.last) list = list_files(dst,tmp_path,tmp_list) if list == [] then target = dst.split("/").last elsif list.length ==1 and list[0].mime_type == "application/vnd.google-apps.folder" then directory(list[0].name,tmp_path,tmp_list) target = src.split("/").last else puts "cannot send file" end # check last path is directory or filename parents = [] parents.push(tmp_path.last[:id]) = Google::Apis::DriveV3::File.new( name: target, mine_type: 'application/vnd.google-apps.unknown', parents: parents, ) @service.create_file(, upload_source: src, fields: 'id') msg = "success to send file:: " tmp_path.each do |folder| if folder[:name] == "root" then msg = msg +"~/" elsif folder[:name] == "shared" then msg = msg + "$/" else msg = msg + folder[:name] +"/" end end msg += target puts msg end |
#shared(path, list) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/easy-google-drive/file_base.rb', line 170 def shared(path,list) path.clear list.clear page_token = nil begin response = @service.list_files( q: "trashed = false", spaces: 'drive', fields: "nextPageToken, files(id, name, parents,kind,mimeType)", page_token: page_token) for file in response.files if file.parents == nil then list.push(file) end end page_token = response.next_page_token end while !page_token.nil? path.push({name:"shared",id:nil}) return end |