Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use StaticFileHandler when files are local #2656

Merged
merged 4 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -28,6 +28,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 @@ -146,6 +147,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 @@ -28,6 +28,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 @@ -129,6 +130,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