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( '
+ {{name}} - {{statusText}} +
++ Is {{sizeWhenDone}} + saved to {{downloadDir}} + added by {{added_by}} + {{createdAtString}} +
- {{#isActive}} - - {{/isActive}} + {{^isFinished}} ++ +
- + {{#isActive}} ++ {{rateDownload}} + Finishing {{eta}} + From {{peersConnected}} Peers +
- {{/isFinished}} + + {{/isActive}} - {{#isFinished}} -+ {{errorString}} +
+ {{/errorString}} + + {{#isFinished}} ++ Done! HTTP Link +
+ {{/isFinished}} +
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
- +
+
+
+
+
- + + +
- - + + + -