lots...
This commit is contained in:
126
models/sql/torrent.js
Normal file
126
models/sql/torrent.js
Normal file
@ -0,0 +1,126 @@
|
||||
'use strict';
|
||||
|
||||
const Transmission = require('transmission-promise');
|
||||
const conf = require('>/conf');
|
||||
|
||||
const tr_client = new Transmission(conf.transmission)
|
||||
|
||||
const statusMap = [
|
||||
'STOPPED',
|
||||
'CHECK_WAIT',
|
||||
'CHECK',
|
||||
'DOWNLOAD_WAIT',
|
||||
'DOWNLOAD',
|
||||
'SEED_WAIT',
|
||||
'SEED',
|
||||
'ISOLATED',
|
||||
];
|
||||
|
||||
module.exports = (sequelize, DataTypes, Model) => {
|
||||
class Torrent extends Model {
|
||||
/**
|
||||
* Helper method for defining associations.
|
||||
* This method is not a part of Sequelize lifecycle.
|
||||
* The `models/index` file will call this method automatically.
|
||||
*/
|
||||
static associate(models) {
|
||||
// define association here
|
||||
}
|
||||
|
||||
static trClient = tr_client;
|
||||
|
||||
static async create(data, ...args){
|
||||
try{
|
||||
|
||||
// let instance = this.build(data);
|
||||
// console.log('instance', instance)
|
||||
await this.build(data).validate();
|
||||
// console.log('validate', val);
|
||||
|
||||
let res = await tr_client.addUrl(data.magnetLink);
|
||||
|
||||
return await super.create({
|
||||
magnetLink: data.magnetLink,
|
||||
torrent_id: res.id,
|
||||
hashString: res.hashString,
|
||||
name: res.name,
|
||||
added_by: data.added_by,
|
||||
status: 0,
|
||||
percentDone: 0,
|
||||
}, args);
|
||||
}catch (error){
|
||||
// console.log('Torrent create error', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getTorrentData(noUpdate){
|
||||
try{
|
||||
let res = ( await tr_client.get(Number(this.torrent_id), [
|
||||
"eta", "percentDone", "status", "rateDownload",
|
||||
"errorString", "hashString", 'name',
|
||||
'downloadDir',
|
||||
'files', //array of files
|
||||
'filesStats', // array of files with status
|
||||
'isFinished',
|
||||
'isStalled',
|
||||
'peers',
|
||||
'peersConnected', // array of peers,
|
||||
'sizeWhenDone',
|
||||
]) ).torrents[0];
|
||||
|
||||
await this.update(res);
|
||||
if(noUpdate) await this.save();
|
||||
return {...res, ...this.dataValues};
|
||||
}catch(error){
|
||||
console.error(`Torrent ${this.id} getTorrentData error`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Torrent.init({
|
||||
torrent_id: DataTypes.STRING,
|
||||
magnetLink: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
validate:{
|
||||
notNull: true,
|
||||
notEmpty: true,
|
||||
},
|
||||
},
|
||||
isPrivate: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false,
|
||||
},
|
||||
hashString: DataTypes.STRING,
|
||||
name: DataTypes.STRING,
|
||||
added_by: {
|
||||
type: DataTypes.STRING,
|
||||
ldapModel: 'User',
|
||||
allowNull: false,
|
||||
validate:{
|
||||
notNull: true,
|
||||
notEmpty: true,
|
||||
},
|
||||
},
|
||||
status: DataTypes.NUMBER,
|
||||
percentDone: DataTypes.FLOAT,
|
||||
downloadDir: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
},
|
||||
errorString: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
},
|
||||
sizeWhenDone: {
|
||||
type: DataTypes.NUMBER,
|
||||
allowNull: true,
|
||||
},
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: 'Torrent',
|
||||
logging: false,
|
||||
});
|
||||
return Torrent;
|
||||
};
|
Reference in New Issue
Block a user