From b0c12acbc6e0cc3c9e052a72a63f0bdb57e0e036 Mon Sep 17 00:00:00 2001 From: lonelyhentxi Date: Sat, 5 Apr 2025 10:40:48 +0800 Subject: [PATCH] fix: fix paths --- apps/recorder/src/downloader/qbit/mod.rs | 53 +++++++++++++++--------- packages/testing-torrents/main.ts | 50 ++++++---------------- packages/testing-torrents/package.json | 1 - packages/testing-torrents/pnpm-lock.yaml | 3 -- 4 files changed, 46 insertions(+), 61 deletions(-) diff --git a/apps/recorder/src/downloader/qbit/mod.rs b/apps/recorder/src/downloader/qbit/mod.rs index 4e37df6..5438d6f 100644 --- a/apps/recorder/src/downloader/qbit/mod.rs +++ b/apps/recorder/src/downloader/qbit/mod.rs @@ -587,6 +587,7 @@ impl QBittorrentDownloader { Ok(torrent.save_path.take()) } + #[instrument(level = "debug", skip(self))] async fn sync_data(&self) -> Result<(), DownloaderError> { let rid = { self.sync_data.read().await.rid }; let sync_data_patch = self.client.sync(Some(rid)).await?; @@ -649,7 +650,7 @@ impl DownloaderTrait for QBittorrentDownloader { &self, creation: Self::Creation, ) -> Result, DownloaderError> { - let tag = { + let tags = { let mut tags = vec![TORRENT_TAG_NAME.to_string()]; tags.extend(creation.tags); Some(tags.into_iter().filter(|s| !s.is_empty()).join(",")) @@ -658,7 +659,7 @@ impl DownloaderTrait for QBittorrentDownloader { let save_path = Some(creation.save_path.into_string()); let sources = creation.sources; - let ids = HashSet::from_iter(sources.iter().map(|s| s.hash_info().to_string())); + let hashes = HashSet::from_iter(sources.iter().map(|s| s.hash_info().to_string())); let (urls_source, files_source) = { let mut urls = vec![]; let mut files = vec![]; @@ -691,7 +692,20 @@ impl DownloaderTrait for QBittorrentDownloader { ) }; - let category = TORRENT_TAG_NAME.to_string(); + let category = creation.category; + + if let Some(category) = category.as_deref() { + let has_caetgory = { + self.sync_data + .read() + .await + .categories + .contains_key(category) + }; + if !has_caetgory { + self.add_category(category).await?; + } + } if let Some(source) = urls_source { self.client @@ -699,8 +713,8 @@ impl DownloaderTrait for QBittorrentDownloader { source, savepath: save_path.clone(), auto_torrent_management: Some(false), - category: Some(category.clone()), - tags: tag.clone(), + category: category.clone(), + tags: tags.clone(), ..Default::default() }) .await?; @@ -710,10 +724,10 @@ impl DownloaderTrait for QBittorrentDownloader { self.client .add_torrent(AddTorrentArg { source, - savepath: save_path.clone(), + savepath: save_path, auto_torrent_management: Some(false), - category: Some(category.clone()), - tags: tag, + category, + tags, ..Default::default() }) .await?; @@ -721,12 +735,12 @@ impl DownloaderTrait for QBittorrentDownloader { self.wait_sync_until( |sync_data| { let torrents = &sync_data.torrents; - ids.iter().all(|id| torrents.contains_key(id)) + hashes.iter().all(|hash| torrents.contains_key(hash)) }, None, ) .await?; - Ok(ids) + Ok(hashes) } async fn pause_downloads( @@ -769,6 +783,7 @@ impl DownloaderTrait for QBittorrentDownloader { } })) .await?; + let tasks = torrent_list .into_iter() .zip(torrent_contents) @@ -938,7 +953,7 @@ pub mod tests { use tokio::io::AsyncReadExt; tracing_subscriber::fmt() - .with_max_level(tracing::Level::TRACE) + .with_max_level(tracing::Level::DEBUG) .with_test_writer() .init(); @@ -946,16 +961,16 @@ pub mod tests { let torrents_container = torrents_image.start().await?; let torrents_req = MockRequest { - id: "test".into(), + id: "f10ebdda-dd2e-43f8-b80c-bf0884d071c4".into(), file_list: vec![MockFileItem { - path: "test.torrent".into(), + path: "[Nekomoe kissaten&LoliHouse] Boku no Kokoro no Yabai Yatsu - 20 [WebRip \ + 1080p HEVC-10bit AAC ASSx2].mkv" + .into(), size: 1024, }], }; - let torrent_res: MockResponse = reqwest::Client::builder() - .pool_max_idle_per_host(0) - .build()? + let torrent_res: MockResponse = reqwest::Client::new() .post("http://127.0.0.1:6080/api/torrents/mock") .json(&torrents_req) .send() @@ -988,10 +1003,7 @@ pub mod tests { let password = logs .lines() .find_map(|line| { - if line.contains( - "A temporary password is provided for this - session", - ) { + if line.contains("A temporary password is provided for") { line.split_whitespace().last() } else { None @@ -1074,6 +1086,7 @@ pub mod tests { let target_torrent = get_torrent().await?; let files = target_torrent.contents; + assert!(!files.is_empty()); let first_file = files.first().expect("should have first file"); diff --git a/packages/testing-torrents/main.ts b/packages/testing-torrents/main.ts index 14d8377..4b3d95b 100644 --- a/packages/testing-torrents/main.ts +++ b/packages/testing-torrents/main.ts @@ -6,7 +6,6 @@ import fsp from 'node:fs/promises'; import path from 'node:path'; // @ts-ignore import TrackerServer from 'bittorrent-tracker/server'; -import createTorrent from 'create-torrent'; import WebTorrent, { type Torrent } from 'webtorrent'; // Configuration @@ -17,7 +16,7 @@ const STATIC_API_PATH = '/api/static'; const LOCAL_IP = '127.0.0.1'; const WORKSPACE_PATH = 'workspace'; const TRACKER_URL = `http://${LOCAL_IP}:${TRACKER_PORT}/announce`; -const API_BASE_URL = `http://${LOCAL_IP}:${API_PORT}/${STATIC_API_PATH}/`; +const API_BASE_URL = `http://${LOCAL_IP}:${API_PORT}${STATIC_API_PATH}/`; // Initialize Fastify instance const app = Fastify({ logger: true }); @@ -90,44 +89,22 @@ async function generateMockFile(filePath: string, size: number) { await fsp.truncate(filePath, size); } -// Generate bittorrent file -function generateTorrent(folderPath: string, torrentPath: string) { - return new Promise((resolve, reject) => { - createTorrent( - folderPath, +// Add bittorrent and seed +async function seedTorrent( + torrentPath: string, + contentFolder: string +): Promise { + return new Promise((resolve) => { + const torrent = webTorrent.seed( + contentFolder, { announceList: [[TRACKER_URL]], // Specify tracker URL private: false, - createdBy: 'WebTorrent', - comment: 'Generated by WebTorrent server', + createdBy: 'Konobangu Testing Torrents', urlList: [API_BASE_URL], }, - async (err, torrent) => { - if (err) { - reject(new Error(`Failed to create torrent: ${err}`)); - return; - } - await fsp.writeFile(torrentPath, torrent); - if (!fs.existsSync(torrentPath)) { - reject(new Error(`Torrent file ${torrentPath} was not created`)); - return; - } - console.log(`Generated torrent with tracker: ${TRACKER_URL}`); - resolve(); - } - ); - }); -} - -// Add bittorrent and seed -async function seedTorrent(torrentPath: string): Promise { - return new Promise((resolve) => { - const torrent = webTorrent.seed( - torrentPath, - { - announce: [TRACKER_URL], - }, - (t) => { + async (t) => { + await fsp.writeFile(torrentPath, t.torrentFile); resolve(t); } ); @@ -156,9 +133,8 @@ app.post<{ Body: RequestSchema }>('/api/torrents/mock', async (req, _reply) => { } const torrentPath = path.join(WORKSPACE_PATH, `${id}.torrent`); - await generateTorrent(idFolder, torrentPath); - const torrent = await seedTorrent(torrentPath); + const torrent = await seedTorrent(torrentPath, idFolder); const magnetUrl = `magnet:?xt=urn:btih:${torrent.infoHash}&tr=${TRACKER_URL}`; return { diff --git a/packages/testing-torrents/package.json b/packages/testing-torrents/package.json index 774f25d..d09cefe 100644 --- a/packages/testing-torrents/package.json +++ b/packages/testing-torrents/package.json @@ -10,7 +10,6 @@ "dependencies": { "@fastify/static": "^8.1.1", "bittorrent-tracker": "^11.2.1", - "create-torrent": "^6.1.0", "fastify": "^5.2.2", "tsx": "^4.19.2", "webtorrent": "^2.5.19" diff --git a/packages/testing-torrents/pnpm-lock.yaml b/packages/testing-torrents/pnpm-lock.yaml index 1be8f9d..bcdcefd 100644 --- a/packages/testing-torrents/pnpm-lock.yaml +++ b/packages/testing-torrents/pnpm-lock.yaml @@ -14,9 +14,6 @@ importers: bittorrent-tracker: specifier: ^11.2.1 version: 11.2.1 - create-torrent: - specifier: ^6.1.0 - version: 6.1.0 fastify: specifier: ^5.2.2 version: 5.2.2