more...
This commit is contained in:
parent
abc547c642
commit
7fad640cca
@ -5,9 +5,19 @@ const {Torrent} = require('>/models');
|
||||
|
||||
console.log('here!!!!!!!!!!!!!')
|
||||
|
||||
let statusLock = false;
|
||||
setInterval(async function(){
|
||||
ps.publish('torrent:server:status', await Torrent.trClient.sessionStats())
|
||||
}, 3000);
|
||||
if(statusLock) return;
|
||||
statusLock = true;
|
||||
try{
|
||||
ps.publish('torrent:server:status', await Torrent.trClient.sessionStats());
|
||||
}catch(error){
|
||||
ps.publish('torrent:server:status:down')
|
||||
// if(error.code === 'ECONNREFUSED') throw new Error('TorrentGatewayDown');
|
||||
// console.error('status interval error', error)
|
||||
}
|
||||
statusLock = false
|
||||
}, 10000, statusLock);
|
||||
|
||||
|
||||
// ps.subscribe(/./g, function(...args){
|
||||
|
@ -9,9 +9,6 @@ module.exports = {
|
||||
primaryKey: true,
|
||||
type: Sequelize.INTEGER
|
||||
},
|
||||
torrent_id: {
|
||||
type: Sequelize.STRING
|
||||
},
|
||||
hashString: {
|
||||
type: Sequelize.STRING
|
||||
},
|
||||
|
@ -6,14 +6,14 @@ const conf = require('>/conf');
|
||||
const tr_client = new Transmission(conf.transmission)
|
||||
|
||||
const statusMap = [
|
||||
'STOPPED',
|
||||
'CHECK_WAIT',
|
||||
'CHECK',
|
||||
'DOWNLOAD_WAIT',
|
||||
'DOWNLOAD',
|
||||
'SEED_WAIT',
|
||||
'SEED',
|
||||
'ISOLATED',
|
||||
'STOPPED', // 0
|
||||
'CHECK_WAIT', // 1
|
||||
'CHECK', // 2
|
||||
'DOWNLOAD_WAIT', // 3
|
||||
'DOWNLOAD', // 4
|
||||
'SEED_WAIT', // 5
|
||||
'SEED', // 6
|
||||
'ISOLATED', // 7
|
||||
];
|
||||
|
||||
module.exports = (sequelize, DataTypes, Model) => {
|
||||
@ -41,7 +41,6 @@ module.exports = (sequelize, DataTypes, Model) => {
|
||||
|
||||
return await super.create({
|
||||
magnetLink: data.magnetLink,
|
||||
torrent_id: res.id,
|
||||
hashString: res.hashString,
|
||||
name: res.name,
|
||||
added_by: data.added_by,
|
||||
@ -49,14 +48,14 @@ module.exports = (sequelize, DataTypes, Model) => {
|
||||
percentDone: 0,
|
||||
}, args);
|
||||
}catch (error){
|
||||
// console.log('Torrent create error', error);
|
||||
console.log('Torrent create error', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getTorrentData(noUpdate){
|
||||
try{
|
||||
let res = ( await tr_client.get(Number(this.torrent_id), [
|
||||
let res = ( await tr_client.get(this.hashString, [
|
||||
"eta", "percentDone", "status", "rateDownload",
|
||||
"errorString", "hashString", 'name',
|
||||
'downloadDir',
|
||||
@ -69,17 +68,38 @@ module.exports = (sequelize, DataTypes, Model) => {
|
||||
'sizeWhenDone',
|
||||
]) ).torrents[0];
|
||||
|
||||
if(this.percentDone === 1) return this.dataValues
|
||||
await this.update(res);
|
||||
if(noUpdate) await this.save();
|
||||
return {...res, ...this.dataValues};
|
||||
}catch(error){
|
||||
console.error(`Torrent ${this.id} getTorrentData error`, error);
|
||||
if(error.code === 'ECONNREFUSED'){
|
||||
let e = new Error('TorrentGatewayDown')
|
||||
e.status = 555
|
||||
throw e
|
||||
}
|
||||
// console.error(`Torrent ${this.hashString} getTorrentData error`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async stop(){
|
||||
return await this.constructor.trClient.stop(this.hashString);
|
||||
}
|
||||
|
||||
async start(force){
|
||||
if(force) return await this.constructor.trClient.startNow(this.hashString);
|
||||
let res = await this.constructor.trClient.start(this.hashString);
|
||||
console.log('start', res);
|
||||
return res;
|
||||
}
|
||||
|
||||
async destroy(){
|
||||
await await this.constructor.trClient.remove(this.hashString, true);
|
||||
return await super.destroy()
|
||||
}
|
||||
}
|
||||
Torrent.init({
|
||||
torrent_id: DataTypes.STRING,
|
||||
magnetLink: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
|
@ -64,7 +64,7 @@ app.socket = (function(app){
|
||||
if(data.__noSocket) return;
|
||||
// console.log('local_pubs 2', data, topic)
|
||||
|
||||
socket.emit('P2PSub', { topic, data })
|
||||
socket.emit('P2PSub', { topic, data });
|
||||
});
|
||||
})
|
||||
|
||||
@ -75,7 +75,14 @@ app.socket = (function(app){
|
||||
app.api = (function(app){
|
||||
var baseURL = '/__api/'
|
||||
|
||||
|
||||
$( document ).on( "ajaxError", function(event, res, req) {
|
||||
console.log('bad!', `app:api:error:`, res.status);
|
||||
app.publish(`app:api:error:${res.status}`, {res, res});
|
||||
} );
|
||||
|
||||
function post(url, data, callback){
|
||||
callback = callback || app.util.emptyFuction;
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: baseURL+url,
|
||||
|
@ -95,7 +95,7 @@
|
||||
//figure out new elements index
|
||||
var key = I + index;
|
||||
// apply values to template
|
||||
var render = Mustache.render( this.__rq_template, toAdd[I] );
|
||||
var render = Mustache.render( this.__rq_template, {__id:I, ...toAdd[I]} );
|
||||
|
||||
//set call name and index keys to DOM element
|
||||
var $render = $( render ).addClass( 'jq-repeat-'+ this.__repeatId ).attr( 'jq-repeat-index', key );
|
||||
|
@ -110,7 +110,10 @@
|
||||
padding-left: 0;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
#tbp_proxy_torrent_dialog li{
|
||||
padding-left: 1em;
|
||||
padding-right: 1em;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div id="tbp_proxy_torrent_dialog" title="Torrents">
|
||||
@ -120,8 +123,30 @@
|
||||
{{^isFinished}}
|
||||
<progress id="file" max="100" value="{{percentDone}}">{{percentDone}}%</progress>
|
||||
<br />
|
||||
Status: <b>{{statusText}}</b> ETA: <b>{{eta}}</b> Rate: <b>{{rateDownload}}</b>
|
||||
<b>{{statusText}}</b> @ <b>{{rateDownload}}</b> finishing <b>{{eta}}</b>
|
||||
<br />
|
||||
{{^isActive}}
|
||||
<button class="ui-button ui-widget ui-corner-all" onclick="app.torrent.start({{id}})">
|
||||
<span class="ui-icon ui-icon-play"></span> Start
|
||||
</button>
|
||||
{{/isActive}}
|
||||
|
||||
{{#isActive}}
|
||||
<button class="ui-button ui-widget ui-corner-all" onclick="app.torrent.stop({{id}})">
|
||||
<span class="ui-icon ui-icon-pause"></span>Pause
|
||||
</button>
|
||||
{{/isActive}}
|
||||
|
||||
<button class="ui-button ui-widget ui-corner-all" onclick="
|
||||
app.torrent.destroy({{id}}, function(error, data){
|
||||
$.scope.tbp_proxy_torrent_dialog_torrents.splice({{__id}}, 1);
|
||||
});
|
||||
">
|
||||
<span class="ui-icon ui-icon-closethick"></span>Delete
|
||||
</button>
|
||||
|
||||
{{/isFinished}}
|
||||
|
||||
{{#isFinished}}
|
||||
<br /> Done! <a href="https://stuff.718it.biz/torrents/{{name}}" target="_blank"> HTTP Link</a>
|
||||
{{/isFinished}}
|
||||
@ -205,7 +230,9 @@
|
||||
<div id="tbp_proxy_header_right">
|
||||
<span id="tbp_proxy_torrent_dialog_opener" class="tbp_proxy_is_authed">
|
||||
<img src="/__static/img/Transmission_Icon.svg" height="22" width="22" style="margin-right: .3em;" />
|
||||
<span><b>106</b></span>
|
||||
<span jq-repeat="tbp_proxy_torrent_dialog_opener_status">
|
||||
<b> <span class="ui-icon ui-icon-arrowthick-1-n"></span>{{downloadSpeed}} <span class="ui-icon ui-icon-arrowthick-1-s"></span>{{uploadSpeed}}</b>
|
||||
</span>
|
||||
</span>
|
||||
<button id="tbp_proxy_login_dialog_opener" class="tbp_proxy_not_authed ui-button ui-corner-all ui-widget">Login</button>
|
||||
<button class="tbp_proxy_is_authed ui-button ui-corner-all ui-widget"
|
||||
@ -273,10 +300,32 @@
|
||||
$.scope.tbp_proxy_torrent_dialog_torrents.unshift(app.torrent.parseTorrnetItem(data))
|
||||
});
|
||||
|
||||
listTorrents();
|
||||
setInterval(refreshTorrents, 2000)
|
||||
app.subscribe('torrent:server:status', function(data, topic){
|
||||
app.torrent.isDown = false
|
||||
$('#tbp_proxy_torrent_dialog_opener').css('background', "lightseagreen")
|
||||
$.scope.tbp_proxy_torrent_dialog_opener_status.update(app.torrent.parseServerStatus(data));
|
||||
});
|
||||
|
||||
app.subscribe(`app:api:error:555`, function(data, topics){
|
||||
console.log('we down')
|
||||
app.torrent.isDown = true
|
||||
$('#tbp_proxy_torrent_dialog_opener').css('background', "red")
|
||||
});
|
||||
|
||||
app.subscribe('torrent:server:status:down', function(data, topic){
|
||||
app.torrent.isDown = true
|
||||
$('#tbp_proxy_torrent_dialog_opener').css('background', "red")
|
||||
});
|
||||
|
||||
listTorrents();
|
||||
setInterval(refreshTorrents, 5000)
|
||||
});
|
||||
|
||||
function humanFileSize(size) {
|
||||
var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
|
||||
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
|
||||
}
|
||||
|
||||
function openDialog($el){
|
||||
$el.parent().css({
|
||||
position:"fixed", 'margin-right': "2em", 'margin-top': '3em'
|
||||
@ -305,91 +354,127 @@
|
||||
}
|
||||
|
||||
app.torrent = (function(app){
|
||||
let myTorrents = [];
|
||||
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',
|
||||
'CHECK_WAIT',
|
||||
'Verifying',
|
||||
'DOWNLOAD_WAIT',
|
||||
'Downloading',
|
||||
'SEED_WAIT',
|
||||
'Seeding',
|
||||
'ISOLATED',
|
||||
'Unknown',
|
||||
'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', callback);
|
||||
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': '' }`, callback);
|
||||
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": `${Math.floor(torrent.rateDownload/1024/1024)}mb/s`,
|
||||
"sizeWhenDone": `${Math.floor(torrent.sizeWhenDone/1024/1024)}mb/s`,
|
||||
"percentDone": torrent.percentDone*100,
|
||||
"statusText": statusMap[torrent.status || 8],
|
||||
// "createdAtString": moment(torrent.createdAt).fromNow().
|
||||
// "downloadDir": "/media/torrents",
|
||||
// "errorString": "",
|
||||
// "files": [],
|
||||
// "hashString": "4794a0915cada6c491eb5c910e1f4a0da727cac8",
|
||||
"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",
|
||||
// "peers": [],
|
||||
// "peersConnected": 50,
|
||||
// "hashString": "4794a0915cada6c491eb5c910e1f4a0da727cac8",
|
||||
// "status": 4,
|
||||
// "id": 1,
|
||||
// "torrent_id": "454",
|
||||
|
||||
// "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,
|
||||
// "added_by": "wmantly",
|
||||
// "createdAt": "2024-01-05T21:18:30.607Z",
|
||||
// "updatedAt": "2024-01-05T21:32:54.493Z"
|
||||
// "torrent_id": "454",
|
||||
}
|
||||
}
|
||||
|
||||
return {list, get, parseTorrnetItem};
|
||||
return {list, get, start, stop, destroy, parseTorrnetItem, parseServerStatus, isDown};
|
||||
})(app);
|
||||
|
||||
</script>
|
||||
|
||||
<!-- {
|
||||
"result": {
|
||||
"downloadDir": "/media/torrents",
|
||||
"errorString": "",
|
||||
"eta": -2,
|
||||
"files": [],
|
||||
"hashString": "a7ff69e500f0b62c3dec1061b9998610385dc7b6",
|
||||
"isFinished": false,
|
||||
"isStalled": true,
|
||||
"name": "Aftershock.2012.LIMITED.720p.BluRay.x264-GECKOS+[PublicHD]",
|
||||
"peers": [],
|
||||
"peersConnected": 0,
|
||||
"percentDone": 0,
|
||||
"rateDownload": 0,
|
||||
"sizeWhenDone": 0,
|
||||
"status": 4,
|
||||
"dataValues": {
|
||||
"id": 1,
|
||||
"torrent_id": "451",
|
||||
"magnetLink": "magnet:?xt=urn:btih:DF0146FBD120793246BE3D29ADD11BC7D460BFBB&dn=Magnum+P+I+2018+S05E19+720p+HDTV+x264-SYNCOPY&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",
|
||||
"hashString": "a7ff69e500f0b62c3dec1061b9998610385dc7b6",
|
||||
"name": "Aftershock.2012.LIMITED.720p.BluRay.x264-GECKOS+[PublicHD]",
|
||||
"added_by": "wmantly",
|
||||
"status": 4,
|
||||
"percentDone": 0,
|
||||
"downloadDir": "/media/torrents",
|
||||
"errorString": "",
|
||||
"sizeWhenDone": 0,
|
||||
"createdAt": "2024-01-04T17:40:58.187Z",
|
||||
"updatedAt": "2024-01-05T19:30:37.676Z"
|
||||
},
|
||||
} -->
|
@ -5,7 +5,11 @@ const {Torrent} = require('>/models');
|
||||
|
||||
router.get('/', async function(req, res, next){
|
||||
try{
|
||||
res.json({results: await Torrent.findAll({where:{added_by: req.user.username}})});
|
||||
res.json({results: await Torrent.findAll({
|
||||
where:{added_by: req.query.username || req.user.username},
|
||||
limit: req.query.limit,
|
||||
offset: req.query.offset
|
||||
})});
|
||||
}catch(error){
|
||||
next(error);
|
||||
}
|
||||
@ -39,4 +43,36 @@ router.get("/:id", async function(req, res, next){
|
||||
}
|
||||
});
|
||||
|
||||
router.delete("/:id", async function(req, res, next){
|
||||
try{
|
||||
let torrent = await Torrent.findByPk(req.params.id);
|
||||
|
||||
res.json({result: torrent, activity: await torrent.destroy()});
|
||||
}catch(error){
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/:id/stop", async function(req, res, next){
|
||||
try{
|
||||
let torrent = await Torrent.findByPk(req.params.id);
|
||||
|
||||
res.json({result: torrent, activity: await torrent.stop()});
|
||||
}catch(error){
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/:id/start", async function(req, res, next){
|
||||
try{
|
||||
let torrent = await Torrent.findByPk(req.params.id);
|
||||
|
||||
res.json({result: torrent, activity: await torrent.start()});
|
||||
}catch(error){
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
Loading…
x
Reference in New Issue
Block a user