diff --git a/notebook/services/api/api.yaml b/notebook/services/api/api.yaml index 9024fbd55..4cf8f9c9d 100644 --- a/notebook/services/api/api.yaml +++ b/notebook/services/api/api.yaml @@ -817,7 +817,7 @@ definitions: size: type: integer description: "The size of the file or directory." - formate: bytes + format: bytes mimetype: type: string description: "The mimetype of a file. If content is not null, and type is 'file', this will contain the mimetype of the file, otherwise this will be null." diff --git a/notebook/services/contents/filemanager.py b/notebook/services/contents/filemanager.py index b716c1822..e0a5af37e 100644 --- a/notebook/services/contents/filemanager.py +++ b/notebook/services/contents/filemanager.py @@ -190,10 +190,8 @@ class FileContentsManager(FileManagerMixin, ContentsManager): 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. @@ -272,7 +270,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) - # Create the base model. model = {} model['name'] = path.rsplit('/', 1)[-1] @@ -362,7 +359,6 @@ 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: @@ -391,7 +387,6 @@ class FileContentsManager(FileManagerMixin, ContentsManager): model['type'] = 'notebook' os_path = self._get_os_path(path) model['size'] = self._get_file_size(os_path) - if content: nb = self._read_notebook(os_path, as_version=4) @@ -400,7 +395,6 @@ class FileContentsManager(FileManagerMixin, ContentsManager): model['format'] = 'json' self.validate_notebook_model(model) - return model def get(self, path, content=True, type=None, format=None): diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index 588e6edd0..ef00a6d47 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1038,21 +1038,33 @@ define([ return (order == 1) ? 1 : -1; } }; + + // source: https://github.com/sindresorhus/pretty-bytes + var format_filesize = function(num) { + if (num === undefined) + return; - var format_filesize = function(filesize) { - if (filesize) { - var units = ['B', 'kB', 'MB', 'GB', 'TB']; - var base = 1000; - if (Math.abs(filesize) < base){ - return filesize + " B"; - } - var u = -1; - do { - filesize /= base; - u++; - } while(Math.abs(filesize) >= base && u < units.length - 1); - return filesize.toFixed(1) + " " + units[u]; + var UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + + if (!Number.isFinite(num)) { + console.error(`Expected a finite number, got ${typeof num}: ${num}`); } + + var neg = num < 0; + + if (neg) { + num = -num; + } + + if (num < 1) { + return (neg ? '-' : '') + num + ' B'; + } + + var exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1); + var numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3)); + var unit = UNITS[exponent]; + + return (neg ? '-' : '') + numStr + ' ' + unit; } // javascript stores text as utf16 and string indices use "code units",