class EasyGoogleDrive::Drive

Constants

APPLICATION_NAME
CLIENT_SECRETS_PATH
CREDENTIALS_PATH
OOB_URI
SCOPE

Public Class Methods

new() click to toggle source
# File lib/easy-google-drive/file_base.rb, line 36
def initialize
        @drive   = Google::Apis::DriveV3
        @service = @drive::DriveService.new
        @service.client_options.application_name = APPLICATION_NAME
        @service.authorization = authorize


        @current_file_list=[]
        @root_path=[]
        root(@root_path,@current_file_list)

        return
end

Public Instance Methods

authorize() click to toggle source
# File lib/easy-google-drive/file_base.rb, line 49
def authorize
        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)
        authorizer = Google::Auth::UserAuthorizer.new(
        client_id, SCOPE, token_store)
        user_id = 'default'
        credentials = authorizer.get_credentials(user_id)
        if credentials.nil?
                url = authorizer.get_authorization_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 = authorizer.get_and_store_credentials_from_code(
                        user_id: user_id, code: code, base_url: OOB_URI)
        end
        credentials
end
cd(target) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/easy-google-drive/file_api.rb, line 25
def mkdir(name)
        parents = []
        parents.push(@root_path.last[:id])
        file_metadata = {
                name: name,
               parents: parents,
               mime_type: 'application/vnd.google-apps.folder',
                }
        @service.create_file(file_metadata, fields: 'id')
        puts "success to create folder:: " + name
        list(@root_path,@current_file_list)
end
pwd() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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"
                file_metadata = Google::Apis::DriveV3::File.new(
                        name: "gdrive.dat",
                        mine_type: 'application/vnd.google-apps.unknown',
                        )
                @service.create_file(file_metadata, 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) click to toggle source
# 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])
        file_metadata = Google::Apis::DriveV3::File.new(
                name: target,
                mine_type: 'application/vnd.google-apps.unknown',
                parents: parents,
                )
        @service.create_file(file_metadata, 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) click to toggle source
# 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