Fix downloaded rounding showing 100% when not all pieces are downloaded (#764)

* Fix downloaded rounding showing 100% when not all pieces are downloaded

* Make suggested changes
This commit is contained in:
John Marinaro
2019-03-17 15:34:59 -06:00
committed by John Furrow
parent e62485d772
commit de4c6f5786
3 changed files with 11 additions and 3 deletions
+3 -2
View File
@@ -5,6 +5,7 @@ const config = require('../../config');
const formatUtil = require('../../shared/util/formatUtil');
const methodCallUtil = require('../util/methodCallUtil');
const serverEventTypes = require('../../shared/constants/serverEventTypes');
const truncateTo = require('../util/numberUtils');
const torrentListPropMap = require('../constants/torrentListPropMap');
const torrentServiceEvents = require('../constants/torrentServiceEvents');
const torrentStatusMap = require('../../shared/constants/torrentStatusMap');
@@ -116,9 +117,9 @@ class TorrentService extends BaseService {
const percentComplete = (torrentDetails.bytesDone / torrentDetails.sizeBytes) * 100;
if (percentComplete > 0 && percentComplete < 10) {
return Number(percentComplete.toFixed(2));
return Number(truncateTo(percentComplete, 2));
} else if (percentComplete > 10 && percentComplete < 100) {
return Number(percentComplete.toFixed(1));
return Number(truncateTo(percentComplete, 1));
}
return percentComplete;
+2 -1
View File
@@ -1,4 +1,5 @@
const geoip = require('geoip-lite');
const truncateTo = require('./numberUtils');
const torrentFilePropsMap = require('../../shared/constants/torrentFilePropsMap');
const torrentPeerPropsMap = require('../../shared/constants/torrentPeerPropsMap');
const torrentTrackerPropsMap = require('../../shared/constants/torrentTrackerPropsMap');
@@ -72,7 +73,7 @@ let clientResponseUtil = {
processFile(file) {
file.filename = file.pathComponents[file.pathComponents.length - 1];
file.percentComplete = ((file.completedChunks / file.sizeChunks) * 100).toFixed(0);
file.percentComplete = truncateTo((file.completedChunks / file.sizeChunks) * 100);
delete file.completedChunks;
delete file.pathComponents;
+6
View File
@@ -0,0 +1,6 @@
const truncateTo = (num, precision = 0) => {
const factor = 10 ** precision;
return Math.floor(num * factor) / factor;
};
module.exports = truncateTo;