Skip to content

Commit

Permalink
use raw files for gist images
Browse files Browse the repository at this point in the history
The gist API casts all file content to text without mentioning this, so images in gists are corrupted.

Always use the bytes from the raw URL for `image/*` mime types.
  • Loading branch information
minrk committed May 22, 2016
1 parent 0bf9258 commit 443a92e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions nbviewer/providers/gist/handlers.py
@@ -1,9 +1,5 @@
#-----------------------------------------------------------------------------
# Copyright (C) 2013 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import json

Expand Down Expand Up @@ -109,7 +105,12 @@ def get(self, user, gist_id, filename=''):

if filename and filename in files:
file = files[filename]
if file['truncated']:
if (file['type'] or '').startswith('image/'):
app_log.debug("Fetching raw image (%s) %s/%s: %s", file['type'], gist_id, filename, file['raw_url'])
response = yield self.fetch(file['raw_url'])
# use raw bytes for images:
content = response.body
elif file['truncated']:
app_log.debug("Gist %s/%s truncated, fetching %s", gist_id, filename, file['raw_url'])
response = yield self.fetch(file['raw_url'])
content = response_text(response, encoding='utf-8')
Expand All @@ -128,6 +129,7 @@ def get(self, user, gist_id, filename=''):
**PROVIDER_CTX
)
else:
self.set_header('Content-Type', file.get('type') or 'text/plain')
# cannot redirect because of X-Frame-Content
self.finish(content)
return
Expand Down

0 comments on commit 443a92e

Please sign in to comment.