Commit 68b106ce authored by Mikaël Salson's avatar Mikaël Salson
Browse files

Server: Remove duplicated code for formatting filesize

Filesize is given in bytes (B) not in octets ;)
And the K, M, G prefixes are international standards for
10^3, 10^6 and 10^9 not 2^10, 2^20 and 2^30 ;)
parent 486e8997
......@@ -2,7 +2,6 @@
import gluon.contrib.simplejson
import os.path
import defs
import math
if request.env.http_origin:
response.headers['Access-Control-Allow-Origin'] = request.env.http_origin
response.headers['Access-Control-Allow-Credentials'] = 'true'
......@@ -30,12 +29,7 @@ def repair_missing_files():
db.sequence_file[row.id] = dict(data_file = None)
flist += " : " + row.filename
else :
size = os.path.getsize(seq_file)
size = math.floor((size/1024)/1024)
if size > 1024 :
size = str( round( (size/1024), 3 ) ) + " Go"
else :
size = str( math.floor(size) ) + " Mo"
size = defs.format_size(os.path.getsize(seq_file))
db.sequence_file[row.id] = dict(size_file = size)
res = {"success" : "true", "message" : "path of missing files have been removed from the database"+flist}
......
......@@ -2,7 +2,6 @@
import gluon.contrib.simplejson
import defs
import os
import math
if request.env.http_origin:
response.headers['Access-Control-Allow-Origin'] = request.env.http_origin
response.headers['Access-Control-Allow-Credentials'] = 'true'
......@@ -130,12 +129,7 @@ def upload():
mes = f.filename + ": upload finished"
seq_file = defs.DIR_SEQUENCES+db.sequence_file[request.vars["id"]].data_file
size = os.path.getsize(seq_file)
size = math.floor((size/1024)/1024)
if size > 1024 :
size = str( round( (size/1024), 3 ) ) + " Go"
else :
size = str( math.floor(size) ) + " Mo"
size = defs.format_size(os.path.getsize(seq_file))
db.sequence_file[request.vars["id"]] = dict(size_file = size)
res = {"message": mes}
......
import math
### Upload directory for .fasta/.fastq.
### Old sequences files could be thrown away.
......@@ -16,3 +16,20 @@ DIR_RESULTS = '/mnt/result/results/'
DIR_OUT_VIDJIL_ID = '/mnt/result/tmp/out-%06d/'
def format_size(n):
'''
Takes an integer n, representing a filesize and returns a string
where the size is formatted in M, G, ...
Example:
>>> format_size(1000000)
1.0 MB
>>> format_size(1024*1014*1024)
1.073 GB
'''
size = math.floor((n/1000)/1000)
if size > 1000 :
size = str( round( (size/1000), 3 ) ) + " GB"
else :
size = str( math.floor(size) ) + " MB"
return size
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment