Skip to content

Commit 07058be

Browse files
authored
fix: Avoid raising 404 when file cannot generate a thumbnail (#642)
1 parent 67991e3 commit 07058be

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Next release
1414

1515
**Bug Fixes:**
1616
- Limit cryptography to version <3.5.0 (`#636 <https://github.com/box/box-python-sdk/pull/636>`_)
17+
- Avoid raising 404 when a thumbnail cannot be generated for a file (`#642 <https://github.com/box/box-python-sdk/pull/642>`_)
1718

1819
2.13.0 (2021-09-30)
1920
++++++++

boxsdk/object/file.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,12 @@ def get_thumbnail_representation(self, dimensions, extension='png'):
791791
`bytes`
792792
"""
793793
rep_hints = '[{0}?dimensions={1}]'.format(extension, dimensions)
794-
representation = self.get_representation_info(rep_hints)
795-
if representation:
796-
url = representation[0]['content']['url_template']
794+
representations = self.get_representation_info(rep_hints)
795+
if representations:
796+
representation = representations[0]
797+
if representation['status'].get('code') in ('error_conversion_failed', 'error_password_protected'):
798+
return b''
799+
url = representation['content']['url_template']
797800
url = url.replace('{+asset_path}', '')
798801
response = self._session.get(url, expect_json_response=False)
799802
return response.content

test/unit/object/test_file.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,3 +902,49 @@ def test_get_thumbnail_representation_not_found(
902902
params={'fields': 'representations'},
903903
)
904904
assert thumb == b''
905+
906+
907+
def test_get_thumbnail_representation_not_available(
908+
test_file,
909+
mock_box_session,
910+
mock_content_response,
911+
):
912+
representation_url = '{0}/files/{1}'.format(API.BASE_API_URL, test_file.object_id)
913+
dimensions = '100x100'
914+
extension = 'jpg'
915+
916+
mock_representations_response = Mock()
917+
mock_representations_response.json.return_value = {
918+
'etag': '1',
919+
'id': test_file.object_id,
920+
'representations': {
921+
'entries': [
922+
{
923+
'content': {
924+
'url_template': 'content_url {+asset_path}'
925+
},
926+
'info': {
927+
'url': 'https://api.box.com/2.0/internal_files/123/versions/345/representations/jpg'
928+
},
929+
'properties': {},
930+
'representation': 'pdf',
931+
'status': {'state': 'error', 'code': 'error_password_protected'}
932+
}
933+
]
934+
},
935+
'type': 'file'
936+
}
937+
938+
mock_box_session.get.side_effect = [mock_representations_response, mock_content_response]
939+
940+
thumb = test_file.get_thumbnail_representation(
941+
dimensions=dimensions,
942+
extension=extension,
943+
)
944+
945+
mock_box_session.get.assert_any_call(
946+
representation_url,
947+
headers={'X-Rep-Hints': '[{}?dimensions={}]'.format(extension, dimensions)},
948+
params={'fields': 'representations'},
949+
)
950+
assert thumb == b''

0 commit comments

Comments
 (0)