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