Show More
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 |
@@ -0,0 +1,48 b'' | |||||
|
1 | """Serve files directly from the ContentsManager.""" | |||
|
2 | ||||
|
3 | # Copyright (c) IPython Development Team. | |||
|
4 | # Distributed under the terms of the Modified BSD License. | |||
|
5 | ||||
|
6 | import os | |||
|
7 | import mimetypes | |||
|
8 | import json | |||
|
9 | import base64 | |||
|
10 | ||||
|
11 | from tornado import web | |||
|
12 | ||||
|
13 | from IPython.html.base.handlers import IPythonHandler | |||
|
14 | ||||
|
15 | class FilesHandler(IPythonHandler): | |||
|
16 | """serve files via ContentsManager""" | |||
|
17 | ||||
|
18 | @web.authenticated | |||
|
19 | def get(self, path): | |||
|
20 | cm = self.settings['contents_manager'] | |||
|
21 | if cm.is_hidden(path): | |||
|
22 | self.log.info("Refusing to serve hidden file, via 404 Error") | |||
|
23 | raise web.HTTPError(404) | |||
|
24 | ||||
|
25 | path, name = os.path.split(path) | |||
|
26 | model = cm.get_model(name, path) | |||
|
27 | ||||
|
28 | if model['type'] == 'notebook': | |||
|
29 | self.set_header('Content-Type', 'application/json') | |||
|
30 | else: | |||
|
31 | cur_mime = mimetypes.guess_type(name)[0] | |||
|
32 | if cur_mime is not None: | |||
|
33 | self.set_header('Content-Type', cur_mime) | |||
|
34 | ||||
|
35 | self.set_header('Content-Disposition','attachment; filename="%s"' % name) | |||
|
36 | ||||
|
37 | if model['format'] == 'base64': | |||
|
38 | b64_bytes = model['content'].encode('ascii') | |||
|
39 | self.write(base64.decodestring(b64_bytes)) | |||
|
40 | elif model['format'] == 'json': | |||
|
41 | self.write(json.dumps(model['content'])) | |||
|
42 | else: | |||
|
43 | self.write(model['content']) | |||
|
44 | self.flush() | |||
|
45 | ||||
|
46 | default_handlers = [ | |||
|
47 | (r"/files/(.*)", FilesHandler), | |||
|
48 | ] No newline at end of file |
@@ -186,6 +186,7 b' class NotebookWebApplication(web.Application):' | |||||
186 | handlers.extend(load_handlers('tree.handlers')) |
|
186 | handlers.extend(load_handlers('tree.handlers')) | |
187 | handlers.extend(load_handlers('auth.login')) |
|
187 | handlers.extend(load_handlers('auth.login')) | |
188 | handlers.extend(load_handlers('auth.logout')) |
|
188 | handlers.extend(load_handlers('auth.logout')) | |
|
189 | handlers.extend(load_handlers('files.handlers')) | |||
189 | handlers.extend(load_handlers('notebook.handlers')) |
|
190 | handlers.extend(load_handlers('notebook.handlers')) | |
190 | handlers.extend(load_handlers('nbconvert.handlers')) |
|
191 | handlers.extend(load_handlers('nbconvert.handlers')) | |
191 | handlers.extend(load_handlers('kernelspecs.handlers')) |
|
192 | handlers.extend(load_handlers('kernelspecs.handlers')) | |
@@ -195,12 +196,6 b' class NotebookWebApplication(web.Application):' | |||||
195 | handlers.extend(load_handlers('services.sessions.handlers')) |
|
196 | handlers.extend(load_handlers('services.sessions.handlers')) | |
196 | handlers.extend(load_handlers('services.nbconvert.handlers')) |
|
197 | handlers.extend(load_handlers('services.nbconvert.handlers')) | |
197 | handlers.extend(load_handlers('services.kernelspecs.handlers')) |
|
198 | handlers.extend(load_handlers('services.kernelspecs.handlers')) | |
198 | # FIXME: /files/ should be handled by the Contents service when it exists |
|
|||
199 | cm = settings['contents_manager'] |
|
|||
200 | if hasattr(cm, 'root_dir'): |
|
|||
201 | handlers.append( |
|
|||
202 | (r"/files/(.*)", AuthenticatedFileHandler, {'path' : cm.root_dir}), |
|
|||
203 | ) |
|
|||
204 | handlers.append( |
|
199 | handlers.append( | |
205 | (r"/nbextensions/(.*)", FileFindHandler, {'path' : settings['nbextensions_path']}), |
|
200 | (r"/nbextensions/(.*)", FileFindHandler, {'path' : settings['nbextensions_path']}), | |
206 | ) |
|
201 | ) |
@@ -8,11 +8,17 b' from unicodedata import normalize' | |||||
8 | pjoin = os.path.join |
|
8 | pjoin = os.path.join | |
9 |
|
9 | |||
10 | import requests |
|
10 | import requests | |
|
11 | import json | |||
|
12 | ||||
|
13 | from IPython.nbformat.current import (new_notebook, write, new_worksheet, | |||
|
14 | new_heading_cell, new_code_cell, | |||
|
15 | new_output) | |||
11 |
|
16 | |||
12 | from IPython.html.utils import url_path_join |
|
17 | from IPython.html.utils import url_path_join | |
13 | from .launchnotebook import NotebookTestBase |
|
18 | from .launchnotebook import NotebookTestBase | |
14 | from IPython.utils import py3compat |
|
19 | from IPython.utils import py3compat | |
15 |
|
20 | |||
|
21 | ||||
16 | class FilesTest(NotebookTestBase): |
|
22 | class FilesTest(NotebookTestBase): | |
17 | def test_hidden_files(self): |
|
23 | def test_hidden_files(self): | |
18 | not_hidden = [ |
|
24 | not_hidden = [ | |
@@ -50,6 +56,50 b' class FilesTest(NotebookTestBase):' | |||||
50 | r = requests.get(url_path_join(url, 'files', d, foo)) |
|
56 | r = requests.get(url_path_join(url, 'files', d, foo)) | |
51 | self.assertEqual(r.status_code, 404) |
|
57 | self.assertEqual(r.status_code, 404) | |
52 |
|
58 | |||
|
59 | def test_contents_manager(self): | |||
|
60 | "make sure ContentsManager returns right files (ipynb, bin, txt)." | |||
|
61 | ||||
|
62 | nbdir = self.notebook_dir.name | |||
|
63 | base = self.base_url() | |||
|
64 | ||||
|
65 | nb = new_notebook(name='testnb') | |||
|
66 | ||||
|
67 | ws = new_worksheet() | |||
|
68 | nb.worksheets = [ws] | |||
|
69 | ws.cells.append(new_heading_cell(u'Created by test ³')) | |||
|
70 | cc1 = new_code_cell(input=u'print(2*6)') | |||
|
71 | cc1.outputs.append(new_output(output_text=u'12', output_type='stream')) | |||
|
72 | ws.cells.append(cc1) | |||
|
73 | ||||
|
74 | with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', | |||
|
75 | encoding='utf-8') as f: | |||
|
76 | write(nb, f, format='ipynb') | |||
|
77 | ||||
|
78 | with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f: | |||
|
79 | f.write(b'\xff' + os.urandom(5)) | |||
|
80 | f.close() | |||
|
81 | ||||
|
82 | with io.open(pjoin(nbdir, 'test.txt'), 'w') as f: | |||
|
83 | f.write(u'foobar') | |||
|
84 | f.close() | |||
|
85 | ||||
|
86 | r = requests.get(url_path_join(base, 'files', 'testnb.ipynb')) | |||
|
87 | self.assertEqual(r.status_code, 200) | |||
|
88 | self.assertIn('print(2*6)', r.text) | |||
|
89 | json.loads(r.text) | |||
|
90 | ||||
|
91 | r = requests.get(url_path_join(base, 'files', 'test.bin')) | |||
|
92 | self.assertEqual(r.status_code, 200) | |||
|
93 | self.assertEqual(r.headers['content-type'], 'application/octet-stream') | |||
|
94 | self.assertEqual(r.content[:1], b'\xff') | |||
|
95 | self.assertEqual(len(r.content), 6) | |||
|
96 | ||||
|
97 | r = requests.get(url_path_join(base, 'files', 'test.txt')) | |||
|
98 | self.assertEqual(r.status_code, 200) | |||
|
99 | self.assertEqual(r.headers['content-type'], 'text/plain') | |||
|
100 | self.assertEqual(r.text, 'foobar') | |||
|
101 | ||||
|
102 | ||||
53 | def test_old_files_redirect(self): |
|
103 | def test_old_files_redirect(self): | |
54 | """pre-2.0 'files/' prefixed links are properly redirected""" |
|
104 | """pre-2.0 'files/' prefixed links are properly redirected""" | |
55 | nbdir = self.notebook_dir.name |
|
105 | nbdir = self.notebook_dir.name |
General Comments 0
You need to be logged in to leave comments.
Login now