Skip to content

Commit 39de4cd

Browse files
authored
feat: Add removeLabel, fix file upload (#99)
1 parent 3a95504 commit 39de4cd

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Diff for: src/index.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ export class Deluge implements TorrentClient {
211211
const form = new FormData();
212212
if (typeof torrent === 'string') {
213213
if (existsSync(torrent)) {
214-
form.append('file', Buffer.from(readFileSync(torrent)));
214+
form.append('file', Buffer.from(readFileSync(torrent)), 'temp.torrent');
215215
} else {
216-
form.append('file', Buffer.from(torrent, 'base64'));
216+
form.append('file', Buffer.from(torrent, 'base64'), 'temp.torrent');
217217
}
218218
} else {
219-
form.append('file', torrent);
219+
form.append('file', torrent, 'temp.torrent');
220220
}
221221

222222
const url = urlJoin(this.config.baseUrl, '/upload');
@@ -534,6 +534,11 @@ export class Deluge implements TorrentClient {
534534
return req.body;
535535
}
536536

537+
async removeLabel(label: string): Promise<DefaultResponse> {
538+
const req = await this.request<DefaultResponse>('label.remove', [label]);
539+
return req.body;
540+
}
541+
537542
async getLabels(): Promise<ListMethods> {
538543
const req = await this.request<ListMethods>('label.get_labels', []);
539544
return req.body;
@@ -615,7 +620,7 @@ export class Deluge implements TorrentClient {
615620
Cookie: this._cookie?.cookieString?.(),
616621
};
617622
const url = urlJoin(this.config.baseUrl, this.config.path);
618-
return got.post(url, {
623+
const res: Response<T> = await got.post(url, {
619624
json: {
620625
method,
621626
params,
@@ -628,6 +633,14 @@ export class Deluge implements TorrentClient {
628633
timeout: this.config.timeout,
629634
responseType: 'json',
630635
});
636+
637+
const err = (res.body as {error: unknown})?.error ?? (typeof res.body === 'string' && res.body);
638+
639+
if (err) {
640+
throw new Error((err as Error).message || err as string);
641+
}
642+
643+
return res;
631644
}
632645

633646
private _normalizeTorrentData(id: string, torrent: Torrent): NormalizedTorrent {

Diff for: test/index.spec.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ describe('Deluge', () => {
219219
const client = new Deluge({ baseUrl });
220220
const list = await setupTorrent(client);
221221
const key = Object.keys(list.result.torrents)[0];
222+
await client.addLabel('swag');
222223
const res = await client.setTorrentLabel(key, 'swag');
224+
await client.removeLabel('swag');
223225
expect(res.result).toBe(null);
224226
});
225227
it('should pause/resume torrents', async () => {
@@ -285,9 +287,9 @@ describe('Deluge', () => {
285287
label: 'test',
286288
});
287289
expect(torrent.connectedPeers).toBeGreaterThanOrEqual(0);
288-
expect(torrent.connectedSeeds).toBe(0);
289-
expect(torrent.downloadSpeed).toBe(0);
290-
expect(torrent.eta).toBe(0);
290+
expect(torrent.connectedSeeds).toBeGreaterThanOrEqual(0);
291+
expect(torrent.downloadSpeed).toBeGreaterThanOrEqual(0);
292+
expect(torrent.eta).toBeGreaterThanOrEqual(0);
291293
// expect(torrent.isCompleted).toBe(false);
292294
// its setting the label but it takes an unknown number of seconds to save to db
293295
// expect(torrent.label).toBe('');
@@ -298,12 +300,12 @@ describe('Deluge', () => {
298300
// expect(torrent.savePath).toBe('/downloads/');
299301
// expect(torrent.state).toBe(TorrentState.checking);
300302
// expect(torrent.stateMessage).toBe('');
301-
expect(torrent.totalDownloaded).toBe(0);
303+
expect(torrent.totalDownloaded).toBeGreaterThanOrEqual(0);
302304
expect(torrent.totalPeers).toBe(-1);
303305
expect(torrent.totalSeeds).toBe(-1);
304306
expect(torrent.totalSelected).toBe(1953349632);
305307
// expect(torrent.totalSize).toBe(undefined);
306-
expect(torrent.totalUploaded).toBe(0);
307-
expect(torrent.uploadSpeed).toBe(0);
308+
expect(torrent.totalUploaded).toBeGreaterThanOrEqual(0);
309+
expect(torrent.uploadSpeed).toBeGreaterThanOrEqual(0);
308310
}, 15000);
309311
});

0 commit comments

Comments
 (0)