Skip to content

Commit

Permalink
feat: Add removeLabel, fix file upload (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsg2021 committed Sep 27, 2020
1 parent 3a95504 commit 39de4cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ export class Deluge implements TorrentClient {
const form = new FormData();
if (typeof torrent === 'string') {
if (existsSync(torrent)) {
form.append('file', Buffer.from(readFileSync(torrent)));
form.append('file', Buffer.from(readFileSync(torrent)), 'temp.torrent');
} else {
form.append('file', Buffer.from(torrent, 'base64'));
form.append('file', Buffer.from(torrent, 'base64'), 'temp.torrent');
}
} else {
form.append('file', torrent);
form.append('file', torrent, 'temp.torrent');
}

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

async removeLabel(label: string): Promise<DefaultResponse> {
const req = await this.request<DefaultResponse>('label.remove', [label]);
return req.body;
}

async getLabels(): Promise<ListMethods> {
const req = await this.request<ListMethods>('label.get_labels', []);
return req.body;
Expand Down Expand Up @@ -615,7 +620,7 @@ export class Deluge implements TorrentClient {
Cookie: this._cookie?.cookieString?.(),
};
const url = urlJoin(this.config.baseUrl, this.config.path);
return got.post(url, {
const res: Response<T> = await got.post(url, {
json: {
method,
params,
Expand All @@ -628,6 +633,14 @@ export class Deluge implements TorrentClient {
timeout: this.config.timeout,
responseType: 'json',
});

const err = (res.body as {error: unknown})?.error ?? (typeof res.body === 'string' && res.body);

if (err) {
throw new Error((err as Error).message || err as string);
}

return res;
}

private _normalizeTorrentData(id: string, torrent: Torrent): NormalizedTorrent {
Expand Down
14 changes: 8 additions & 6 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ describe('Deluge', () => {
const client = new Deluge({ baseUrl });
const list = await setupTorrent(client);
const key = Object.keys(list.result.torrents)[0];
await client.addLabel('swag');
const res = await client.setTorrentLabel(key, 'swag');
await client.removeLabel('swag');
expect(res.result).toBe(null);
});
it('should pause/resume torrents', async () => {
Expand Down Expand Up @@ -285,9 +287,9 @@ describe('Deluge', () => {
label: 'test',
});
expect(torrent.connectedPeers).toBeGreaterThanOrEqual(0);
expect(torrent.connectedSeeds).toBe(0);
expect(torrent.downloadSpeed).toBe(0);
expect(torrent.eta).toBe(0);
expect(torrent.connectedSeeds).toBeGreaterThanOrEqual(0);
expect(torrent.downloadSpeed).toBeGreaterThanOrEqual(0);
expect(torrent.eta).toBeGreaterThanOrEqual(0);
// expect(torrent.isCompleted).toBe(false);
// its setting the label but it takes an unknown number of seconds to save to db
// expect(torrent.label).toBe('');
Expand All @@ -298,12 +300,12 @@ describe('Deluge', () => {
// expect(torrent.savePath).toBe('/downloads/');
// expect(torrent.state).toBe(TorrentState.checking);
// expect(torrent.stateMessage).toBe('');
expect(torrent.totalDownloaded).toBe(0);
expect(torrent.totalDownloaded).toBeGreaterThanOrEqual(0);
expect(torrent.totalPeers).toBe(-1);
expect(torrent.totalSeeds).toBe(-1);
expect(torrent.totalSelected).toBe(1953349632);
// expect(torrent.totalSize).toBe(undefined);
expect(torrent.totalUploaded).toBe(0);
expect(torrent.uploadSpeed).toBe(0);
expect(torrent.totalUploaded).toBeGreaterThanOrEqual(0);
expect(torrent.uploadSpeed).toBeGreaterThanOrEqual(0);
}, 15000);
});

0 comments on commit 39de4cd

Please sign in to comment.