Skip to content

Commit

Permalink
feat: Support magnet links in normalizedAddTorrent
Browse files Browse the repository at this point in the history
fixes #99
  • Loading branch information
scttcper committed Jun 3, 2022
1 parent fadf2b8 commit b2bf46d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
51 changes: 42 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"test:ci": "vitest run --coverage"
},
"dependencies": {
"@ctrl/shared-torrent": "^4.1.0",
"@ctrl/magnet-link": "^3.1.0",
"@ctrl/shared-torrent": "^4.1.1",
"@ctrl/url-join": "^2.0.0",
"got": "^12.1.0"
},
Expand Down
21 changes: 16 additions & 5 deletions src/transmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync, readFileSync } from 'fs';

import got, { Response } from 'got';

import { magnetDecode } from '@ctrl/magnet-link';
import {
AddTorrentOptions as NormalizedAddTorrentOptions,
AllClientData,
Expand Down Expand Up @@ -223,12 +224,22 @@ export class Transmission implements TorrentClient {
torrentOptions.paused = true;
}

if (!Buffer.isBuffer(torrent)) {
torrent = Buffer.from(torrent);
}
let torrentHash: string | undefined;
if (typeof torrent === 'string' && torrent.startsWith('magnet:')) {
torrentHash = magnetDecode(torrent).infoHash;
if (!torrentHash) {
throw new Error('Magnet did not contain hash');
}

await this.addMagnet(torrent, torrentOptions);
} else {
if (!Buffer.isBuffer(torrent)) {
torrent = Buffer.from(torrent);
}

const res = await this.addTorrent(torrent, torrentOptions);
const torrentHash = [res.arguments['torrent-added'].hashString];
const res = await this.addTorrent(torrent, torrentOptions);
torrentHash = res.arguments['torrent-added'].hashString;
}

if (options.label) {
await this.setTorrent(torrentHash, { labels: [options.label] });
Expand Down
7 changes: 7 additions & 0 deletions test/transmission.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ describe('Transmission', () => {
const res = await client.addMagnet(magnet);
expect(res.result).toBe('success');
});
it('should add normalized magnet link', async () => {
const magnet =
'magnet:?xt=urn:btih:B0B81206633C42874173D22E564D293DAEFC45E2&dn=Ubuntu+11+10+Alternate+Amd64+Iso&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2710%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.open-internet.nl%3A6969%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.si%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.pirateparty.gr%3A6969%2Fannounce&tr=udp%3A%2F%2Fdenis.stalker.upeer.me%3A6969%2Fannounce&tr=udp%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce';
const client = new Transmission({ baseUrl });
const res = await client.normalizedAddTorrent(magnet);
expect(res.id).toBeTruthy();
});
it('should add torrent from file buffer', async () => {
const transmission = new Transmission({ baseUrl });
const res = await transmission.addTorrent(fs.readFileSync(torrentFile));
Expand Down

0 comments on commit b2bf46d

Please sign in to comment.