Skip to content

Commit

Permalink
Add RETURN_MEDIA_AS_BASE64_STRING setting
Browse files Browse the repository at this point in the history
  • Loading branch information
amagdas committed May 27, 2014
1 parent 1377269 commit d5a4adf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions eve/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@

# use a simple file response format by default
EXTENDED_MEDIA_INFO = []
RETURN_MEDIA_AS_BASE64_STRING = True

# list of extra fields to be included with every POST response. This list
# should not include the 'standard' fields (ID_FIELD, LAST_UPDATED,
Expand Down
7 changes: 5 additions & 2 deletions eve/methods/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,18 @@ def resolve_media_files(document, resource):
_file = app.media.get(document[field])
# check if we should send a basic file response
if config.EXTENDED_MEDIA_INFO == []:
if _file:
if _file and config.RETURN_MEDIA_AS_BASE64_STRING:
document[field] = base64.encodestring(_file.read())
else:
document[field] = None
elif _file:
# otherwise we have a valid file and should send extended response
# start with the basic file object
ret_file = None
if config.RETURN_MEDIA_AS_BASE64_STRING:
ret_file = base64.encodestring(_file.read())
document[field] = {
'file': base64.encodestring(_file.read()),
'file': ret_file,
}

# check if we should return any special fields
Expand Down
53 changes: 53 additions & 0 deletions eve/tests/io/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@ def test_gridfs_media_storage_post(self):
# which decodes to the original clean
self.assertEqual(base64.decodestring(returned.encode()), self.clean)

def test_gridfs_media_storage_post_excluded_file_in_result(self):
# send something different than a file and get an error back
data = {'media': 'not a file'}
r, s = self.parse_response(
self.test_client.post(self.url, data=data, headers=self.headers))
self.assertEqual(STATUS_ERR, r[STATUS])

# validates media fields
self.assertTrue('file was expected' in r[ISSUES]['media'])
# also validates ordinary fields
self.assertTrue('required' in r[ISSUES][self.test_field])

r, s = self._post()
self.assertEqual(STATUS_OK, r[STATUS])

self.app.config['RETURN_MEDIA_AS_BASE64_STRING'] = False
# compare original and returned data
_id = r[ID_FIELD]

# GET the file at the resource endpoint
where = 'where={"%s": "%s"}' % (ID_FIELD, _id)
r, s = self.parse_response(
self.test_client.get('%s?%s' % (self.url, where)))
self.assertEqual(len(r['_items']), 1)
returned = r['_items'][0]['media']

# returned value is a base64 encoded string
self.assertEqual(returned, None)

def test_gridfs_media_storage_post_extended(self):
r, s = self._post()
self.assertEqual(STATUS_OK, r[STATUS])
Expand Down Expand Up @@ -99,6 +128,30 @@ def test_gridfs_media_storage_post_extended(self):
self.assertEqual(returned['content_type'], 'text/plain')
self.assertEqual(returned['length'], 16)

def test_gridfs_media_storage_post_extended_excluded_file_in_result(self):
r, s = self._post()
self.assertEqual(STATUS_OK, r[STATUS])

# request extended format file response
self.app.config['EXTENDED_MEDIA_INFO'] = ['content_type', 'length']
self.app.config['RETURN_MEDIA_AS_BASE64_STRING'] = False
# compare original and returned data
_id = r[ID_FIELD]

# GET the file at the resource endpoint
where = 'where={"%s": "%s"}' % (ID_FIELD, _id)
r, s = self.parse_response(
self.test_client.get('%s?%s' % (self.url, where)))
self.assertEqual(len(r['_items']), 1)
returned = r['_items'][0]['media']

# returned value is None
self.assertEqual(returned['file'], None)

# also verify our extended fields
self.assertEqual(returned['content_type'], 'text/plain')
self.assertEqual(returned['length'], 16)

def test_gridfs_media_storage_put(self):
r, s = self._post()
_id = r[ID_FIELD]
Expand Down

0 comments on commit d5a4adf

Please sign in to comment.