@@ -11,6 +11,7 @@ import {
11
11
TorrentState ,
12
12
AddTorrentOptions as NormalizedAddTorrentOptions ,
13
13
} from '@ctrl/shared-torrent' ;
14
+ import { hash } from '@ctrl/torrent-file' ;
14
15
15
16
import {
16
17
GetHostsResponse ,
@@ -31,8 +32,6 @@ import {
31
32
TorrentStatus ,
32
33
Tracker ,
33
34
Torrent ,
34
- StringStatus ,
35
- AddTorrentResponse ,
36
35
} from './types' ;
37
36
38
37
const defaults : TorrentSettings = {
@@ -78,11 +77,11 @@ export class Deluge implements TorrentClient {
78
77
}
79
78
80
79
/**
81
- * Connects deluge and returns a list of available methods
80
+ * Connects deluge
82
81
* @param host index of host to use in result of get hosts
83
82
* @param hostIdx index of host to use in result of get hosts
84
83
*/
85
- async connect ( selectedHost ?: string , hostIdx = 0 ) : Promise < ListMethods > {
84
+ async connect ( selectedHost ?: string , hostIdx = 0 ) : Promise < DefaultResponse > {
86
85
let host = selectedHost ;
87
86
if ( ! host ) {
88
87
const hosts = await this . getHosts ( ) ;
@@ -93,7 +92,7 @@ export class Deluge implements TorrentClient {
93
92
throw new Error ( 'No hosts found' ) ;
94
93
}
95
94
96
- const res = await this . request < ListMethods > ( 'web.connect' , [ host ] , true , false ) ;
95
+ const res = await this . request < DefaultResponse > ( 'web.connect' , [ host ] , true , false ) ;
97
96
return res . body ;
98
97
}
99
98
@@ -107,14 +106,8 @@ export class Deluge implements TorrentClient {
107
106
* Other instances may also reconnect. Not really sure why you would want to disconnect
108
107
*/
109
108
async disconnect ( ) : Promise < boolean > {
110
- const res = await this . request < StringStatus > ( 'web.disconnect' , [ ] , true , false ) ;
111
- // deluge 1.x returns a boolean and 2.x returns a string
112
- if ( typeof res . body . result === 'boolean' ) {
113
- return res . body . result ;
114
- }
115
-
116
- // "Connection was closed cleanly."
117
- return res . body . result . includes ( 'closed cleanly' ) ;
109
+ const res = await this . request < BooleanStatus > ( 'web.disconnect' , [ ] , true , false ) ;
110
+ return res . body . result ;
118
111
}
119
112
120
113
/**
@@ -215,7 +208,7 @@ export class Deluge implements TorrentClient {
215
208
headers : form . getHeaders ( ) ,
216
209
body : form ,
217
210
retry : 0 ,
218
- } ;
211
+ }
219
212
// allow proxy agent
220
213
if ( this . config . agent ) {
221
214
options . agent = this . config . agent ;
@@ -229,10 +222,7 @@ export class Deluge implements TorrentClient {
229
222
return JSON . parse ( res . body ) ;
230
223
}
231
224
232
- async addTorrent (
233
- torrent : string | Buffer ,
234
- config : Partial < AddTorrentOptions > = { } ,
235
- ) : Promise < AddTorrentResponse > {
225
+ async addTorrent ( torrent : string | Buffer , config : Partial < AddTorrentOptions > = { } ) {
236
226
const upload = await this . upload ( torrent ) ;
237
227
if ( ! upload . success || ! upload . files . length ) {
238
228
throw new Error ( 'Failed to upload' ) ;
@@ -251,16 +241,11 @@ export class Deluge implements TorrentClient {
251
241
// not passing path by default uses default
252
242
// download_location: '/root/Downloads',
253
243
// move_completed_path: '/root/Downloads',
254
- pre_allocate_storage : false ,
255
- move_completed : false ,
256
- seed_mode : false ,
257
- sequential_download : false ,
258
- super_seeding : false ,
259
244
...config ,
260
245
} ;
261
- const res = await this . request < AddTorrentResponse > ( 'web.add_torrents' , [ [ { path, options } ] ] ) ;
246
+ const res = await this . request < BooleanStatus > ( 'web.add_torrents' , [ [ { path, options } ] ] ) ;
262
247
263
- if ( res . body . result [ 0 ] [ 0 ] === false ) {
248
+ if ( res . body . result === false ) {
264
249
throw new Error ( 'Failed to add torrent' ) ;
265
250
}
266
251
@@ -280,12 +265,11 @@ export class Deluge implements TorrentClient {
280
265
torrent = Buffer . from ( torrent ) ;
281
266
}
282
267
283
- const res = await this . addTorrent ( torrent , torrentOptions ) ;
284
- const torrentHash = res . result [ 0 ] [ 1 ] ;
268
+ const torrentHash = await hash ( torrent ) ;
269
+
270
+ await this . addTorrent ( torrent , torrentOptions ) ;
285
271
286
272
if ( options . label ) {
287
- // sets the label but it might not set the label right away
288
- // sometimes takes a few seconds for label to reflect in results
289
273
await this . setTorrentLabel ( torrentHash , options . label ) ;
290
274
}
291
275
@@ -304,12 +288,7 @@ export class Deluge implements TorrentClient {
304
288
prioritize_first_last_pieces : false ,
305
289
// not passing path by default uses default
306
290
// download_location: '/root/Downloads',
307
- move_completed : false ,
308
291
// move_completed_path: '/root/Downloads',
309
- pre_allocate_storage : false ,
310
- seed_mode : false ,
311
- sequential_download : false ,
312
- super_seeding : false ,
313
292
...config ,
314
293
} ;
315
294
const res = await this . request < BooleanStatus > ( 'core.add_torrent_magnet' , [ magnet , options ] ) ;
@@ -412,10 +391,7 @@ export class Deluge implements TorrentClient {
412
391
* get torrent state/status
413
392
* @param additionalFields fields ex - `['label']`
414
393
*/
415
- async getTorrentStatus (
416
- torrentId : string ,
417
- additionalFields : string [ ] = [ ] ,
418
- ) : Promise < TorrentStatus > {
394
+ async getTorrentStatus ( torrentId : string , additionalFields : string [ ] = [ ] ) : Promise < TorrentStatus > {
419
395
const fields = [
420
396
'total_done' ,
421
397
'total_payload_download' ,
0 commit comments