From 5dc4e8a119a61f5d33951aca4402c504b9c5c413 Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Mon, 8 Nov 2021 22:33:22 -0800 Subject: [PATCH] server: normalize fetching of torrents added with URL --- .../Transmission/clientGatewayService.ts | 67 +++++++++++++------ .../qBittorrent/clientGatewayService.ts | 24 ++++++- 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/server/services/Transmission/clientGatewayService.ts b/server/services/Transmission/clientGatewayService.ts index 4fc3cf2e..cefe8234 100644 --- a/server/services/Transmission/clientGatewayService.ts +++ b/server/services/Transmission/clientGatewayService.ts @@ -28,6 +28,7 @@ import type {SetClientSettingsOptions} from '@shared/types/api/client'; import ClientGatewayService from '../interfaces/clientGatewayService'; import ClientRequestManager from './clientRequestManager'; +import {fetchUrls} from '../../util/fetchUtil'; import {getDomainsFromURLs} from '../../util/torrentPropertiesUtil'; import {TorrentContentPriority} from '../../../shared/types/TorrentContent'; import {TorrentPriority} from '../../../shared/types/Torrent'; @@ -77,38 +78,62 @@ class TransmissionClientGatewayService extends ClientGatewayService { } async addTorrentsByURL({ - urls, + urls: inputUrls, cookies, destination, tags, + isBasePath, + isCompleted, + isInitialSeeding, + isSequential, start, }: Required): Promise { - const addedTorrents = await Promise.all( - urls.map(async (url) => { - const domain = url.split('/')[2]; - const {hashString} = - (await this.clientRequestManager - .addTorrent({ - filename: url, - cookies: cookies[domain] != null ? `${cookies[domain].join('; ')};` : undefined, - 'download-dir': destination, - paused: !start, - }) - .then(this.processClientRequestSuccess, this.processClientRequestError) - .catch(() => undefined)) || {}; - return hashString; - }), - ).then((results) => results.filter((hash) => hash) as string[]); + const {files, urls} = await fetchUrls(inputUrls, cookies); - if (addedTorrents[0] == null) { + if (!files[0] && !urls[0]) { throw new Error(); } - if (tags.length > 0) { - await this.setTorrentsTags({hashes: addedTorrents as [string, ...string[]], tags}); + const result: string[] = []; + + if (urls[0]) { + result.push( + ...(await Promise.all( + urls.map((url) => + this.clientRequestManager + .addTorrent({ + filename: url, + 'download-dir': destination, + paused: !start, + }) + .then(this.processClientRequestSuccess, this.processClientRequestError) + .catch(() => undefined) + .then((result) => result?.hashString), + ), + ).then((hashes) => hashes.filter((hash) => hash) as string[])), + ); } - return addedTorrents; + if (result[0] && tags.length > 0) { + await this.setTorrentsTags({hashes: result as [string, ...string[]], tags}); + } + + if (files[0]) { + result.push( + ...(await this.addTorrentsByFile({ + files: files.map((file) => file.toString('base64')) as [string, ...string[]], + destination, + tags, + isBasePath, + isCompleted, + isInitialSeeding, + isSequential, + start, + })), + ); + } + + return result; } async checkTorrents({hashes}: CheckTorrentsOptions): Promise { diff --git a/server/services/qBittorrent/clientGatewayService.ts b/server/services/qBittorrent/clientGatewayService.ts index d06e8295..807befa8 100644 --- a/server/services/qBittorrent/clientGatewayService.ts +++ b/server/services/qBittorrent/clientGatewayService.ts @@ -32,6 +32,7 @@ import type {SetClientSettingsOptions} from '@shared/types/api/client'; import ClientGatewayService from '../interfaces/clientGatewayService'; import ClientRequestManager from './clientRequestManager'; +import {fetchUrls} from '../../util/fetchUtil'; import {getDomainsFromURLs} from '../../util/torrentPropertiesUtil'; import { getTorrentPeerPropertiesFromFlags, @@ -98,20 +99,24 @@ class QBittorrentClientGatewayService extends ClientGatewayService { } async addTorrentsByURL({ - urls, + urls: inputUrls, cookies, destination, tags, isBasePath, isCompleted, + isInitialSeeding, isSequential, start, }: Required): Promise { - // TODO: isInitialSeeding not implemented + const {files, urls} = await fetchUrls(inputUrls, cookies); + + if (!files[0] && !urls[0]) { + throw new Error(); + } await this.clientRequestManager .torrentsAddURLs(urls, { - cookie: cookies != null ? Object.values(cookies)[0]?.[0] : undefined, savepath: destination, tags: tags.join(','), paused: !start, @@ -122,6 +127,19 @@ class QBittorrentClientGatewayService extends ClientGatewayService { }) .then(this.processClientRequestSuccess, this.processClientRequestError); + if (files[0]) { + return this.addTorrentsByFile({ + files: files.map((file) => file.toString('base64')) as [string, ...string[]], + destination, + tags, + isBasePath, + isCompleted, + isInitialSeeding, + isSequential, + start, + }); + } + return []; }