forked from siame/flask-gae_gcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_gae_gcs.py
362 lines (303 loc) · 11.3 KB
/
flask_gae_gcs.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
"""
flask_gae_gcs
~~~~~~~~~~~~~~~~~~~
Flask extension for working with Google Cloud Storage on
Google App Engine.
:license: BSD, see LICENSE for more details.
"""
import re
import uuid
import string
import random
import logging
import os
from cgi import parse_header
from StringIO import StringIO
import cloudstorage as gcs
from flask import Response, request
from werkzeug.datastructures import FileStorage
from functools import wraps
from google.appengine.api import app_identity
__all__ = [
'WRITE_MAX_RETRIES', 'WRITE_SLEEP_SECONDS', 'DEFAULT_NAME_LEN',
'MSG_INVALID_FILE_POSTED', 'UPLOAD_MIN_FILE_SIZE', 'UPLOAD_MAX_FILE_SIZE',
'UPLOAD_ACCEPT_FILE_TYPES', 'ORIGINS', 'OPTIONS', 'HEADERS', 'MIMETYPE',
'RemoteResponse', 'FileUploadResultSet', 'FileUploadResult',
'upload_files', 'save_files', 'write_to_gcs']
#:
WRITE_MAX_RETRIES = 3
#:
WRITE_SLEEP_SECONDS = 0.05
#:
DEFAULT_NAME_LEN = 20
#:
MSG_INVALID_FILE_POSTED = 'Invalid file posted.'
#:
UPLOAD_MIN_FILE_SIZE = 1
#:
UPLOAD_MAX_FILE_SIZE = 1024 * 1024
# set by default to images..
#:
UPLOAD_ACCEPT_FILE_TYPES = re.compile('image/(gif|p?jpeg|jpg|(x-)?png|tiff)')
# todo: need a way to easily configure these values..
#:
ORIGINS = '*'
#:
OPTIONS = ['OPTIONS', 'HEAD', 'GET', 'POST', 'PUT']
#:
HEADERS = ['Accept', 'Content-Type', 'Origin', 'X-Requested-With']
#:
MIMETYPE = 'application/json'
class RemoteResponse(Response):
'''Base class for remote service `Response` objects.
:param response:
:param mimetype:
'''
default_mimetype = MIMETYPE
def __init__(self, response=None, mimetype=None, *args, **kw):
if mimetype is None:
mimetype = self.default_mimetype
Response.__init__(self, response=response, mimetype=mimetype, **kw)
self._fixcors()
def _fixcors(self):
self.headers['Access-Control-Allow-Origin'] = ORIGINS
self.headers['Access-Control-Allow-Methods'] = ', '.join(OPTIONS)
self.headers['Access-Control-Allow-Headers'] = ', '.join(HEADERS)
class FileUploadResultSet(list):
def to_dict(self):
'''
:returns: List of `FileUploadResult` as `dict`s.
'''
result = []
for field in self:
result.append(field.to_dict())
return result
class FileUploadResult:
'''
:param successful:
:param error_msg:
:param uuid:
:param name:
:param type:
:param size:
:param field:
:param value:
'''
def __init__(self, name, type, size, field, value, bucket_name):
self.successful = False
self.error_msg = ''
self.uuid = None
self.name = name
self.type = type
self.size = size
self.field = field
self.value = value
self.bucket_name = None
@property
def file_info(self):
return gcs.stat(get_gcs_filename(self.uuid, self.bucket_name))
def to_dict(self):
'''
:returns: Instance of a dict.
'''
return {
'successful': self.successful,
'error_msg': self.error_msg,
'uuid': str(self.uuid),
'name': self.name,
'type': self.type,
'size': self.size
}
def get_gcs_filename(filename, bucket_name=None):
if bucket_name:
return '/' + bucket_name + '/' + filename
return '/' + app_identity.get_default_gcs_bucket_name() + '/' + filename
def upload_files(validators=None, retry_params=None, bucket_name=None):
'''Method decorator for writing posted files to Google Cloud Storage using
the App Engine CloudStorage api. Passes an argument to the method with a
list of `FileUploadResult` with UUID, name, type, size for each posted
input file.
:param validators: List of callable objects.
:param retry_params: `RetryParams` object from `cloudstorage`
:param bucket_name: String of custom bucket name.
'''
def wrapper(fn):
@wraps(fn)
def decorated(*args, **kw):
return fn(
uploads=save_files(
fields=_upload_fields(),
validators=validators,
retry_params=retry_params,
bucket_name=bucket_name
),
*args, **kw
)
return decorated
return wrapper
def save_files(fields, validators=None, retry_params=None, bucket_name=None):
'''Returns a list of `FileUploadResult` with UUID, name, type, size for
each posted file.
:param fields: List of `werkzeug.datastructures.FileStorage` objects.
:param validators: List of functions, usually one of validate_min_size,
validate_file_type, validate_max_size included here.
By default validate_min_size is included to make sure
the file is not empty (see UPLOAD_MIN_FILE_SIZE).
:param retry_params: `RetryParams` object from `cloudstorage`
:param bucket_name: String of custom bucket name.
:returns: Instance of a `FileUploadResultSet`.
'''
if validators is None:
validators = [
validate_min_size
]
results = FileUploadResultSet()
for name, field in fields:
value = field.stream.read()
filename = re.sub(r'^.*\\', '', field.filename)
result = FileUploadResult(
name=filename,
type=field.mimetype,
size=len(value),
field=field,
value=value,
bucket_name=bucket_name if bucket_name else None)
if validators:
for fn in validators:
if not fn(result):
result.successful = False
result.error_msg = MSG_INVALID_FILE_POSTED
logging.warn('Error in file upload: %s', result.error_msg)
else:
result.uuid = write_to_gcs(
result.value, mime_type=result.type, name=result.name,
retry_params=retry_params, bucket_name=bucket_name)
if result.uuid:
result.successful = True
else:
result.successful = False
results.append(result)
else:
result.uuid = write_to_gcs(
result.value, mime_type=result.type, name=result.name,
retry_params=retry_params, bucket_name=bucket_name)
logging.error('result.uuid: %s', result.uuid)
if result.uuid:
result.successful = True
else:
result.successful = False
results.append(result)
return results
def _upload_fields():
'''Gets a list of files from the request.
Uses Flask's request.files to get all files, unless the Content-Type is
`text/csv` or `text/plain`: then returns the request body as a FileStorage
object, attempting to use Content-Disposition to get a file name.
:returns: List of tuples of:
(field_name, `werkzeug.datastructures.FileStorage`)
'''
result = []
content_type = request.headers.get('content-type')
mime_type, _ = parse_header(content_type)
# special case for application/xml; name="Sample 9.16.16.csv"
is_app_xml_sap = "application/xml" in content_type
if mime_type in ('text/plain', 'text/csv') or is_app_xml_sap:
_, params = parse_header(
request.headers.get('content-disposition', '')
)
filename = params.get('filename', 'noname.txt')
fileo = FileStorage(stream=StringIO(request.data),
filename=filename,
content_type=request.headers.get('content-type'))
result.append(('file', fileo))
else:
for key, value in request.files.iteritems():
if not isinstance(value, unicode):
result.append((key, value))
return result
def get_field_size(field):
'''
:param field: a file-like object, e.g. instance of
`werkzeug.datastructures.FileStorage`.
:returns: Integer.
'''
try:
field.seek(0, os.SEEK_END) # Seek to the end of the file
size = field.tell() # Get the position of EOF
field.seek(0) # Reset the file position to the beginning
return size
except:
return 0
def validate_max_size(result, max_file_size=UPLOAD_MAX_FILE_SIZE):
'''Validates an upload input based on maximum size.
:param result: Instance of `FileUploadResult`.
:param max_file_size: Integer.
:returns: Boolean, True if field validates.
'''
if result.size > max_file_size:
result.error_msg = 'max_file_size'
return False
return True
def validate_min_size(result, min_file_size=UPLOAD_MIN_FILE_SIZE):
'''Validates an upload input based on minimum size.
:param result: Instance of `FileUploadResult`.
:param min_file_size: Integer.
:returns: Boolean, True if field validates.
'''
if result.size < min_file_size:
result.error_msg = 'min_file_size'
return False
return True
def validate_file_type(result, accept_file_types=UPLOAD_ACCEPT_FILE_TYPES):
'''Validates an upload input based on accepted mime types.
If validation fails, sets an error property to the field arg dict.
:param result: Instance of `FileUploadResult`.
:param accept_file_types: Instance of a regex.
:returns: Boolean, True if field validates.
'''
if not accept_file_types.match(result.type):
result.field.error_msg = 'accept_file_types'
return False
return True
def write_to_gcs(data, mime_type, name=None, retry_params=None,
bucket_name=None, force_download=False):
'''Writes a file to Google Cloud Storage and returns the file name
if successful.
:param data: Data to be stored.
:param mime_type: String, mime type of the data.
:param name: String, name of the data.
:param retry_params: `RetryParams` object from `cloudstorage`
:param bucket_name: String of custom bucket name.
:param force_download: Boolean, whether or not file will be a forced
download
:returns: String, filename.
'''
if not name:
name = ''.join(random.choice(string.letters)
for x in range(DEFAULT_NAME_LEN))
new_uuid = str(uuid.uuid4())
bucket_filename = get_gcs_filename(new_uuid, bucket_name)
if retry_params:
default_retry_params = retry_params
else:
default_retry_params = gcs.RetryParams(initial_delay=0.2,
max_delay=5.0,
backoff_factor=2,
max_retry_period=15)
if isinstance(name, unicode):
name = name.encode('ascii', errors='replace')
options = {}
if name:
options.update({b'x-goog-meta-filename': name})
if force_download:
options.update({
b'Content-Disposition': 'attachment; filename={}'.format(name)
})
gcs_file = gcs.open(bucket_filename,
'w',
content_type=mime_type,
options=options,
retry_params=default_retry_params)
gcs_file.write(data)
gcs_file.close()
return new_uuid