-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
executable file
·380 lines (298 loc) · 9.89 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
69
70
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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#! /usr/bin/env python2
"""Gdrive/Flask/Zappa"""
from os import environ as env, remove, path
from operator import eq, ne
from tempfile import gettempdir
from traceback import format_exc
import urlparse
from werkzeug.utils import secure_filename
from flask import Flask, request, jsonify, Response
from pydrive.auth import GoogleAuth, AuthError
from pydrive.drive import GoogleDrive
from pydrive.files import ApiRequestError
from pydrive.settings import InvalidConfigError
from middleware import list_routes
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = gettempdir()
def allowed_extensions():
# type: () -> dict
"""Returns extension to mimetype mapping for allowed extensions.
Args:
None
Returns:
{extension: mimetype}
"""
return dict(
csv='text/csv',
doc='application/msword',
docx='application/vnd.openxmlformats-officedocument'
'.wordprocessingml.document',
json='application/json',
pdf='application/pdf',
png='image/png',
ppt='application/vnd.ms-powerpoint',
pptx='application/vnd.openxmlformats-officedocument'
'.presentationml.presentation',
svg='image/svg+xml',
txt='text/plain',
xls='application/vnd.ms-excel',
xlsx='application/vnd.openxmlformats-officedocument'
'.spreadsheetml.sheet',
zip='application/zip',
)
def init_auth():
# type: -> GoogleDrive
"""initialize auth when needed
Args:
None
Returns:
GoogleDrive object
"""
try:
gauth = GoogleAuth(
settings_file=(
'settings-%(stage)s.yaml' % dict(stage=env.get('STAGE')))
)
except AuthError as err:
raise(err)
except InvalidConfigError as err:
raise(err)
except Exception as err:
raise(err)
try:
gauth.ServiceAuth()
except AuthError as err:
raise(err)
except InvalidConfigError as err:
raise(err)
return GoogleDrive(gauth)
@app.errorhandler(Exception)
def exception_handler(error):
# type: (Exception) -> Exception
"""Show uncaught exceptions.
Args:
error
Raises:
Exception
"""
raise Exception(format_exc())
@app.route('/gdrive')
def list_api_routes():
"""List all endpoints
Args:
None
Returns:
json list of endpoints.
"""
return jsonify(list_routes(app))
def parse_url(url):
# type: (str) -> str
"""Return the 3rd part of the url or get id param if it exists.
Args:
url: google drive url
Returns:
url id
"""
parsed = urlparse.urlparse(url)
queries = urlparse.parse_qs(parsed.query)
path = parsed.path.split('/')
return queries['id'][0] if 'id' in queries else path[3]
@app.route('/gdrive/metadata')
def get_file_metadata():
# type: () -> dict
"""Get all metadata on a file or folder.
Args:
url <str> # Gdrive url link
Returns:
List(List()) # json list of lists.
"""
drive = init_auth()
url = request.args.get('url')
parsed_id = parse_url(url)
ifile = drive.CreateFile(dict(id=parsed_id))
print(ifile['mimeType'])
return jsonify(ifile.items())
def yield_bytes(data):
# type: (file) -> file
"""yield bytes to the caller for the response object.
There is a hard limit of 5MB of data return so the results are
streamed to the user.
Args:
data is a file object that you want streamed.
Yields:
one megabyte of data at a time.
"""
one_megabyte = 1024 * 1024
while True:
data_bytes = data.read(one_megabyte)
if not data_bytes:
break
yield data_bytes
@app.route('/gdrive/read', methods=['GET'])
def read_file():
# type: () -> file
"""Given a URL or ID of a URL, return the file.
Args:
url: # gdrive url link
Returns:
fileobject: binary representation of mimetype
"""
drive = init_auth()
url = request.args.get('url')
parsed_id = parse_url(url)
google_app_mimetypes = {
'application/vnd.google-apps.document': 'application/vnd'
'.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.google-apps.presentation': 'application/vnd'
'.openxmlformats-officedocument.presentationml.presentation',
'application/vnd.google-apps.spreadsheet': 'application/vnd'
'.openxmlformats-officedocument.spreadsheetml.sheet',
}
ifile = drive.CreateFile(dict(id=parsed_id))
mimetype = ifile['mimeType']
title = ifile['title']
tmp_file = '{}/{}'.format(gettempdir(), title)
if mimetype in (google_app_mimetypes):
mimetype = google_app_mimetypes[mimetype]
ifile.GetContentFile(tmp_file, mimetype=mimetype)
data = open(tmp_file, 'rb')
remove(tmp_file)
return Response(yield_bytes(data), mimetype=mimetype)
def create_folder(drive, parent_folder_id, folder_name):
# type: (GoogleDrive, str, str) -> str
"""Create a folder at the given folder id location with a folder name.
Args:
drive: Auth object.
parent_folder_id: folder location you would like this folder created.
folder_name: name of the folder.
Returns:
folder_id: of this newly created folder.
"""
try:
folder = drive.CreateFile(dict(
title=folder_name,
parents=[dict(id=parent_folder_id)],
mimeType="application/vnd.google-apps.folder"
))
except:
raise('problem creating folder')
try:
folder.Upload()
except ApiRequestError:
pass
return folder['id']
def list_file_object(drive, folder_id, directory_only=False):
# type: (GoogleDrive, str, bool) -> dict
"""List all files in a given folder id.
Args:
drive: Auth object.
folder_id: folder id to begin listing.
directory_only: Whether to list only directories or not.
Returns:
dict(id:file_id, title:file_title)
"""
_q = {'q': "'{}' in parents and trashed=false".format(folder_id)}
file_object_list = drive.ListFile(_q).GetList()
op = {True: eq, False: ne}[directory_only]
file_objects = [
x for x in file_object_list
if op(x['mimeType'], 'application/vnd.google-apps.folder')
]
return [{"id": fld["id"], "title": fld["title"]} for fld in file_objects]
def validate_file_name(file_name):
# type: (str) -> dict
"""Validate file name and return file in pieces.
Args:
file_name: Full filename + extension.
Returns:
dict(file_name: file_name, folder: folder, ext=ext)
"""
if not file_name:
raise('no file provided')
folder, ext = (
file_name.split('.')[:-1][0],
file_name.split('.')[-1].lower()
)
if not folder:
raise('not a valid folder')
if ext not in allowed_extensions():
raise('%s not a valid file format' % ext)
return dict(file_name=file_name, folder=folder, ext=ext)
def create_file(drive, folder_id, file_name, ext, to_gapp=False):
# type: (GoogleDrive, str, str, str, bool) -> str
"""Creates a file in Google Drive.
Args:
drive: auth object
folder_id: folder where you want this file created.
file_name: name of file.
ext: valid extension in allowed_extensions().
to_gapp: Convert to google app for csv, xlsx, xls, docx, pptx.
Default False.
Returns:
Alternate Link to file.
"""
try:
ifile = drive.CreateFile(dict(
title=file_name,
parents=[dict(id=folder_id)],
mimetype=allowed_extensions()[ext]))
except:
raise("Couldn't create %s" % file_name)
ifile.SetContentFile(path.join(app.config['UPLOAD_FOLDER'], file_name))
google_app_extensions = ('csv', 'xls', 'xlsx', 'docx', 'pptx')
convert_to_google_app = (to_gapp and ext in google_app_extensions)
ifile.Upload(dict(convert=convert_to_google_app))
return ifile['alternateLink']
@app.route('/gdrive/write', methods=['POST'])
def write_file():
# type: () -> str
"""Given file.ext, create file/file.ext.
Make sure to set your .env for GDRIVE_PARENT_FOLDER_ID.
This is a folder of last resort if the user doesn't share
the file or folder with your service account email. It also
allows you to control who gets access to your files.
Args:
file <tuple(file_name:str, file_name:file)>:
File must exist on your machine.
folder_id <str>: Optional. Defaults to GDRIVE_PARENT_FOLDER_ID.
Returns:
Alternate Link str # gdrive alternate link to preview the file.
"""
drive = init_auth()
ifile = request.files['file']
file_metadata = validate_file_name(ifile.filename)
args = request.form
folder = file_metadata['folder']
parent_folder_id = args.get(
'folder_id', env.get('GDRIVE_PARENT_FOLDER_ID')
)
to_gapp = args.get('to_gapp', False)
file_name = path.join(app.config['UPLOAD_FOLDER'],
secure_filename(file_metadata['file_name']))
ifile.save(file_name)
ext = file_metadata['ext']
folder_list = list_file_object(
drive, parent_folder_id, directory_only=True)
match = [x for x in folder_list if x['title'] == folder]
folder_id = (
match[0]['id'] if match else
create_folder(drive, parent_folder_id, folder)
)
file_list = list_file_object(drive, folder_id)
match = [x for x in file_list if x['title'] == file_name]
if match:
file_id = match[0]['id']
to_delete = drive.CreateFile({"id": file_id})
to_delete.Delete()
url = create_file(
drive=drive,
folder_id=folder_id,
file_name=file_metadata['file_name'],
ext=ext,
to_gapp=to_gapp,
)
remove(file_name)
return jsonify(url)
if __name__ == '__main__':
DEBUG = False if env['STAGE'] == 'prod' else True
app.run(debug=DEBUG, port=5000)