diff --git a/models/sql/torrent.js b/models/sql/torrent.js index a2e49c9..3df71ae 100644 --- a/models/sql/torrent.js +++ b/models/sql/torrent.js @@ -74,11 +74,26 @@ module.exports = (sequelize, DataTypes, Model) => { let res = await trClient.addUrl(data.magnetLink, options); + // Transmission usually returns hashString, but if it doesn't (duplicate, malformed + // response, etc.), fall back to parsing the info hash from the magnet link. + let hashString = res && (res.hashString || res.id); + if(!hashString){ + let match = data.magnetLink.match(/urn:btih:([A-Fa-f0-9]{40})/i); + if(match) hashString = match[1].toLowerCase(); + } + + if(!hashString){ + console.error('Transmission addUrl response missing hashString:', res); + let error = new Error('TorrentCreateError'); + error.message = 'Transmission did not return a hash for this torrent'; + throw error; + } + return await super.create({ magnetLink: data.magnetLink, - hashString: res.hashString, + hashString, isPrivate: data.isPrivate, - name: res.name, + name: res && res.name, added_by: data.added_by, category: this.categoryFromTPB(data.category), status: 0,