122 lines
5.0 KiB
JavaScript
122 lines
5.0 KiB
JavaScript
app.torrent = (function(app){
|
|
let isDown = false;
|
|
|
|
$( document ).on( "ajaxSend", function(event, ajax, res, ...args) {
|
|
console.log('right?', res.url, res.url.startsWith('/__api/torrent'), app.torrent.isDown)
|
|
if(res.url.startsWith('/__api/torrent') && isDown){
|
|
ajax.abort()
|
|
}
|
|
// $( ".log" ).text( "Triggered ajaxStart handler." );
|
|
// throw new Error('go')
|
|
} );
|
|
|
|
statusMap = [
|
|
'Inactive', // 0
|
|
'CHECK_WAIT', // 1
|
|
'Verifying', // 2
|
|
'DOWNLOAD_WAIT', // 3
|
|
'Downloading', // 4
|
|
'SEED_WAIT', // 5
|
|
'Seeding', // 6
|
|
'ISOLATED', // 7
|
|
'Unknown', // 8
|
|
];
|
|
|
|
function list(callback, username) {
|
|
app.api.get('torrent', function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function get(callback, id, forceUpdate){
|
|
app.api.get(`torrent/${id}?${forceUpdate ? 'latest': '' }`, function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function start(id, callback){
|
|
app.api.post(`torrent/${id}/start`, function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function stop(id, callback){
|
|
app.api.post(`torrent/${id}/stop`, function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function destroy(id, callback){
|
|
app.api.delete(`torrent/${id}`, function(error, data){
|
|
if(error) return;
|
|
callback(null, data)
|
|
});
|
|
}
|
|
|
|
function parseServerStatus(data){
|
|
return {
|
|
...data,
|
|
"downloadSpeed": humanFileSize(data.downloadSpeed)+'/s',
|
|
"uploadSpeed": humanFileSize(data.uploadSpeed)+'/s',
|
|
/* "activeTorrentCount": 11,
|
|
"cumulative-stats": {
|
|
"downloadedBytes": 2925927098021,
|
|
"filesAdded": 80609,
|
|
"secondsActive": 12136579,
|
|
"sessionCount": 21,
|
|
"uploadedBytes": 107123787853
|
|
},
|
|
"current-stats": {
|
|
"downloadedBytes": 48440590262,
|
|
"filesAdded": 544,
|
|
"secondsActive": 111907,
|
|
"sessionCount": 1,
|
|
"uploadedBytes": 461874022
|
|
},
|
|
"pausedTorrentCount": 462,
|
|
"torrentCount": 473,
|
|
"__noSocket": true*/
|
|
}
|
|
}
|
|
|
|
function parseTorrnetItem(torrent){
|
|
let percentDone = (torrent.percentDone || 0)*100;
|
|
return {
|
|
...torrent,
|
|
"eta": torrent.eta > 0 ? moment().seconds(torrent.eta).fromNow() : 'Unknown',
|
|
"rateDownload": `${humanFileSize(torrent.rateDownload)}/s`,
|
|
"sizeWhenDone": humanFileSize(torrent.sizeWhenDone),
|
|
"percentDone": percentDone,
|
|
"statusText": statusMap[torrent.status],
|
|
"isActive": [3, 4, 5, 6].includes(torrent.status), // DOWNLOAD_WAIT ,DOWNLOAD, SEED_WAIT, SEED
|
|
"isFinished": torrent.isFinished || percentDone === 100,
|
|
"createdAtString": moment(torrent.createdAt).fromNow(),
|
|
|
|
// "isFinished": false,
|
|
// "isStalled": false,
|
|
// "name": "reacher.s02e06.1080p.web.h264-successfulcrab[EZTVx.to].mkv",
|
|
// "hashString": "4794a0915cada6c491eb5c910e1f4a0da727cac8",
|
|
// "status": 4,
|
|
// "id": 1,
|
|
|
|
// "peersConnected": 50,
|
|
// "added_by": "wmantly",
|
|
// "errorString": "",
|
|
|
|
// "downloadDir": "/media/torrents",
|
|
// "files": [],
|
|
// "peers": [],
|
|
// "magnetLink": "magnet:?xt=urn:btih:4794A0915CADA6C491EB5C910E1F4A0DA727CAC8&dn=Reacher+S02E06+1080p+WEB+H264-SuccessfulCrab&tr=http%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2F47.ip-51-68-199.eu%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.me%3A2780%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2710%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2730%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2920%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.cyberia.is%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.dler.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=udp%3A%2F%2Ftracker.pirateparty.gr%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.tiny-vps.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce",
|
|
// "isPrivate": false,
|
|
// "createdAt": "2024-01-05T21:18:30.607Z",
|
|
// "updatedAt": "2024-01-05T21:32:54.493Z"
|
|
// "torrent_id": "454",
|
|
}
|
|
}
|
|
|
|
return {list, get, start, stop, destroy, parseTorrnetItem, parseServerStatus, isDown};
|
|
})(app); |