Merge pull request #2656 from agermanidis/master

Use StaticFileHandler when files are local
pull/2672/head
Grant Nestor 9 years ago committed by GitHub
commit 5192d72c63

@ -5,6 +5,7 @@
import functools
import json
import mimetypes
import os
import re
import sys
@ -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

@ -26,6 +26,13 @@ class FilesHandler(IPythonHandler):
@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)

@ -30,6 +30,7 @@ from notebook.utils import (
is_hidden, is_file_hidden,
to_api_path,
)
from notebook.base.handlers import AuthenticatedFileHandler
try:
from os.path import samefile
@ -148,6 +149,10 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
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?

@ -29,6 +29,7 @@ from traitlets import (
default,
)
from ipython_genutils.py3compat import string_types
from notebook.base.handlers import IPythonHandler
copy_pat = re.compile(r'\-Copy\d*\.')
@ -130,6 +131,8 @@ class ContentsManager(LoggingConfigurable):
log=self.log,
)
files_handler_class = Type(IPythonHandler, allow_none=True, config=True)
# ContentsManager API part 1: methods that must be
# implemented in subclasses.

Loading…
Cancel
Save