-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding file size to views #3539
Changes from 21 commits
3650cc7
b864f5a
3315dbd
388d636
38acc78
59b7c66
86c45a1
a299be7
6475eaa
d6c7f2e
42c3c02
8c96ad5
d19f970
a47a188
ff8e1cc
abe05de
13933f3
acc2056
2e9ba4a
8cc9734
06214ae
d35ac8b
dd608ad
577cbe5
dee58e0
174e724
192e3fe
160754d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -814,6 +814,10 @@ definitions: | |
type: string | ||
description: Last modified timestamp | ||
format: dateTime | ||
size: | ||
type: integer | ||
description: "The size of the file or directory." | ||
format: bytes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should just go in the description. Swagger specifies a fixed list of formats for things like |
||
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." | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,6 +183,15 @@ 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will do a new stat on the file, which is best avoided for performance reasons. We already get a stat object in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @takluyver thank you -- I've updated so that it uses file size from |
||
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. | ||
|
||
|
@@ -270,6 +279,7 @@ def _base_model(self, path): | |
model['content'] = None | ||
model['format'] = None | ||
model['mimetype'] = None | ||
|
||
try: | ||
model['writable'] = os.access(os_path, os.W_OK) | ||
except OSError: | ||
|
@@ -333,6 +343,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 | ||
|
||
|
@@ -348,6 +359,7 @@ 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) | ||
|
@@ -373,13 +385,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' | ||
self.validate_notebook_model(model) | ||
|
||
return model | ||
|
||
def get(self, path, content=True, type=None, format=None): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1038,7 +1038,34 @@ define([ | |
return (order == 1) ? 1 : -1; | ||
} | ||
}; | ||
|
||
// source: https://github.com/sindresorhus/pretty-bytes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm OK with this since it's one fairly small function, and it's unlikely to change. But we should include a copy of the license in the comment. https://github.com/sindresorhus/pretty-bytes/blob/master/license |
||
var format_filesize = function(num) { | ||
if (num === undefined) | ||
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}`); | ||
} | ||
|
||
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", | ||
// which stores high-codepoint characters as "surrogate pairs", | ||
|
@@ -1180,6 +1207,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, | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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,11 @@ define([ | |||
.addClass("item_name") | ||||
.appendTo(link); | ||||
|
||||
$("<span/>") | ||||
.addClass("file_size") | ||||
.addClass("pull-right") | ||||
.appendTo(item); | ||||
|
||||
$("<span/>") | ||||
.addClass("item_modified") | ||||
.addClass("pull-right") | ||||
|
@@ -835,6 +865,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) || " "); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the use of If somewhere where to replace you API to return If you've seen use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re: using it's also used here, which i'm assuming the author also wanted to use the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question, there are several way to approach that. A simple one would be to use
That reduce the use of html so it's one less risk. Thinking about it more,
If you want to even avoid using jQuery you can use the With your gained knowledge you can even open an issue for As an exercise – if you like the fun of hacking – get in a shoes of an attacker and try to modify the server side to not return the size as an integer but a string, and attempt to have a script injection that display a message in the frontend. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stepping back a bit from unicode discussions: do we actually need a non-breaking space at all? I think the layout should still work if the element has no text. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed it with using |
||||
}; | ||||
|
||||
|
||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Integer or None ? If size can't be found, probably say that in the description at least.