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

View File

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

View File

@ -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,