From 3650cc7cb41b2c52b55875922003a6eb604f61fc Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Fri, 13 Apr 2018 21:41:58 -0400 Subject: [PATCH 01/26] adding file size to model --- notebook/services/api/api.yaml | 5 +++++ notebook/services/contents/filemanager.py | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/notebook/services/api/api.yaml b/notebook/services/api/api.yaml index 0e3b3ead3a..56efce9548 100644 --- a/notebook/services/api/api.yaml +++ b/notebook/services/api/api.yaml @@ -789,6 +789,7 @@ definitions: - mimetype - format - content + - size properties: name: type: string @@ -814,6 +815,10 @@ definitions: type: string description: Last modified timestamp format: dateTime + size: + type: integer + description: "The size of the file or directory." + formate: 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 833fe4a4e2..94bfcb3e30 100644 --- a/notebook/services/contents/filemanager.py +++ b/notebook/services/contents/filemanager.py @@ -261,6 +261,13 @@ def _base_model(self, path): 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 = {} model['name'] = path.rsplit('/', 1)[-1] @@ -270,6 +277,8 @@ def _base_model(self, path): model['content'] = None model['format'] = None model['mimetype'] = None + model['size'] = size + try: model['writable'] = os.access(os_path, os.W_OK) except OSError: From b864f5aaccd702fb296b29107d4dd54f384cb3cc Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Sat, 14 Apr 2018 18:49:01 -0400 Subject: [PATCH 02/26] make file size optional pproperty --- notebook/services/api/api.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/notebook/services/api/api.yaml b/notebook/services/api/api.yaml index 56efce9548..9024fbd55d 100644 --- a/notebook/services/api/api.yaml +++ b/notebook/services/api/api.yaml @@ -789,7 +789,6 @@ definitions: - mimetype - format - content - - size properties: name: type: string From 3315dbd55f5c1ab1a0f96456f9fbb7d6f9238bc6 Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Sat, 14 Apr 2018 20:04:45 -0400 Subject: [PATCH 03/26] only get filesize for files and notebooks --- notebook/services/contents/filemanager.py | 26 +++++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/notebook/services/contents/filemanager.py b/notebook/services/contents/filemanager.py index 94bfcb3e30..a2003d3296 100644 --- a/notebook/services/contents/filemanager.py +++ b/notebook/services/contents/filemanager.py @@ -183,6 +183,17 @@ def is_hidden(self, path): 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 @@ def _base_model(self, path): 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 @@ def _base_model(self, path): 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 @@ def _dir_model(self, path, content=True): return model + def _file_model(self, path, content=True, format=None): """Build a model for a file @@ -357,6 +362,8 @@ def _file_model(self, path, content=True, format=None): 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 @@ def _notebook_model(self, path, content=True): """ 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): From 388d6367caa8aca4c13751b7bc752bedc0a64b97 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Sun, 15 Apr 2018 10:17:10 -0400 Subject: [PATCH 04/26] starting out the sort function --- notebook/static/tree/js/notebooklist.js | 26 ++++++++++++++++++++++++- notebook/templates/tree.html | 6 ++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 9fadfe7292..2246002ac5 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -57,14 +57,32 @@ define([ function modified_sorter(ascending) { var order = ascending ? 1 : 0; return (function(a, b) { + if (a['type'] === 'directory') { + return (ascending) ? 1 : -1; + } return utils.datetime_sort_helper(a.last_modified, b.last_modified, order) }); } + function size_sorter(ascending) { + var order = ascending ? 1 : 0; + // directories have file size of undefined + return (function(a, b) { + if (a['type'] === 'directory') { + return (ascending) ? 1 : -1; + } + + if (a.size > b.size) { + return (ascending) ? 1 : -1; + } + }); + } + 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 +538,12 @@ define([ .addClass("item_name") .appendTo(link); + $("") + .addClass("file_size") + .addClass("pull-right") + .css("width", "50px") + .appendTo(item); + $("") .addClass("item_modified") .addClass("pull-right") diff --git a/notebook/templates/tree.html b/notebook/templates/tree.html index e37f08d3cf..295bd77681 100644 --- a/notebook/templates/tree.html +++ b/notebook/templates/tree.html @@ -117,6 +117,12 @@ {% endfor %} +
+ + {% trans %}File size{% endtrans %} + + +
{% trans %}Last Modified{% endtrans %} From 38acc780cab786337bb15624cbf7a197a6a8386d Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Sun, 15 Apr 2018 10:30:50 -0400 Subject: [PATCH 05/26] add file size text --- notebook/static/tree/js/notebooklist.js | 1 + 1 file changed, 1 insertion(+) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 2246002ac5..92933538c7 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -859,6 +859,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").text(model.size || ''); }; From 59b7c666628d19f93a801b05a9d23e50c41e7425 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Sun, 15 Apr 2018 14:47:29 -0400 Subject: [PATCH 06/26] revert chages --- notebook/static/tree/js/notebooklist.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 92933538c7..ffde675d97 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -57,9 +57,6 @@ define([ function modified_sorter(ascending) { var order = ascending ? 1 : 0; return (function(a, b) { - if (a['type'] === 'directory') { - return (ascending) ? 1 : -1; - } return utils.datetime_sort_helper(a.last_modified, b.last_modified, order) }); From 86c45a17ed946ff1056d0a4522e071f4fc3b7979 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Sun, 15 Apr 2018 14:54:16 -0400 Subject: [PATCH 07/26] fix sort function --- notebook/static/tree/js/notebooklist.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index ffde675d97..8606327851 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -66,13 +66,21 @@ define([ var order = ascending ? 1 : 0; // directories have file size of undefined return (function(a, b) { - if (a['type'] === 'directory') { + 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; + } else { + return (ascending) ? -1 : 1; } + + return 0; }); } From a299be7ff7112ac1821110703c65c77f69ff4dd2 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Sun, 15 Apr 2018 15:02:51 -0400 Subject: [PATCH 08/26] fix sorting function foreal --- notebook/static/tree/js/notebooklist.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 8606327851..fb8104ec36 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -71,12 +71,14 @@ define([ } if (b.size === undefined) { - return (ascending) ? 1 : -1; + return (ascending) ? -1 : 1; } if (a.size > b.size) { return (ascending) ? 1 : -1; - } else { + } + + if (b.size > a.size) { return (ascending) ? -1 : 1; } @@ -864,7 +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").text(model.size || ''); + item.find(".file_size").html(model.size || " "); }; From 6475eaa73f45a6771b6713d21d5f71c18469a981 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Sun, 15 Apr 2018 15:06:21 -0400 Subject: [PATCH 09/26] empty function to format filesize --- notebook/static/base/js/utils.js | 6 ++++++ notebook/static/tree/js/notebooklist.js | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index 8f54a6384b..c7f51ecef3 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 fb8104ec36..5e8ff1154f 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -866,7 +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(model.size || " "); + item.find(".file_size").html(utils.format_filesize(model.size) || " "); }; From d6c7f2e7a8b3ff1851bcc89a56b70437e394a65d Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Tue, 17 Apr 2018 11:14:12 -0400 Subject: [PATCH 10/26] change column width, flip order --- notebook/static/tree/js/notebooklist.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 5e8ff1154f..d630de66a3 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; + return (ascending) ? -1 : 1; } if (b.size === undefined) { - return (ascending) ? -1 : 1; + 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 42c3c02f9564450ea83f917c1274f0da24ddcfdd Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Tue, 17 Apr 2018 11:37:56 -0400 Subject: [PATCH 11/26] 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 c7f51ecef3..a1074e6b47 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 8c96ad58c717e1b13d7af4de2df0bc1df207fb0a Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Tue, 17 Apr 2018 11:41:09 -0400 Subject: [PATCH 12/26] cleanup --- notebook/static/base/js/utils.js | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index a1074e6b47..588e6edd0d 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1041,26 +1041,20 @@ define([ var format_filesize = function(filesize) { if (filesize) { - 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 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 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", // which occupy two indices in the javascript string. From d19f97017fe2bfc887d274b5addbd0e550a0e181 Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Tue, 17 Apr 2018 11:45:07 -0400 Subject: [PATCH 13/26] fixed filesize --- notebook/services/contents/filemanager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notebook/services/contents/filemanager.py b/notebook/services/contents/filemanager.py index a2003d3296..b716c18228 100644 --- a/notebook/services/contents/filemanager.py +++ b/notebook/services/contents/filemanager.py @@ -389,15 +389,15 @@ def _notebook_model(self, path, content=True): """ 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) From ff8e1cc983cc89f75b6430e114e1f24b5e32ed19 Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Sat, 14 Apr 2018 20:04:45 -0400 Subject: [PATCH 14/26] 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 94bfcb3e30..a2003d3296 100644 --- a/notebook/services/contents/filemanager.py +++ b/notebook/services/contents/filemanager.py @@ -183,6 +183,17 @@ def is_hidden(self, path): 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 @@ def _base_model(self, path): 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 @@ def _base_model(self, path): 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 @@ def _dir_model(self, path, content=True): return model + def _file_model(self, path, content=True, format=None): """Build a model for a file @@ -357,6 +362,8 @@ def _file_model(self, path, content=True, format=None): 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 @@ def _notebook_model(self, path, content=True): """ 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 8f54a6384b..c7f51ecef3 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 9fadfe7292..5e8ff1154f 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 e37f08d3cf..295bd77681 100644 --- a/notebook/templates/tree.html +++ b/notebook/templates/tree.html @@ -117,6 +117,12 @@ {% 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 15/26] change column width, flip order --- notebook/static/tree/js/notebooklist.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 5e8ff1154f..d630de66a3 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; + return (ascending) ? -1 : 1; } if (b.size === undefined) { - return (ascending) ? -1 : 1; + 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 16/26] 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 c7f51ecef3..a1074e6b47 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 17/26] 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 9024fbd55d..4cf8f9c9dc 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 a2003d3296..a2494eb25d 100644 --- a/notebook/services/contents/filemanager.py +++ b/notebook/services/contents/filemanager.py @@ -185,15 +185,12 @@ def is_hidden(self, path): 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 @@ def _base_model(self, path): 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 @@ def _dir_model(self, path, content=True): return model - def _file_model(self, path, content=True, format=None): """Build a model for a file @@ -362,7 +357,6 @@ def _file_model(self, path, content=True, format=None): 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 @@ def _notebook_model(self, path, content=True): """ 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 a1074e6b47..588e6edd0d 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1041,26 +1041,20 @@ define([ var format_filesize = function(filesize) { if (filesize) { - 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 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 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", // which occupy two indices in the javascript string. From 2e9ba4a1acb0f8ea329860dac6c98a4a13d65d13 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Wed, 18 Apr 2018 00:03:52 -0400 Subject: [PATCH 18/26] 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 588e6edd0d..ef00a6d479 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", From 06214aea0a71ea97ec1f7f8b837cd45e359918c5 Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Wed, 18 Apr 2018 10:44:53 -0400 Subject: [PATCH 19/26] remove inline css --- notebook/static/tree/js/notebooklist.js | 1 - notebook/static/tree/less/tree.less | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index d630de66a3..b1cd6e821e 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -548,7 +548,6 @@ define([ $("") .addClass("file_size") .addClass("pull-right") - .css("width", "65px") .appendTo(item); $("") diff --git a/notebook/static/tree/less/tree.less b/notebook/static/tree/less/tree.less index 4e24193db2..f73d7332fb 100644 --- a/notebook/static/tree/less/tree.less +++ b/notebook/static/tree/less/tree.less @@ -167,6 +167,10 @@ ul.breadcrumb { margin-left: @dashboard_lr_pad; } +.file_size{ + width: 65px; +} + [dir="rtl"] .item_modified.pull-right{ .pull-left(); } From d35ac8bc3ac88c63d98538631b4cca79dea05dd2 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Wed, 18 Apr 2018 12:31:23 -0400 Subject: [PATCH 20/26] right align file size column --- notebook/static/tree/less/tree.less | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/notebook/static/tree/less/tree.less b/notebook/static/tree/less/tree.less index f73d7332fb..27f7123f2d 100644 --- a/notebook/static/tree/less/tree.less +++ b/notebook/static/tree/less/tree.less @@ -167,8 +167,9 @@ ul.breadcrumb { margin-left: @dashboard_lr_pad; } -.file_size{ +.file_size { width: 65px; + text-align: right; } [dir="rtl"] .item_modified.pull-right{ From dd608ad2bd30207c85ae321dd03093ded008ec58 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Wed, 18 Apr 2018 12:33:03 -0400 Subject: [PATCH 21/26] add MIT licence --- notebook/static/base/js/utils.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index ef00a6d479..4d0d5c602a 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1039,7 +1039,30 @@ define([ } }; - // source: https://github.com/sindresorhus/pretty-bytes + /** + source: https://github.com/sindresorhus/pretty-bytes + The MIT License (MIT) + + Copyright (c) Sindre Sorhus (sindresorhus.com) + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + **/ var format_filesize = function(num) { if (num === undefined) return; From 577cbe52b7369f9db67f0f5ed59eeda2efa4eea9 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Wed, 18 Apr 2018 12:35:16 -0400 Subject: [PATCH 22/26] update api description --- notebook/services/api/api.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/notebook/services/api/api.yaml b/notebook/services/api/api.yaml index 4cf8f9c9dc..dca988b59c 100644 --- a/notebook/services/api/api.yaml +++ b/notebook/services/api/api.yaml @@ -816,8 +816,7 @@ definitions: format: dateTime size: type: integer - description: "The size of the file or directory." - format: bytes + description: "The size of the file or notebook in bytes. If no size is provided, defaults to None." 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." From dee58e0a086bde53a661a9c526ff79c2f107e788 Mon Sep 17 00:00:00 2001 From: Ashley Teoh Date: Wed, 18 Apr 2018 12:47:06 -0400 Subject: [PATCH 23/26] use text() not html() --- notebook/static/tree/js/notebooklist.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index b1cd6e821e..7d8de5267e 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -865,7 +865,9 @@ 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) || " "); + + var filesize = utils.format_filesize(model.size); + item.find(".file_size").text(filesize || '\xA0'); }; From 174e72417493a7eb5ab5db0a9d99df0a4b0acb09 Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Wed, 18 Apr 2018 14:56:15 -0400 Subject: [PATCH 24/26] get file size in base model --- notebook/services/contents/filemanager.py | 21 ++++++++++----------- notebook/static/base/js/utils.js | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/notebook/services/contents/filemanager.py b/notebook/services/contents/filemanager.py index e0a5af37ef..6a4aa40c55 100644 --- a/notebook/services/contents/filemanager.py +++ b/notebook/services/contents/filemanager.py @@ -183,15 +183,6 @@ def is_hidden(self, path): 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. @@ -254,6 +245,14 @@ def _base_model(self, path): """Build the common base of a contents model""" os_path = self._get_os_path(path) info = os.lstat(os_path) + + try: + # size of file + size = info.st_size + except (ValueError, OSError): + self.log.warning('Unable to get size.') + size = None + try: last_modified = tz.utcfromtimestamp(info.st_mtime) except (ValueError, OSError): @@ -279,6 +278,7 @@ def _base_model(self, path): model['content'] = None model['format'] = None model['mimetype'] = None + model['size'] = size try: model['writable'] = os.access(os_path, os.W_OK) @@ -306,6 +306,7 @@ def _dir_model(self, path, content=True): model = self._base_model(path) model['type'] = 'directory' + model['size'] = None if content: model['content'] = contents = [] os_dir = self._get_os_path(path) @@ -359,7 +360,6 @@ def _file_model(self, path, content=True, format=None): 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) @@ -386,7 +386,6 @@ def _notebook_model(self, path, content=True): 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: nb = self._read_notebook(os_path, as_version=4) diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index 4d0d5c602a..d9846a7c5b 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1064,7 +1064,7 @@ define([ THE SOFTWARE. **/ var format_filesize = function(num) { - if (num === undefined) + if (num === undefined || num == null) return; var UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; From 192e3fe9d8dcbaec470017e024378e7ed084ad1f Mon Sep 17 00:00:00 2001 From: Celina Kilcrease Date: Wed, 18 Apr 2018 17:21:47 -0400 Subject: [PATCH 25/26] remove es6 syntax --- notebook/static/base/js/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index d9846a7c5b..26b657db22 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -1064,13 +1064,13 @@ define([ THE SOFTWARE. **/ var format_filesize = function(num) { - if (num === undefined || num == null) + if (num === undefined || num === null) return; 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}`); + console.error("Expected finite number, got ", typeof(num) + ": " + num); } var neg = num < 0; From 160754d27ef836683e87681fd6b5d34c5bdd9870 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Thu, 19 Apr 2018 09:13:01 +0200 Subject: [PATCH 26/26] None -> null for JSON API --- notebook/services/api/api.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebook/services/api/api.yaml b/notebook/services/api/api.yaml index dca988b59c..90dd85d8b0 100644 --- a/notebook/services/api/api.yaml +++ b/notebook/services/api/api.yaml @@ -816,7 +816,7 @@ definitions: format: dateTime size: type: integer - description: "The size of the file or notebook in bytes. If no size is provided, defaults to None." + description: "The size of the file or notebook in bytes. If no size is provided, defaults to null." 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."