2014-06-02 21:36:21 +02:00
|
|
|
angular.module('webui.services.utils', ['webui.services.configuration'])
|
2014-05-17 15:46:11 +02:00
|
|
|
.factory('$utils', ['$filter', "$name", function(filter, $name) {
|
2013-07-31 22:07:50 +02:00
|
|
|
var rnd16 = (function() {
|
|
|
|
"use strict";
|
2013-08-18 02:13:29 +02:00
|
|
|
var rndBuffer = new Uint8Array(16);
|
2013-07-31 22:07:50 +02:00
|
|
|
var rnd16Weak = function() {
|
|
|
|
for (var i = 0, r; i < 16; i++) {
|
|
|
|
if (!(i % 0x3)) r = Math.random() * 0x100000000 | 0;
|
|
|
|
rndBuffer[i] = r >>> ((i & 0x3) << 0x3) & 0xff;
|
|
|
|
}
|
|
|
|
return rndBuffer;
|
|
|
|
};
|
|
|
|
|
2014-03-14 13:57:57 +01:00
|
|
|
if (!window.crypto || !crypto.getRandomValues) {
|
2013-07-31 22:07:50 +02:00
|
|
|
return rnd16Weak;
|
|
|
|
}
|
|
|
|
return function() {
|
|
|
|
try {
|
|
|
|
crypto.getRandomValues(rndBuffer);
|
|
|
|
return rndBuffer;
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
// Entropy might be exhausted
|
|
|
|
return rnd16Weak();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2014-02-24 02:50:23 +01:00
|
|
|
var utils = {
|
|
|
|
|
|
|
|
fmtsize: function(len) {
|
|
|
|
len = +len; // coerce to number
|
|
|
|
if (len <= 1024) {
|
|
|
|
return len.toFixed(0) + " B";
|
|
|
|
}
|
|
|
|
len /= 1024;
|
|
|
|
if (len <= 1024) {
|
|
|
|
return len.toFixed(1) + " KB"
|
|
|
|
}
|
|
|
|
len /= 1024;
|
|
|
|
if (len <= 1024) {
|
|
|
|
return len.toFixed(2) + " MB";
|
|
|
|
}
|
|
|
|
len /= 1024;
|
|
|
|
return len.toFixed(3) + " GB";
|
|
|
|
},
|
|
|
|
|
|
|
|
fmtspeed: function(speed) {
|
|
|
|
return utils.fmtsize(speed) + "/s";
|
|
|
|
},
|
2013-03-11 19:16:48 +01:00
|
|
|
// saves the key value pair in cookies
|
|
|
|
setCookie: function(key, value) {
|
|
|
|
var exdate = new Date();
|
|
|
|
exdate.setDate(exdate.getDate() + 30 * 12);
|
|
|
|
var cvalue = escape(JSON.stringify(value)) + "; expires=" + exdate.toUTCString();
|
|
|
|
document.cookie = key + "=" + cvalue;
|
|
|
|
},
|
|
|
|
// gets a value for a key stored in cookies
|
|
|
|
getCookie: function(key) {
|
|
|
|
var chunks = document.cookie.split(";");
|
|
|
|
for (var i = 0; i < chunks.length; i++) {
|
|
|
|
var ckey = chunks[i].substr(0, chunks[i].indexOf("=")).replace(/^\s+|\s+$/g,"");
|
|
|
|
var cvalue = chunks[i].substr(chunks[i].indexOf("=") + 1);
|
|
|
|
if (key == ckey) {
|
|
|
|
return JSON.parse(unescape(cvalue));
|
|
|
|
}
|
|
|
|
}
|
2013-06-22 22:37:14 +02:00
|
|
|
|
|
|
|
return null;
|
2013-03-11 19:16:48 +01:00
|
|
|
},
|
2013-01-17 17:18:23 +01:00
|
|
|
getFileName: function(path) {
|
|
|
|
var seed = path.split(/[/\\]/);
|
|
|
|
return seed[seed.length - 1];
|
|
|
|
},
|
2013-07-31 22:07:50 +02:00
|
|
|
uuid: (function() {
|
|
|
|
var bt = [];
|
|
|
|
for (var i = 0; i < 0x100; ++i) {
|
|
|
|
bt.push((i + 0x100).toString(16).substr(1));
|
2013-01-15 09:24:09 +01:00
|
|
|
}
|
2013-07-31 22:07:50 +02:00
|
|
|
Object.freeze(bt);
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
var r = rnd16();
|
|
|
|
r[6] = (r[6] & 0xf) | 0x40; // Version 4
|
2014-03-14 13:57:57 +01:00
|
|
|
r[8] = (r[8] & 0x3f) | 0x80; // Version 4y
|
2013-07-31 22:07:50 +02:00
|
|
|
return bt[r[0]] + bt[r[1]] + bt[r[2]] + bt[r[3]] + "-" +
|
|
|
|
bt[r[4]] + bt[r[5]] + "-" +
|
|
|
|
bt[r[6]] + bt[r[7]] + "-" +
|
|
|
|
bt[r[8]] + bt[r[9]] + "-" +
|
|
|
|
bt[r[10]] + bt[r[11]] + bt[r[12]] + bt[r[13]] + bt[r[14]] + bt[r[15]];
|
|
|
|
};
|
|
|
|
})(),
|
|
|
|
randStr: function() {
|
2014-02-24 02:50:23 +01:00
|
|
|
return utils.uuid();
|
2013-01-15 09:24:09 +01:00
|
|
|
},
|
2013-01-17 07:51:03 +01:00
|
|
|
|
2013-01-21 15:07:55 +01:00
|
|
|
// maps the array in place to the destination
|
|
|
|
// arr, dest (optional): array
|
|
|
|
// func: a merge mapping func, see ctrls/download.js
|
|
|
|
mergeMap: function(arr, dest, func) {
|
2014-02-24 02:50:23 +01:00
|
|
|
if (!dest) {
|
|
|
|
dest = [];
|
|
|
|
}
|
2013-01-21 15:07:55 +01:00
|
|
|
|
2014-02-24 02:50:23 +01:00
|
|
|
for (var i = 0, e = Math.min(arr.length, dest.length); i < e; ++i) {
|
2013-01-21 15:07:55 +01:00
|
|
|
func(arr[i], dest[i]);
|
|
|
|
}
|
|
|
|
|
2014-02-24 02:50:23 +01:00
|
|
|
// Insert newly created downloads
|
2013-01-21 15:07:55 +01:00
|
|
|
while (i < arr.length) {
|
|
|
|
dest.push(func(arr[i++]));
|
|
|
|
}
|
|
|
|
|
2014-02-24 02:50:23 +01:00
|
|
|
// Truncate if necessary.
|
|
|
|
dest.length = arr.length;
|
|
|
|
|
2013-01-21 15:07:55 +01:00
|
|
|
return dest;
|
|
|
|
},
|
2013-07-17 16:10:24 +02:00
|
|
|
// get info title from global statistics
|
|
|
|
getTitle: function(stats) {
|
|
|
|
var title =
|
2014-05-17 15:46:11 +02:00
|
|
|
'active: ' + stats.numActive
|
|
|
|
+ ' - waiting: ' + stats.numWaiting
|
|
|
|
+ ' - stopped: ' + stats.numStopped
|
|
|
|
+ ' — '
|
|
|
|
+ $name;
|
2013-07-17 16:10:24 +02:00
|
|
|
|
|
|
|
return title;
|
|
|
|
},
|
2013-01-17 07:51:03 +01:00
|
|
|
|
|
|
|
// get download chunks from aria2 bitfield
|
|
|
|
getChunksFromHex: function(bitfield, numOfPieces) {
|
|
|
|
var chunks = [], len = 0, numPieces = parseInt(numOfPieces);
|
2013-01-22 13:11:38 +01:00
|
|
|
if (!bitfield) return [];
|
|
|
|
|
2013-01-17 07:51:03 +01:00
|
|
|
var totalDownloaded = 0;
|
|
|
|
if (numPieces > 1) {
|
|
|
|
var chunk_ratio = 1 / numPieces;
|
|
|
|
var piecesProcessed = 0;
|
|
|
|
for (var i = 0; i < bitfield.length; i++) {
|
|
|
|
var hex = parseInt(bitfield[i], 16);
|
|
|
|
for (var j = 1; j <= 4; j++) {
|
|
|
|
var bit = hex & (1 << (4 - j));
|
|
|
|
if (bit) totalDownloaded++;
|
|
|
|
var prog = !!bit;
|
|
|
|
if (len >= 1 && chunks[len - 1].show == prog) {
|
|
|
|
chunks[len - 1].ratio += chunk_ratio;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
chunks.push({
|
|
|
|
ratio: chunk_ratio,
|
|
|
|
show: prog
|
|
|
|
});
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
piecesProcessed++;
|
|
|
|
if (piecesProcessed == numPieces)
|
|
|
|
return chunks;
|
|
|
|
}
|
2013-01-15 09:24:09 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-17 07:51:03 +01:00
|
|
|
return chunks;
|
2013-01-15 09:24:09 +01:00
|
|
|
}
|
|
|
|
};
|
2014-02-24 02:50:23 +01:00
|
|
|
return utils;
|
2013-07-17 16:10:24 +02:00
|
|
|
}]);
|