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