Skip to content

Commit

Permalink
Add a torrent from-link subcommand
Browse files Browse the repository at this point in the history
It is now possible to create a torrent file from a magnet link. The
search is performed with minimal concurrency, and so may take a while to
complete.

type: added
fixes: casey#255
  • Loading branch information
atomgardner committed Aug 2, 2023
1 parent 5ff30ed commit 1a8593e
Show file tree
Hide file tree
Showing 12 changed files with 529 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rand = "0.7.3"
open = "1.4.0"
pretty_assertions = "0.6.0"
pretty_env_logger = "0.4.0"
rayon = "<=1.6.0"
regex = "1.0.0"
serde-hex = "0.1.0"
serde_bytes = "0.11.0"
Expand Down
4 changes: 4 additions & 0 deletions bin/gen/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ examples:
text: "Intermodal can be used to create `.torrent` files:"
code: "imdl torrent create --input foo"

- command: imdl torrent from-link
text: "Intermodal can be used to create a `.torrent` file from a magnet link:"
code: "imdl torrent from-link magnet:?foo"

- command: imdl torrent show
text: "Print information about existing `.torrent` files:"
code: "imdl torrent show --input foo.torrent"
Expand Down
3 changes: 2 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) use std::{
process::ExitStatus,
str::{self, FromStr},
string::FromUtf8Error,
sync::Once,
sync::{mpsc::channel, Once},
time::{Duration, SystemTime, SystemTimeError},
usize,
};
Expand Down Expand Up @@ -94,6 +94,7 @@ mod test {
ops::{Deref, DerefMut},
process::Command,
rc::Rc,
thread,
};

// test dependencies
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub(crate) enum Error {
Filesystem { source: io::Error, path: PathBuf },
#[snafu(display("Error searching for files: {}", source))]
FileSearch { source: ignore::Error },
#[snafu(display("Failed to fetch infodict from accessible peers"))]
FromLinkNoInfo,
#[snafu(display("Invalid glob: {}", source))]
GlobParse { source: globset::Error },
#[snafu(display("Failed to serialize torrent info dictionary: {}", source))]
Expand Down
12 changes: 6 additions & 6 deletions src/magnet_link.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::common::*;

#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct MagnetLink {
infohash: Infohash,
name: Option<String>,
peers: Vec<HostPort>,
trackers: Vec<Url>,
indices: BTreeSet<u64>,
pub(crate) infohash: Infohash,
pub(crate) name: Option<String>,
pub(crate) peers: Vec<HostPort>,
pub(crate) trackers: Vec<Url>,
pub(crate) indices: BTreeSet<u64>,
}

impl MagnetLink {
Expand Down
3 changes: 3 additions & 0 deletions src/subcommand/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::common::*;

mod announce;
mod create;
mod from_link;
mod link;
mod piece_length;
mod show;
Expand All @@ -17,6 +18,7 @@ mod verify;
pub(crate) enum Torrent {
Announce(announce::Announce),
Create(create::Create),
FromLink(from_link::FromLink),
Link(link::Link),
#[structopt(alias = "piece-size")]
PieceLength(piece_length::PieceLength),
Expand All @@ -30,6 +32,7 @@ impl Torrent {
match self {
Self::Announce(announce) => announce.run(env),
Self::Create(create) => create.run(env, options),
Self::FromLink(from_link) => from_link.run(env, options),
Self::Link(link) => link.run(env),
Self::PieceLength(piece_length) => piece_length.run(env),
Self::Show(show) => show.run(env),
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/torrent/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Announce {
}
};

let client = match tracker::Client::from_url(tracker_url) {
let client = match tracker::Client::from_url(&tracker_url) {
Ok(client) => client,
Err(err) => {
errln!(env, "Couldn't build tracker client. {}", err)?;
Expand All @@ -69,7 +69,7 @@ impl Announce {
};

usable_trackers += 1;
match client.announce_exchange(infohash) {
match client.announce_exchange(&infohash) {
Ok(peer_list) => peers.extend(peer_list),
Err(err) => errln!(env, "Announce failed: {}", err)?,
}
Expand Down
Loading

0 comments on commit 1a8593e

Please sign in to comment.