##// END OF EJS Templates
Merge pull request #8005 from minrk/files-as-file...
Thomas Kluyver -
r20698:9e51e726 merge
parent child Browse files
Show More
@@ -1,54 +1,55
1 """Serve files directly from the ContentsManager."""
1 """Serve files directly from the ContentsManager."""
2
2
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 import os
6 import os
7 import mimetypes
7 import mimetypes
8 import json
8 import json
9 import base64
9 import base64
10
10
11 from tornado import web
11 from tornado import web
12
12
13 from IPython.html.base.handlers import IPythonHandler
13 from IPython.html.base.handlers import IPythonHandler
14
14
15 class FilesHandler(IPythonHandler):
15 class FilesHandler(IPythonHandler):
16 """serve files via ContentsManager"""
16 """serve files via ContentsManager"""
17
17
18 @web.authenticated
18 @web.authenticated
19 def get(self, path):
19 def get(self, path):
20 cm = self.contents_manager
20 cm = self.contents_manager
21 if cm.is_hidden(path):
21 if cm.is_hidden(path):
22 self.log.info("Refusing to serve hidden file, via 404 Error")
22 self.log.info("Refusing to serve hidden file, via 404 Error")
23 raise web.HTTPError(404)
23 raise web.HTTPError(404)
24
24
25 path = path.strip('/')
25 path = path.strip('/')
26 if '/' in path:
26 if '/' in path:
27 _, name = path.rsplit('/', 1)
27 _, name = path.rsplit('/', 1)
28 else:
28 else:
29 name = path
29 name = path
30
30
31 model = cm.get(path)
31 model = cm.get(path, type='file')
32
32
33 if self.get_argument("download", False):
33 if self.get_argument("download", False):
34 self.set_header('Content-Disposition','attachment; filename="%s"' % name)
34 self.set_header('Content-Disposition','attachment; filename="%s"' % name)
35
35
36 if model['type'] == 'notebook':
36 # get mimetype from filename
37 if name.endswith('.ipynb'):
37 self.set_header('Content-Type', 'application/json')
38 self.set_header('Content-Type', 'application/json')
38 else:
39 else:
39 cur_mime = mimetypes.guess_type(name)[0]
40 cur_mime = mimetypes.guess_type(name)[0]
40 if cur_mime is not None:
41 if cur_mime is not None:
41 self.set_header('Content-Type', cur_mime)
42 self.set_header('Content-Type', cur_mime)
42
43
43 if model['format'] == 'base64':
44 if model['format'] == 'base64':
44 b64_bytes = model['content'].encode('ascii')
45 b64_bytes = model['content'].encode('ascii')
45 self.write(base64.decodestring(b64_bytes))
46 self.write(base64.decodestring(b64_bytes))
46 elif model['format'] == 'json':
47 elif model['format'] == 'json':
47 self.write(json.dumps(model['content']))
48 self.write(json.dumps(model['content']))
48 else:
49 else:
49 self.write(model['content'])
50 self.write(model['content'])
50 self.flush()
51 self.flush()
51
52
52 default_handlers = [
53 default_handlers = [
53 (r"/files/(.*)", FilesHandler),
54 (r"/files/(.*)", FilesHandler),
54 ] No newline at end of file
55 ]
General Comments 0
You need to be logged in to leave comments. Login now