Skip to content

Commit

Permalink
fixed filesize
Browse files Browse the repository at this point in the history
fixed  typo
  • Loading branch information
ckilcrease authored and ashleytqy committed Apr 18, 2018
1 parent 42c3c02 commit 2ef8962
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
2 changes: 1 addition & 1 deletion notebook/services/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
12 changes: 2 additions & 10 deletions notebook/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand Down
28 changes: 11 additions & 17 deletions notebook/static/base/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2ef8962

Please sign in to comment.