This commit is contained in:
William Mantly 2024-01-06 15:10:39 -05:00
parent abc547c642
commit 7fad640cca
7 changed files with 252 additions and 97 deletions

View File

@ -5,9 +5,19 @@ const {Torrent} = require('>/models');
console.log('here!!!!!!!!!!!!!') console.log('here!!!!!!!!!!!!!')
let statusLock = false;
setInterval(async function(){ setInterval(async function(){
ps.publish('torrent:server:status', await Torrent.trClient.sessionStats()) if(statusLock) return;
}, 3000); 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){ // ps.subscribe(/./g, function(...args){

View File

@ -9,9 +9,6 @@ module.exports = {
primaryKey: true, primaryKey: true,
type: Sequelize.INTEGER type: Sequelize.INTEGER
}, },
torrent_id: {
type: Sequelize.STRING
},
hashString: { hashString: {
type: Sequelize.STRING type: Sequelize.STRING
}, },

View File

@ -6,14 +6,14 @@ const conf = require('>/conf');
const tr_client = new Transmission(conf.transmission) const tr_client = new Transmission(conf.transmission)
const statusMap = [ const statusMap = [
'STOPPED', 'STOPPED', // 0
'CHECK_WAIT', 'CHECK_WAIT', // 1
'CHECK', 'CHECK', // 2
'DOWNLOAD_WAIT', 'DOWNLOAD_WAIT', // 3
'DOWNLOAD', 'DOWNLOAD', // 4
'SEED_WAIT', 'SEED_WAIT', // 5
'SEED', 'SEED', // 6
'ISOLATED', 'ISOLATED', // 7
]; ];
module.exports = (sequelize, DataTypes, Model) => { module.exports = (sequelize, DataTypes, Model) => {
@ -41,7 +41,6 @@ module.exports = (sequelize, DataTypes, Model) => {
return await super.create({ return await super.create({
magnetLink: data.magnetLink, magnetLink: data.magnetLink,
torrent_id: res.id,
hashString: res.hashString, hashString: res.hashString,
name: res.name, name: res.name,
added_by: data.added_by, added_by: data.added_by,
@ -49,14 +48,14 @@ module.exports = (sequelize, DataTypes, Model) => {
percentDone: 0, percentDone: 0,
}, args); }, args);
}catch (error){ }catch (error){
// console.log('Torrent create error', error); console.log('Torrent create error', error);
throw error; throw error;
} }
} }
async getTorrentData(noUpdate){ async getTorrentData(noUpdate){
try{ try{
let res = ( await tr_client.get(Number(this.torrent_id), [ let res = ( await tr_client.get(this.hashString, [
"eta", "percentDone", "status", "rateDownload", "eta", "percentDone", "status", "rateDownload",
"errorString", "hashString", 'name', "errorString", "hashString", 'name',
'downloadDir', 'downloadDir',
@ -69,17 +68,38 @@ module.exports = (sequelize, DataTypes, Model) => {
'sizeWhenDone', 'sizeWhenDone',
]) ).torrents[0]; ]) ).torrents[0];
if(this.percentDone === 1) return this.dataValues
await this.update(res); await this.update(res);
if(noUpdate) await this.save(); if(noUpdate) await this.save();
return {...res, ...this.dataValues}; return {...res, ...this.dataValues};
}catch(error){ }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; 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.init({
torrent_id: DataTypes.STRING,
magnetLink: { magnetLink: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,

View File

@ -64,7 +64,7 @@ app.socket = (function(app){
if(data.__noSocket) return; if(data.__noSocket) return;
// console.log('local_pubs 2', data, topic) // 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){ app.api = (function(app){
var baseURL = '/__api/' 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){ function post(url, data, callback){
callback = callback || app.util.emptyFuction;
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: baseURL+url, url: baseURL+url,

View File

@ -95,7 +95,7 @@
//figure out new elements index //figure out new elements index
var key = I + index; var key = I + index;
// apply values to template // 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 //set call name and index keys to DOM element
var $render = $( render ).addClass( 'jq-repeat-'+ this.__repeatId ).attr( 'jq-repeat-index', key ); var $render = $( render ).addClass( 'jq-repeat-'+ this.__repeatId ).attr( 'jq-repeat-index', key );

View File

@ -110,7 +110,10 @@
padding-left: 0; padding-left: 0;
margin-bottom: 0; margin-bottom: 0;
} }
#tbp_proxy_torrent_dialog li{
padding-left: 1em;
padding-right: 1em;
}
</style> </style>
<div id="tbp_proxy_torrent_dialog" title="Torrents"> <div id="tbp_proxy_torrent_dialog" title="Torrents">
@ -120,8 +123,30 @@
{{^isFinished}} {{^isFinished}}
<progress id="file" max="100" value="{{percentDone}}">{{percentDone}}%</progress> <progress id="file" max="100" value="{{percentDone}}">{{percentDone}}%</progress>
<br /> <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}}
{{#isFinished}} {{#isFinished}}
<br /> Done! <a href="https://stuff.718it.biz/torrents/{{name}}" target="_blank"> HTTP Link</a> <br /> Done! <a href="https://stuff.718it.biz/torrents/{{name}}" target="_blank"> HTTP Link</a>
{{/isFinished}} {{/isFinished}}
@ -205,7 +230,9 @@
<div id="tbp_proxy_header_right"> <div id="tbp_proxy_header_right">
<span id="tbp_proxy_torrent_dialog_opener" class="tbp_proxy_is_authed"> <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;" /> <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> </span>
<button id="tbp_proxy_login_dialog_opener" class="tbp_proxy_not_authed ui-button ui-corner-all ui-widget">Login</button> <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" <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)) $.scope.tbp_proxy_torrent_dialog_torrents.unshift(app.torrent.parseTorrnetItem(data))
}); });
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(); listTorrents();
setInterval(refreshTorrents, 2000) 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){ function openDialog($el){
$el.parent().css({ $el.parent().css({
position:"fixed", 'margin-right': "2em", 'margin-top': '3em' position:"fixed", 'margin-right': "2em", 'margin-top': '3em'
@ -305,91 +354,127 @@
} }
app.torrent = (function(app){ 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 = [ statusMap = [
'Inactive', 'Inactive', // 0
'CHECK_WAIT', 'CHECK_WAIT', // 1
'Verifying', 'Verifying', // 2
'DOWNLOAD_WAIT', 'DOWNLOAD_WAIT', // 3
'Downloading', 'Downloading', // 4
'SEED_WAIT', 'SEED_WAIT', // 5
'Seeding', 'Seeding', // 6
'ISOLATED', 'ISOLATED', // 7
'Unknown', 'Unknown', // 8
]; ];
function list(callback, username) { 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){ 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 parseTorrnetItem(torrent){ function start(id, callback){
return { app.api.post(`torrent/${id}/start`, function(error, data){
...torrent, if(error) return;
"eta": torrent.eta > 0 ? moment().seconds(torrent.eta).fromNow() : 'Unknown', callback(null, data)
"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], function stop(id, callback){
// "createdAtString": moment(torrent.createdAt).fromNow(). app.api.post(`torrent/${id}/stop`, function(error, data){
// "downloadDir": "/media/torrents", if(error) return;
// "errorString": "", callback(null, data)
// "files": [], });
// "hashString": "4794a0915cada6c491eb5c910e1f4a0da727cac8", }
// "isFinished": false,
// "isStalled": false, function destroy(id, callback){
// "name": "reacher.s02e06.1080p.web.h264-successfulcrab[EZTVx.to].mkv", app.api.delete(`torrent/${id}`, function(error, data){
// "peers": [], if(error) return;
// "peersConnected": 50, callback(null, data)
// "status": 4, });
// "id": 1, }
// "torrent_id": "454",
// "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", function parseServerStatus(data){
// "isPrivate": false, return {
// "added_by": "wmantly", ...data,
// "createdAt": "2024-01-05T21:18:30.607Z", "downloadSpeed": humanFileSize(data.downloadSpeed)+'/s',
// "updatedAt": "2024-01-05T21:32:54.493Z" "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*/
} }
} }
return {list, get, parseTorrnetItem}; 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); })(app);
</script> </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"
},
} -->

View File

@ -5,7 +5,11 @@ const {Torrent} = require('>/models');
router.get('/', async function(req, res, next){ router.get('/', async function(req, res, next){
try{ 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){ }catch(error){
next(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; module.exports = router;