-
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 27 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,9 @@ definitions: | |
type: string | ||
description: Last modified timestamp | ||
format: dateTime | ||
size: | ||
type: integer | ||
description: "The size of the file or notebook in bytes. If no size is provided, defaults to None." | ||
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. Since this is describing a JSON API, it's probably better to refer to null, which is the JSON/Javascript name, rather than None, which is from Python. This isn't worth holding the PR up for, so I'll change it myself and then merge. |
||
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 |
---|---|---|
|
@@ -245,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): | ||
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 don't think there can be an error here - if |
||
self.log.warning('Unable to get size.') | ||
size = None | ||
|
||
try: | ||
last_modified = tz.utcfromtimestamp(info.st_mtime) | ||
except (ValueError, OSError): | ||
|
@@ -270,6 +278,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: | ||
|
@@ -296,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) | ||
|
@@ -333,6 +344,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 | ||
|
||
|
@@ -373,13 +385,15 @@ def _notebook_model(self, path, content=True): | |
""" | ||
model = self._base_model(path) | ||
model['type'] = 'notebook' | ||
os_path = self._get_os_path(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,57 @@ define([ | |
return (order == 1) ? 1 : -1; | ||
} | ||
}; | ||
|
||
/** | ||
source: https://github.com/sindresorhus/pretty-bytes | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Sindre Sorhus <[email protected]> (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 || num === null) | ||
return; | ||
|
||
var UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
|
||
if (!Number.isFinite(num)) { | ||
console.error("Expected 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 +1230,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, | ||
|
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.