|
|
|
|
@ -209,11 +209,15 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
def _file_model(self, path, content=True):
|
|
|
|
|
def _file_model(self, path, content=True, format=None):
|
|
|
|
|
"""Build a model for a file
|
|
|
|
|
|
|
|
|
|
if content is requested, include the file contents.
|
|
|
|
|
UTF-8 text files will be unicode, binary files will be base64-encoded.
|
|
|
|
|
|
|
|
|
|
format:
|
|
|
|
|
If 'text', the contents will be decoded as UTF-8.
|
|
|
|
|
If 'base64', the raw bytes contents will be encoded as base64.
|
|
|
|
|
If not specified, try to decode as UTF-8, and fall back to base64
|
|
|
|
|
"""
|
|
|
|
|
model = self._base_model(path)
|
|
|
|
|
model['type'] = 'file'
|
|
|
|
|
@ -224,13 +228,20 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
raise web.HTTPError(400, "Cannot get content of non-file %s" % os_path)
|
|
|
|
|
with io.open(os_path, 'rb') as f:
|
|
|
|
|
bcontent = f.read()
|
|
|
|
|
try:
|
|
|
|
|
model['content'] = bcontent.decode('utf8')
|
|
|
|
|
except UnicodeError as e:
|
|
|
|
|
|
|
|
|
|
if format != 'base64':
|
|
|
|
|
try:
|
|
|
|
|
model['content'] = bcontent.decode('utf8')
|
|
|
|
|
except UnicodeError as e:
|
|
|
|
|
if format == 'text':
|
|
|
|
|
raise web.HTTPError(400, "%s is not UTF-8 encoded" % path)
|
|
|
|
|
else:
|
|
|
|
|
model['format'] = 'text'
|
|
|
|
|
|
|
|
|
|
if model['content'] is None:
|
|
|
|
|
model['content'] = base64.encodestring(bcontent).decode('ascii')
|
|
|
|
|
model['format'] = 'base64'
|
|
|
|
|
else:
|
|
|
|
|
model['format'] = 'text'
|
|
|
|
|
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -255,7 +266,7 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
self.validate_notebook_model(model)
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
def get_model(self, path, content=True, type_=None):
|
|
|
|
|
def get_model(self, path, content=True, type_=None, format=None):
|
|
|
|
|
""" Takes a path for an entity and returns its model
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
@ -267,6 +278,9 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
type_ : str, optional
|
|
|
|
|
The requested type - 'file', 'notebook', or 'directory'.
|
|
|
|
|
Will raise HTTPError 406 if the content doesn't match.
|
|
|
|
|
format : str, optional
|
|
|
|
|
The requested format for file contents. 'text' or 'base64'.
|
|
|
|
|
Ignored if this returns a notebook or directory model.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
@ -291,7 +305,7 @@ class FileContentsManager(ContentsManager):
|
|
|
|
|
if type_ == 'directory':
|
|
|
|
|
raise web.HTTPError(400,
|
|
|
|
|
u'%s is not a directory')
|
|
|
|
|
model = self._file_model(path, content=content)
|
|
|
|
|
model = self._file_model(path, content=content, format=format)
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
def _save_notebook(self, os_path, model, path=''):
|
|
|
|
|
|