Class: Googledriver::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/googledriver/uploader.rb

Overview

Uploads a filesystem to Google Drive and saves file ids.

Instance Method Summary collapse

Constructor Details

#initialize(client_secrets_path) ⇒ Uploader

Constructs a new Uploader by building an empty hash for file ids and making an Authorizer object to handle the creation of REST resources.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/googledriver/uploader.rb', line 7

def initialize(client_secrets_path)
  @file_ids = {}
  @authorizer = Googledriver::Authorizer.new(client_secrets_path)
  @authorizer.create_refresh_token
  @authorizer.refresh_access_token
  @access_token = @authorizer.access_token
  @token_tob = @authorizer.token_tob
  @token_lifetime = @authorizer.token_lifetime
  create_manager_resource
  create_uploader_resource
end

Instance Method Details

#archive_file_idsObject

Saves the file ids hash to a json file in the working directory.



110
111
112
113
114
# File 'lib/googledriver/uploader.rb', line 110

def archive_file_ids
  archive = File.open('drive_file_ids.json', 'w')
  File.write('drive_file_ids.json', @file_ids.to_json)
  archive.close
end

#obtain_file_metadata(file_id) ⇒ Object

Returns the metadata of a given file.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/googledriver/uploader.rb', line 117

def (file_id)
  begin
     = @drive_manager[file_id].get
  rescue StandardError => error
    warn "#{error};  METHOD  #{__callee__};  RESOURCE  #{file_path}"
    return
  end

   = JSON.parse()
  
end

#update_file_metadata(file_id, element, new_data) ⇒ Object

Updates a piece of metadata for a given file.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/googledriver/uploader.rb', line 130

def (file_id, element, new_data)
  payload = { 'uploadType' => 'resumable', element => new_data }.to_json

  begin
    update = @drive_manager[file_id].patch(
      payload
    )
  rescue StandardError => error
    warn "#{error};  METHOD  #{__callee__};  RESOURCE  #{file_path}"
    return
  end

  update
end

#update_file_permission(file_id, email) ⇒ Object

Shares a given file with an individual or group email address.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/googledriver/uploader.rb', line 146

def update_file_permission(file_id, email)
  payload = { 'role' => 'writer', 'type' => 'group',
              'emailAddress' => email }.to_json

  begin
    update = @drive_manager[file_id + '/permissions'].post(
      payload
    )
  rescue StandardError => error
    warn "#{error};  METHOD  #{__callee__};  RESOURCE  #{file_path}"
    return
  end

  update
end

#upload_file(file_path, file_name, location: 'root') ⇒ Object

Uploads a local file specified by a file path to a given folder in Google Drive.



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
# File 'lib/googledriver/uploader.rb', line 72

def upload_file(file_path, file_name, location: 'root')
  begin
    payload = File.open(file_path)
  rescue StandardError => error
    warn "#{error};  METHOD  #{__callee__};  RESOURCE  #{file_path}"
    return
  end

  begin
    update_refresh_token if refresh_due?
    upload = @drive_uploader.post(
      payload
    )
  rescue StandardError => error
    warn "#{error};  METHOD  #{__callee__};  RESOURCE  #{file_path}"
    retry
  end

  file_id = JSON.parse(upload)['id']
  @file_ids[file_path] = file_id

  begin
    update_refresh_token if refresh_due?
    @drive_manager[file_id + '?addParents=' + location +
                   '&removeParents=root&alt=json'].patch(
                     { 'uploadType' => 'resumable',
                       'name' => file_name }.to_json
                   )
  rescue StandardError => error
    warn "#{error};  METHOD  #{__callee__};  RESOURCE  #{file_path}"
    retry
  end

  payload.close
  file_id
end

#upload_filesystem(directory: '', upload_dest: 'root') ⇒ Object

Recursively uploads a local filesystem specified by a file path to a given folder in Google Drive.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/googledriver/uploader.rb', line 21

def upload_filesystem(directory: '', upload_dest: 'root')
  directory = "#{directory}/" unless directory[-1] == '/'

  Dir[directory + '*'].each do |object|
    if File.directory?(object)
      folder_name = File.basename(object)
      folder_id = upload_folder(folder_name, location: upload_dest)
      upload_filesystem(upload_dest: folder_id,
                        directory: "#{directory}#{folder_name}/")
    else
      file_name = File.basename(object)
      file_id = upload_file(object, file_name, location: upload_dest)
      @file_ids[object] = file_id
    end
  end
end

#upload_folder(folder_name, location: 'root') ⇒ Object

Uploads a local folder specified by a file path to a given folder in Google Drive.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/googledriver/uploader.rb', line 40

def upload_folder(folder_name, location: 'root')
  begin
    update_refresh_token if refresh_due?
    upload = @drive_manager.post(
      { 'name' => folder_name,
        'mimeType' => 'application/vnd.google-apps.folder' }.to_json
    )
  rescue StandardError => error
    warn "#{error};  METHOD  #{__callee__};  RESOURCE  #{folder_name}"
    retry
  end

  folder_id = JSON.parse(upload)['id']

  if location != 'root'
    begin
      update_refresh_token if refresh_due?
      @drive_manager[folder_id + '?addParents=' + location +
                     '&removeParents=root&alt=json'].patch(
                       { 'uploadType' => 'resumable' }.to_json
                     )
    rescue StandardError => error
      warn "#{error};  METHOD  #{__callee__};  RESOURCE  #{folder_name}"
      retry
    end
  end

  folder_id
end