Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve, reject) => {
if(!this._hash){
this._state = State.INVALID;
return reject(INVALID_ERROR());
}
// Do not init() twice
if(this.isInitialized())
return reject(new TypeError('Torrent '+self._hash+' is already initialized'));
var parsed;
try {
parsed = parseTorrent(this._source);
} catch(err) {
return reject(err);
}
self._init = true;
self._title = parsed.name || 'loading title...';
self._state = State.LOADING;
this.emit('change');
this.emit('change:state');
this.emit('initializing');
this._torrent = this._config.client.add(this._source, (torr) => {
console.log('TORRENT ADDED');
this._state = State.CONNECTING;
this.emit('change');
onAddTorrent (payload) {
console.log('payload', payload);
let hash = parseTorrent(payload.hash).infoHash;
client.addTorrent(payload.hash).then((torrent) => {
this.torrents[hash] = torrent;
torrent.on('change', () => {
this.emit('change');
});
this.emit('change');
}).catch((err) => {
console.log(err);
this.emit('change')
});
},
return new Promise((resolve, reject) => {
console.log('create', source, config);
try {
let parsed = parseTorrent(source);
let trt = new Torrent(parsed.infoHash, source, config);
resolve(trt);
} catch (err) {
console.log(err);
return reject(err);
}
});
}
fs.readFile(paths[0], (err, data) => {
if (err) {
showToast(err.message, 'error');
}
const torrent = parseTorrent(data);
torrent.size = torrent.size || prettyBytes(torrent.length);
this.props.dispatch({
type: 'ADD_TO_DOWNLOAD_LIST',
payload: torrent,
});
ipcRenderer.send('add_torrent_to_download', torrent.infoHash);
});
},
addMagnetUrl = () => {
const value = this.magnetInputRef.value;
if (!isMagnet(value)) {
showToast('This is not a valid magnet URL', 'error');
return;
}
const { infoHash } = parseTorrent(value);
this.setState({
loadingMagnetInfo: true,
});
ipcRenderer.on('decoded_infoHash', (event, torrent) => {
this.closeMagnetUrlModal();
this.props.dispatch({
type: 'ADD_TO_DOWNLOAD_LIST',
payload: torrent,
});
});
ipcRenderer.send('decode_infohash_and_add_to_download', infoHash);
};
wstream.on('finish', () => {
try {
let length = parseTorrent(fs.readFileSync(torrentPath)).length;
res({ length });
} catch (exception) {
rej(exception);
} finally {
fs.unlink(torrentPath);
}
});
}).catch(rej);