From ff8e1cc983cc89f75b6430e114e1f24b5e32ed19 Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Sat, 14 Apr 2018 20:04:45 -0400 Subject: [PATCH 1/5] only get filesize for files and notebooks adding sort function add file size text --- notebook/services/contents/filemanager.py | 26 ++++++++++++----- notebook/static/base/js/utils.js | 6 ++++ notebook/static/tree/js/notebooklist.js | 34 ++++++++++++++++++++++- notebook/templates/tree.html | 6 ++++ 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/notebook/services/contents/filemanager.py b/notebook/services/contents/filemanager.py index 94bfcb3e3..a2003d329 100644 --- a/notebook/services/contents/filemanager.py +++ b/notebook/services/contents/filemanager.py @@ -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): diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index 8f54a6384..c7f51ecef 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1039,6 +1039,11 @@ define([ } }; + var format_filesize = function(filesize) { + if (filesize) { + return filesize + " bytes"; + } + } // javascript stores text as utf16 and string indices use "code units", // which stores high-codepoint characters as "surrogate pairs", @@ -1180,6 +1185,7 @@ define([ parse_b64_data_uri: parse_b64_data_uri, time: time, format_datetime: format_datetime, + format_filesize: format_filesize, datetime_sort_helper: datetime_sort_helper, dnd_contain_file: dnd_contain_file, js_idx_to_char_idx: js_idx_to_char_idx, diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 9fadfe729..5e8ff1154 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -62,9 +62,34 @@ define([ }); } + function size_sorter(ascending) { + var order = ascending ? 1 : 0; + // directories have file size of undefined + return (function(a, b) { + if (a.size === undefined) { + return (ascending) ? 1 : -1; + } + + if (b.size === undefined) { + return (ascending) ? -1 : 1; + } + + if (a.size > b.size) { + return (ascending) ? 1 : -1; + } + + if (b.size > a.size) { + return (ascending) ? -1 : 1; + } + + return 0; + }); + } + var sort_functions = { 'sort-name': name_sorter, - 'last-modified': modified_sorter + 'last-modified': modified_sorter, + 'file-size': size_sorter }; var NotebookList = function (selector, options) { @@ -520,6 +545,12 @@ define([ .addClass("item_name") .appendTo(link); + $("") + .addClass("file_size") + .addClass("pull-right") + .css("width", "50px") + .appendTo(item); + $("") .addClass("item_modified") .addClass("pull-right") @@ -835,6 +866,7 @@ define([ // Add in the date that the file was last modified item.find(".item_modified").text(utils.format_datetime(model.last_modified)); item.find(".item_modified").attr("title", moment(model.last_modified).format("YYYY-MM-DD HH:mm")); + item.find(".file_size").html(utils.format_filesize(model.size) || " "); }; diff --git a/notebook/templates/tree.html b/notebook/templates/tree.html index e37f08d3c..295bd7768 100644 --- a/notebook/templates/tree.html +++ b/notebook/templates/tree.html @@ -117,6 +117,12 @@ data-server-root="{{server_root}}" {% endfor %} +
+ + {% trans %}File size{% endtrans %} + + +
{% trans %}Last Modified{% endtrans %} From abe05dea420f2cce107c4f8c13d38f122161590d Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Tue, 17 Apr 2018 11:14:12 -0400 Subject: [PATCH 2/5] change column width, flip order --- notebook/static/tree/js/notebooklist.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 5e8ff1154..d630de66a 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -67,19 +67,19 @@ define([ // directories have file size of undefined return (function(a, b) { if (a.size === undefined) { - return (ascending) ? 1 : -1; - } - - if (b.size === undefined) { return (ascending) ? -1 : 1; } + if (b.size === undefined) { + return (ascending) ? 1 : -1; + } + if (a.size > b.size) { - return (ascending) ? 1 : -1; + return (ascending) ? -1 : 1; } if (b.size > a.size) { - return (ascending) ? -1 : 1; + return (ascending) ? 1 : -1; } return 0; @@ -548,7 +548,7 @@ define([ $("") .addClass("file_size") .addClass("pull-right") - .css("width", "50px") + .css("width", "65px") .appendTo(item); $("") From 13933f396af2c8a0c5705e434099d8c7d9da085e Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Tue, 17 Apr 2018 11:37:56 -0400 Subject: [PATCH 3/5] converting filesize to readable format --- notebook/static/base/js/utils.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index c7f51ecef..a1074e6b4 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1041,9 +1041,25 @@ define([ var format_filesize = function(filesize) { if (filesize) { - return filesize + " bytes"; + return (convertFileSize(filesize)); } } + + var convertFileSize = function(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]; + + } + // javascript stores text as utf16 and string indices use "code units", // which stores high-codepoint characters as "surrogate pairs", From acc2056e05b4e62f74c7a6b5d3e315a7a0d6639e Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Tue, 17 Apr 2018 11:45:07 -0400 Subject: [PATCH 4/5] fixed filesize fixed typo --- notebook/services/api/api.yaml | 2 +- notebook/services/contents/filemanager.py | 12 ++-------- notebook/static/base/js/utils.js | 28 +++++++++-------------- 3 files changed, 14 insertions(+), 28 deletions(-) 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 a2003d329..a2494eb25 100644 --- a/notebook/services/contents/filemanager.py +++ b/notebook/services/contents/filemanager.py @@ -185,15 +185,12 @@ class FileContentsManager(FileManagerMixin, ContentsManager): 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. @@ -272,7 +269,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] @@ -346,7 +342,6 @@ class FileContentsManager(FileManagerMixin, ContentsManager): return model - def _file_model(self, path, content=True, format=None): """Build a model for a file @@ -362,7 +357,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: @@ -389,18 +383,16 @@ class FileContentsManager(FileManagerMixin, ContentsManager): """ model = self._base_model(path) model['type'] = 'notebook' - + os_path = self._get_os_path(path) + model['size'] = self._get_file_size(os_path) 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): diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index a1074e6b4..588e6edd0 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1041,25 +1041,19 @@ define([ var format_filesize = function(filesize) { if (filesize) { - return (convertFileSize(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 convertFileSize = function(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]; - - } - // javascript stores text as utf16 and string indices use "code units", // which stores high-codepoint characters as "surrogate pairs", From 2e9ba4a1acb0f8ea329860dac6c98a4a13d65d13 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Wed, 18 Apr 2018 00:03:52 -0400 Subject: [PATCH 5/5] use pretty-bytes --- notebook/static/base/js/utils.js | 38 +++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) 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",