diff --git a/IPython/html/files/__init__.py b/IPython/html/files/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/IPython/html/files/__init__.py diff --git a/IPython/html/files/handlers.py b/IPython/html/files/handlers.py new file mode 100644 index 0000000..f1f38b1 --- /dev/null +++ b/IPython/html/files/handlers.py @@ -0,0 +1,48 @@ +"""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 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) + + self.set_header('Content-Disposition','attachment; filename="%s"' % name) + + 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), +] \ No newline at end of file diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py index 0d3b6f8..e8e920f 100644 --- a/IPython/html/notebookapp.py +++ b/IPython/html/notebookapp.py @@ -186,6 +186,7 @@ class NotebookWebApplication(web.Application): handlers.extend(load_handlers('tree.handlers')) handlers.extend(load_handlers('auth.login')) handlers.extend(load_handlers('auth.logout')) + handlers.extend(load_handlers('files.handlers')) handlers.extend(load_handlers('notebook.handlers')) handlers.extend(load_handlers('nbconvert.handlers')) handlers.extend(load_handlers('kernelspecs.handlers')) @@ -195,12 +196,6 @@ class NotebookWebApplication(web.Application): handlers.extend(load_handlers('services.sessions.handlers')) handlers.extend(load_handlers('services.nbconvert.handlers')) handlers.extend(load_handlers('services.kernelspecs.handlers')) - # FIXME: /files/ should be handled by the Contents service when it exists - cm = settings['contents_manager'] - if hasattr(cm, 'root_dir'): - handlers.append( - (r"/files/(.*)", AuthenticatedFileHandler, {'path' : cm.root_dir}), - ) handlers.append( (r"/nbextensions/(.*)", FileFindHandler, {'path' : settings['nbextensions_path']}), ) diff --git a/IPython/html/tests/test_files.py b/IPython/html/tests/test_files.py index 0653bfd..2dbf7f0 100644 --- a/IPython/html/tests/test_files.py +++ b/IPython/html/tests/test_files.py @@ -8,11 +8,17 @@ from unicodedata import normalize pjoin = os.path.join import requests +import json + +from IPython.nbformat.current import (new_notebook, write, new_worksheet, + new_heading_cell, new_code_cell, + new_output) from IPython.html.utils import url_path_join from .launchnotebook import NotebookTestBase from IPython.utils import py3compat + class FilesTest(NotebookTestBase): def test_hidden_files(self): not_hidden = [ @@ -50,6 +56,50 @@ class FilesTest(NotebookTestBase): r = requests.get(url_path_join(url, 'files', d, foo)) self.assertEqual(r.status_code, 404) + def test_contents_manager(self): + "make sure ContentsManager returns right files (ipynb, bin, txt)." + + nbdir = self.notebook_dir.name + base = self.base_url() + + nb = new_notebook(name='testnb') + + ws = new_worksheet() + nb.worksheets = [ws] + ws.cells.append(new_heading_cell(u'Created by test ³')) + cc1 = new_code_cell(input=u'print(2*6)') + cc1.outputs.append(new_output(output_text=u'12', output_type='stream')) + ws.cells.append(cc1) + + with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', + encoding='utf-8') as f: + write(nb, f, format='ipynb') + + with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f: + f.write(b'\xff' + os.urandom(5)) + f.close() + + with io.open(pjoin(nbdir, 'test.txt'), 'w') as f: + f.write(u'foobar') + f.close() + + r = requests.get(url_path_join(base, 'files', 'testnb.ipynb')) + self.assertEqual(r.status_code, 200) + self.assertIn('print(2*6)', r.text) + json.loads(r.text) + + r = requests.get(url_path_join(base, 'files', 'test.bin')) + self.assertEqual(r.status_code, 200) + self.assertEqual(r.headers['content-type'], 'application/octet-stream') + self.assertEqual(r.content[:1], b'\xff') + self.assertEqual(len(r.content), 6) + + r = requests.get(url_path_join(base, 'files', 'test.txt')) + self.assertEqual(r.status_code, 200) + self.assertEqual(r.headers['content-type'], 'text/plain') + self.assertEqual(r.text, 'foobar') + + def test_old_files_redirect(self): """pre-2.0 'files/' prefixed links are properly redirected""" nbdir = self.notebook_dir.name