|
| 1 | +use crate::models::torrent_file::Torrent; |
| 2 | +use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v1_0_0::SqliteDatabaseV1_0_0; |
| 3 | +use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v2_0_0::SqliteDatabaseV2_0_0; |
| 4 | +use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v2_0_0::TorrentRecordV2; |
| 5 | +use crate::utils::parse_torrent::decode_torrent; |
| 6 | +use std::sync::Arc; |
| 7 | +use std::{error, fs}; |
| 8 | + |
| 9 | +pub async fn transfer_torrents( |
| 10 | + source_database: Arc<SqliteDatabaseV1_0_0>, |
| 11 | + dest_database: Arc<SqliteDatabaseV2_0_0>, |
| 12 | + upload_path: &str, |
| 13 | +) { |
| 14 | + println!("Transferring torrents ..."); |
| 15 | + |
| 16 | + // Transfer table `torrust_torrents_files` |
| 17 | + |
| 18 | + // Although the The table `torrust_torrents_files` existed in version v1.0.0 |
| 19 | + // it was was not used. |
| 20 | + |
| 21 | + // Transfer table `torrust_torrents` |
| 22 | + |
| 23 | + let torrents = source_database.get_torrents().await.unwrap(); |
| 24 | + |
| 25 | + for torrent in &torrents { |
| 26 | + // [v2] table torrust_torrents |
| 27 | + |
| 28 | + println!( |
| 29 | + "[v2][torrust_torrents] adding the torrent: {:?} ...", |
| 30 | + &torrent.torrent_id |
| 31 | + ); |
| 32 | + |
| 33 | + let uploader = source_database |
| 34 | + .get_user_by_username(&torrent.uploader) |
| 35 | + .await |
| 36 | + .unwrap(); |
| 37 | + |
| 38 | + if uploader.username != torrent.uploader { |
| 39 | + panic!( |
| 40 | + "Error copying torrent with id {:?}. |
| 41 | + Username (`uploader`) in `torrust_torrents` table does not match `username` in `torrust_users` table", |
| 42 | + &torrent.torrent_id |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + let filepath = format!("{}/{}.torrent", upload_path, &torrent.torrent_id); |
| 47 | + |
| 48 | + let torrent_from_file_result = read_torrent_from_file(&filepath); |
| 49 | + |
| 50 | + if torrent_from_file_result.is_err() { |
| 51 | + panic!("Error torrent file not found: {:?}", &filepath); |
| 52 | + } |
| 53 | + |
| 54 | + let torrent_from_file = torrent_from_file_result.unwrap(); |
| 55 | + |
| 56 | + let id = dest_database |
| 57 | + .insert_torrent(&TorrentRecordV2::from_v1_data( |
| 58 | + torrent, |
| 59 | + &torrent_from_file.info, |
| 60 | + &uploader, |
| 61 | + )) |
| 62 | + .await |
| 63 | + .unwrap(); |
| 64 | + |
| 65 | + if id != torrent.torrent_id { |
| 66 | + panic!( |
| 67 | + "Error copying torrent {:?} from source DB to destiny DB", |
| 68 | + &torrent.torrent_id |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + println!( |
| 73 | + "[v2][torrust_torrents] torrent with id {:?} added.", |
| 74 | + &torrent.torrent_id |
| 75 | + ); |
| 76 | + |
| 77 | + // [v2] table torrust_torrent_files |
| 78 | + |
| 79 | + println!("[v2][torrust_torrent_files] adding torrent files"); |
| 80 | + |
| 81 | + if torrent_from_file.is_a_single_file_torrent() { |
| 82 | + // The torrent contains only one file then: |
| 83 | + // - "path" is NULL |
| 84 | + // - "md5sum" can be NULL |
| 85 | + |
| 86 | + println!( |
| 87 | + "[v2][torrust_torrent_files][single-file-torrent] adding torrent file {:?} with length {:?} ...", |
| 88 | + &torrent_from_file.info.name, &torrent_from_file.info.length, |
| 89 | + ); |
| 90 | + |
| 91 | + let file_id = dest_database |
| 92 | + .insert_torrent_file_for_torrent_with_one_file( |
| 93 | + torrent.torrent_id, |
| 94 | + // TODO: it seems med5sum can be None. Why? When? |
| 95 | + &torrent_from_file.info.md5sum.clone(), |
| 96 | + torrent_from_file.info.length.unwrap(), |
| 97 | + ) |
| 98 | + .await; |
| 99 | + |
| 100 | + println!( |
| 101 | + "[v2][torrust_torrent_files][single-file-torrent] torrent file insert result: {:?}", |
| 102 | + &file_id |
| 103 | + ); |
| 104 | + } else { |
| 105 | + // Multiple files are being shared |
| 106 | + let files = torrent_from_file.info.files.as_ref().unwrap(); |
| 107 | + |
| 108 | + for file in files.iter() { |
| 109 | + println!( |
| 110 | + "[v2][torrust_torrent_files][multiple-file-torrent] adding torrent file: {:?} ...", |
| 111 | + &file |
| 112 | + ); |
| 113 | + |
| 114 | + let file_id = dest_database |
| 115 | + .insert_torrent_file_for_torrent_with_multiple_files(torrent, file) |
| 116 | + .await; |
| 117 | + |
| 118 | + println!( |
| 119 | + "[v2][torrust_torrent_files][multiple-file-torrent] torrent file insert result: {:?}", |
| 120 | + &file_id |
| 121 | + ); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // [v2] table torrust_torrent_info |
| 126 | + |
| 127 | + println!( |
| 128 | + "[v2][torrust_torrent_info] adding the torrent info for torrent id {:?} ...", |
| 129 | + &torrent.torrent_id |
| 130 | + ); |
| 131 | + |
| 132 | + let id = dest_database.insert_torrent_info(torrent).await; |
| 133 | + |
| 134 | + println!( |
| 135 | + "[v2][torrust_torrents] torrent info insert result: {:?}.", |
| 136 | + &id |
| 137 | + ); |
| 138 | + |
| 139 | + // [v2] table torrust_torrent_announce_urls |
| 140 | + |
| 141 | + println!( |
| 142 | + "[v2][torrust_torrent_announce_urls] adding the torrent announce url for torrent id {:?} ...", |
| 143 | + &torrent.torrent_id |
| 144 | + ); |
| 145 | + |
| 146 | + if torrent_from_file.announce_list.is_some() { |
| 147 | + // BEP-0012. Multiple trackers. |
| 148 | + |
| 149 | + println!("[v2][torrust_torrent_announce_urls][announce-list] adding the torrent announce url for torrent id {:?} ...", &torrent.torrent_id); |
| 150 | + |
| 151 | + // flatten the nested vec (this will however remove the) |
| 152 | + let announce_urls = torrent_from_file |
| 153 | + .announce_list |
| 154 | + .clone() |
| 155 | + .unwrap() |
| 156 | + .into_iter() |
| 157 | + .flatten() |
| 158 | + .collect::<Vec<String>>(); |
| 159 | + |
| 160 | + for tracker_url in announce_urls.iter() { |
| 161 | + println!("[v2][torrust_torrent_announce_urls][announce-list] adding the torrent announce url for torrent id {:?} ...", &torrent.torrent_id); |
| 162 | + |
| 163 | + let announce_url_id = dest_database |
| 164 | + .insert_torrent_announce_url(torrent.torrent_id, tracker_url) |
| 165 | + .await; |
| 166 | + |
| 167 | + println!("[v2][torrust_torrent_announce_urls][announce-list] torrent announce url insert result {:?} ...", &announce_url_id); |
| 168 | + } |
| 169 | + } else if torrent_from_file.announce.is_some() { |
| 170 | + println!("[v2][torrust_torrent_announce_urls][announce] adding the torrent announce url for torrent id {:?} ...", &torrent.torrent_id); |
| 171 | + |
| 172 | + let announce_url_id = dest_database |
| 173 | + .insert_torrent_announce_url( |
| 174 | + torrent.torrent_id, |
| 175 | + &torrent_from_file.announce.unwrap(), |
| 176 | + ) |
| 177 | + .await; |
| 178 | + |
| 179 | + println!( |
| 180 | + "[v2][torrust_torrent_announce_urls][announce] torrent announce url insert result {:?} ...", |
| 181 | + &announce_url_id |
| 182 | + ); |
| 183 | + } |
| 184 | + } |
| 185 | + println!("Torrents transferred"); |
| 186 | +} |
| 187 | + |
| 188 | +pub fn read_torrent_from_file(path: &str) -> Result<Torrent, Box<dyn error::Error>> { |
| 189 | + let contents = match fs::read(path) { |
| 190 | + Ok(contents) => contents, |
| 191 | + Err(e) => return Err(e.into()), |
| 192 | + }; |
| 193 | + |
| 194 | + match decode_torrent(&contents) { |
| 195 | + Ok(torrent) => Ok(torrent), |
| 196 | + Err(e) => Err(e), |
| 197 | + } |
| 198 | +} |
0 commit comments