Skip to content

Commit

Permalink
Merge pull request #2656 from agermanidis/master
Browse files Browse the repository at this point in the history
Use StaticFileHandler when files are local
  • Loading branch information
gnestor authored Jul 19, 2017
2 parents bcd0a1c + 95a53b7 commit 5192d72
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
21 changes: 18 additions & 3 deletions notebook/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import functools
import json
import mimetypes
import os
import re
import sys
Expand Down Expand Up @@ -471,13 +472,27 @@ class AuthenticatedFileHandler(IPythonHandler, web.StaticFileHandler):

@web.authenticated
def get(self, path):
if os.path.splitext(path)[1] == '.ipynb':
if os.path.splitext(path)[1] == '.ipynb' or self.get_argument("download", False):
name = path.rsplit('/', 1)[-1]
self.set_header('Content-Type', 'application/json')
self.set_header('Content-Disposition','attachment; filename="%s"' % escape.url_escape(name))

return web.StaticFileHandler.get(self, path)

def get_content_type(self):
path = self.absolute_path.strip('/')
if '/' in path:
_, name = path.rsplit('/', 1)
else:
name = path
if name.endswith('.ipynb'):
return 'application/x-ipynb+json'
else:
cur_mime = mimetypes.guess_type(name)[0]
if cur_mime == 'text/plain':
return 'text/plain; charset=UTF-8'
else:
return super(AuthenticatedFileHandler, self).get_content_type()

def set_headers(self):
super(AuthenticatedFileHandler, self).set_headers()
# disable browser caching, rely on 304 replies for savings
Expand Down
7 changes: 7 additions & 0 deletions notebook/files/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def head(self, path):
@web.authenticated
def get(self, path, include_body=True):
cm = self.contents_manager

if cm.files_handler_class:
return cm.files_handler_class(self.application, self.request, path=cm.root_dir)._execute(
[t(self.request) for t in self.application.transforms],
path
)

if cm.is_hidden(path):
self.log.info("Refusing to serve hidden file, via 404 Error")
raise web.HTTPError(404)
Expand Down
5 changes: 5 additions & 0 deletions notebook/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
is_hidden, is_file_hidden,
to_api_path,
)
from notebook.base.handlers import AuthenticatedFileHandler

try:
from os.path import samefile
Expand Down Expand Up @@ -148,6 +149,10 @@ def _validate_root_dir(self, proposal):
def _checkpoints_class_default(self):
return FileCheckpoints

@default('files_handler_class')
def _files_handler_class_default(self):
return AuthenticatedFileHandler

def is_hidden(self, path):
"""Does the API style path correspond to a hidden directory or file?
Expand Down
3 changes: 3 additions & 0 deletions notebook/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
default,
)
from ipython_genutils.py3compat import string_types
from notebook.base.handlers import IPythonHandler

copy_pat = re.compile(r'\-Copy\d*\.')

Expand Down Expand Up @@ -130,6 +131,8 @@ def _default_checkpoints_kwargs(self):
log=self.log,
)

files_handler_class = Type(IPythonHandler, allow_none=True, config=True)

# ContentsManager API part 1: methods that must be
# implemented in subclasses.

Expand Down

0 comments on commit 5192d72

Please sign in to comment.