##// END OF EJS Templates
use ?download=1 to trigger download in /files/...
use ?download=1 to trigger download in /files/ sets `Content-Disposition: attachment...` - master sets this unconditionally - 2.x sets this iff file is a notebook

File last commit:

r18556:70450eeb
r18556:70450eeb
Show More
handlers.py
48 lines | 1.5 KiB | text/x-python | PythonLexer
"""Serve files directly from the ContentsManager."""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import mimetypes
import json
import base64
from tornado import web
from IPython.html.base.handlers import IPythonHandler
class FilesHandler(IPythonHandler):
"""serve files via ContentsManager"""
@web.authenticated
def get(self, path):
cm = self.settings['contents_manager']
if cm.is_hidden(path):
self.log.info("Refusing to serve hidden file, via 404 Error")
raise web.HTTPError(404)
path, name = os.path.split(path)
model = cm.get_model(name, path)
if self.get_argument("download", False):
self.set_header('Content-Disposition','attachment; filename="%s"' % name)
if model['type'] == 'notebook':
self.set_header('Content-Type', 'application/json')
else:
cur_mime = mimetypes.guess_type(name)[0]
if cur_mime is not None:
self.set_header('Content-Type', cur_mime)
if model['format'] == 'base64':
b64_bytes = model['content'].encode('ascii')
self.write(base64.decodestring(b64_bytes))
elif model['format'] == 'json':
self.write(json.dumps(model['content']))
else:
self.write(model['content'])
self.flush()
default_handlers = [
(r"/files/(.*)", FilesHandler),
]