From 1c7e2e794e61991e30cd360665820b7cda60b649 Mon Sep 17 00:00:00 2001 From: William Mantly Date: Sun, 7 Jan 2024 00:30:53 -0500 Subject: [PATCH] works --- .../20240101174644-create-torrent.js | 8 +- models/sql/torrent.js | 44 +- public/lib/js/jq-repeat.js | 35 +- public/partial/header.html | 845 ++++++++++-------- routes/transmission.js | 29 +- 5 files changed, 548 insertions(+), 413 deletions(-) diff --git a/models/sql/migrations/20240101174644-create-torrent.js b/models/sql/migrations/20240101174644-create-torrent.js index a423528..eaedd07 100644 --- a/models/sql/migrations/20240101174644-create-torrent.js +++ b/models/sql/migrations/20240101174644-create-torrent.js @@ -3,13 +3,9 @@ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable('Torrents', { - id: { - allowNull: false, - autoIncrement: true, - primaryKey: true, - type: Sequelize.INTEGER - }, hashString: { + primaryKey: true, + allowNull: false, type: Sequelize.STRING }, magnetLink: { diff --git a/models/sql/torrent.js b/models/sql/torrent.js index 7d60f9a..8c238e1 100644 --- a/models/sql/torrent.js +++ b/models/sql/torrent.js @@ -53,12 +53,47 @@ module.exports = (sequelize, DataTypes, Model) => { } } + static async migrate(hashString, username){ + try{ + let exists = await this.findByPk(hashString); + + if(exists){ + console.log('torrent in DB, skipping') + return {} + } + + let res = ( await tr_client.get(hashString, [ + "eta", "percentDone", "status", "rateDownload", + "errorString", "hashString", 'name', + 'downloadDir', + 'addedDate', + 'magnetLink', + 'files', //array of files + 'filesStats', // array of files with status + 'isFinished', + 'isStalled', + 'peers', + 'peersConnected', // array of peers, + 'sizeWhenDone', + ]) ).torrents[0]; + + // console.log('date:', res.addedDate, new Date(res.addedDate*1000), 'res:', res) + + let instance = await this.build({createdAt: new Date(res.addedDate*1000), ...res, added_by: username}); + await instance.save(); + return {...res, ...instance.dataValues}; + }catch(error){ + console.error('migrate error', error); + } + } + async getTorrentData(noUpdate){ try{ let res = ( await tr_client.get(this.hashString, [ "eta", "percentDone", "status", "rateDownload", "errorString", "hashString", 'name', 'downloadDir', + 'dateCreated', 'files', //array of files 'filesStats', // array of files with status 'isFinished', @@ -100,6 +135,11 @@ module.exports = (sequelize, DataTypes, Model) => { } } Torrent.init({ + hashString: { + type: DataTypes.STRING, + allowNull: false, + primaryKey: true + }, magnetLink: { type: DataTypes.STRING, allowNull: false, @@ -112,7 +152,6 @@ module.exports = (sequelize, DataTypes, Model) => { type: DataTypes.BOOLEAN, defaultValue: false, }, - hashString: DataTypes.STRING, name: DataTypes.STRING, added_by: { type: DataTypes.STRING, @@ -137,6 +176,9 @@ module.exports = (sequelize, DataTypes, Model) => { type: DataTypes.NUMBER, allowNull: true, }, + createdAt: { + type: DataTypes.DATE + }, }, { sequelize, modelName: 'Torrent', diff --git a/public/lib/js/jq-repeat.js b/public/lib/js/jq-repeat.js index 48b9897..ca1a2fe 100644 --- a/public/lib/js/jq-repeat.js +++ b/public/lib/js/jq-repeat.js @@ -7,7 +7,7 @@ var make = function( element ){ //construct array - function makeArray( input ){ + function makeArray( input , index ){ var result = []; @@ -25,6 +25,20 @@ configurable: true } ); + Object.defineProperty( result, "__jq_index", { + value: index, + writable: true, + enumerable: false, + configurable: true + } ); + + function removeEmpty(){ + if(result.__jq_empty){ + result.__jq_empty.remove(); + delete result.__jq_empty; + } + } + result.splice = function(inputValue, ...args){ //splice does all the heavy lifting by interacting with the DOM elements. @@ -88,6 +102,7 @@ //if there are fields to add to the array, add them if( toAdd.length > 0 ){ + removeEmpty() //$.each( toAdd, function( key, value ){ for(var I = 0; I < toAdd.length; I++){ @@ -176,7 +191,7 @@ result.indexOf = function( key, value ){ if( !value ){ value = arguments[0]; - key = this.__index; + key = this.__jq_index; } for ( var index = 0; index < this.length; ++index ) { if( this[index][key] === value ){ @@ -200,7 +215,7 @@ if( !update ){ update = arguments[1]; value = arguments[0]; - key = this.__index; + key = this.__jq_index; } var index = this.indexOf( key, value ); @@ -226,7 +241,7 @@ if( type === 'object' ){ result.push( value ); }else if( type === 'string' ){ - Object.defineProperty( result, "__index", { + Object.defineProperty( result, "__jq_index", { value: value, writable: true, enumerable: false, @@ -248,19 +263,23 @@ var $this = $( element ); var repeatId = $this.attr( 'jq-repeat' ); + var index = $this.attr( 'jq-repeat-index' ); var tempId = repeatId + 'Template'; var templateId = $( '#' + tempId ).html(); + var empty = $(`[jq-repeat-defualt="${repeatId}"]`); + $this.removeAttr( 'jq-repeat' ); - var template = element.outerHTML + $this.removeAttr( 'jq-repeat-index' ); + var template = element.outerHTML $this.replaceWith( '
- -
-
- + +
+ + -
- -
-
- -
- -
-
+
+ +
+
+ +
+ +
+
-
- -
-
- -
- -
-
+
+ +
+
+ +
+ +
+
- - -
+ + +
-
-
- -
-
-

- -
- -

+
+ +
+ +

+ +
+ +

-

- -
- -

+

+ +
+ +

-

- Public Download: +

+ +
+ +

- - +

+ Public Download: - - -

+ + -

- Start on add: + + +

- - +

+ Start on add: - - -

+ + -
- - - -
-
+ + +

+ +
+ + + +
+
- - - - {{downloadSpeed}} {{uploadSpeed}} - - - - + + + + {{downloadSpeed}} {{uploadSpeed}} + + + +
diff --git a/routes/transmission.js b/routes/transmission.js index 648ba34..3bfcdab 100644 --- a/routes/transmission.js +++ b/routes/transmission.js @@ -8,7 +8,10 @@ router.get('/', async function(req, res, next){ res.json({results: await Torrent.findAll({ where:{added_by: req.query.username || req.user.username}, limit: req.query.limit, - offset: req.query.offset + offset: req.query.offset, + order: [ + ['createdAt', 'DESC'], + ], })}); }catch(error){ next(error); @@ -23,6 +26,14 @@ router.post("/", async function(req, res, next){ } }); +router.post("/:hashString", async function(req, res, next){ + try{ + res.json(await Torrent.migrate(req.params.hashString, req.user.username)) + }catch(error){ + next(error); + } +}); + router.get('/server', async function(req, res, next){ try{ res.json(await Torrent.trClient.sessionStats()) @@ -31,9 +42,9 @@ router.get('/server', async function(req, res, next){ } }); -router.get("/:id", async function(req, res, next){ +router.get("/:hashString", async function(req, res, next){ try{ - let torrent = await Torrent.findByPk(req.params.id); + let torrent = await Torrent.findByPk(req.params.hashString); if('latest' in req.query){ torrent = await torrent.getTorrentData(); } @@ -43,9 +54,9 @@ router.get("/:id", async function(req, res, next){ } }); -router.delete("/:id", async function(req, res, next){ +router.delete("/:hashString", async function(req, res, next){ try{ - let torrent = await Torrent.findByPk(req.params.id); + let torrent = await Torrent.findByPk(req.params.hashString); res.json({result: torrent, activity: await torrent.destroy()}); }catch(error){ @@ -53,9 +64,9 @@ router.delete("/:id", async function(req, res, next){ } }); -router.post("/:id/stop", async function(req, res, next){ +router.post("/:hashString/stop", async function(req, res, next){ try{ - let torrent = await Torrent.findByPk(req.params.id); + let torrent = await Torrent.findByPk(req.params.hashString); res.json({result: torrent, activity: await torrent.stop()}); }catch(error){ @@ -63,9 +74,9 @@ router.post("/:id/stop", async function(req, res, next){ } }); -router.post("/:id/start", async function(req, res, next){ +router.post("/:hashString/start", async function(req, res, next){ try{ - let torrent = await Torrent.findByPk(req.params.id); + let torrent = await Torrent.findByPk(req.params.hashString); res.json({result: torrent, activity: await torrent.start()}); }catch(error){