only get filesize for files and notebooks

This commit is contained in:
Celina Kilcrease 2018-04-14 20:04:45 -04:00
parent b864f5aacc
commit 3315dbd55f

View File

@ -183,6 +183,17 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
os_path = self._get_os_path(path=path)
return is_hidden(os_path, self.root_dir)
def _get_file_size(self, path):
try:
# size of file
size = os.path.getsize(path)
except (ValueError, OSError):
self.log.warning('Unable to get size.')
size = None
return size
def file_exists(self, path):
"""Returns True if the file exists, else returns False.
@ -261,12 +272,6 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
self.log.warning('Invalid ctime %s for %s', info.st_ctime, os_path)
created = datetime(1970, 1, 1, 0, 0, tzinfo=tz.UTC)
try:
# size of file and directory?
size = os.path.getsize(os_path)
except (ValueError, OSError):
self.log.warning('Unable to get size.')
size = None
# Create the base model.
model = {}
@ -277,7 +282,6 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
model['content'] = None
model['format'] = None
model['mimetype'] = None
model['size'] = size
try:
model['writable'] = os.access(os_path, os.W_OK)
@ -342,6 +346,7 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
return model
def _file_model(self, path, content=True, format=None):
"""Build a model for a file
@ -357,6 +362,8 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
os_path = self._get_os_path(path)
model['mimetype'] = mimetypes.guess_type(os_path)[0]
model['size'] = self._get_file_size(os_path)
if content:
content, format = self._read_file(os_path, format)
@ -382,13 +389,18 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
"""
model = self._base_model(path)
model['type'] = 'notebook'
if content:
os_path = self._get_os_path(path)
nb = self._read_notebook(os_path, as_version=4)
self.mark_trusted_cells(nb, path)
model['content'] = nb
model['format'] = 'json'
model['size'] = self._get_file_size(os_path)
self.validate_notebook_model(model)
return model
def get(self, path, content=True, type=None, format=None):