Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/librqbit/examples/custom_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ struct CustomStorage {
impl StorageFactory for CustomStorageFactory {
type Storage = CustomStorage;

fn create(&self, _info: &librqbit::ManagedTorrentShared) -> anyhow::Result<Self::Storage> {
fn create(
&self,
_: &librqbit::ManagedTorrentShared,
_: &librqbit::TorrentMetadata,
) -> anyhow::Result<Self::Storage> {
Ok(CustomStorage::default())
}

Expand Down Expand Up @@ -54,7 +58,11 @@ impl TorrentStorage for CustomStorage {
anyhow::bail!("not implemented")
}

fn init(&mut self, _meta: &librqbit::ManagedTorrentShared) -> anyhow::Result<()> {
fn init(
&mut self,
_meta: &librqbit::ManagedTorrentShared,
_: &librqbit::TorrentMetadata,
) -> anyhow::Result<()> {
anyhow::bail!("not implemented")
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/librqbit/examples/ubuntu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ async fn main() -> Result<(), anyhow::Error> {
_ => unreachable!(),
};

info!("Details: {:?}", &handle.shared().info);
handle.with_metadata(|r| {
info!("Details: {:?}", &r.info);
})?;

// Print stats periodically.
tokio::spawn({
Expand Down
71 changes: 40 additions & 31 deletions crates/librqbit/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl Api {
let mut r = TorrentDetailsResponse {
id: Some(id),
info_hash: mgr.shared().info_hash.as_string(),
name: mgr.shared().info.name.as_ref().map(|n| n.to_string()),
name: mgr.name(),
output_folder: mgr
.shared()
.options
Expand Down Expand Up @@ -245,7 +245,8 @@ impl Api {
make_torrent_details(
Some(handle.id()),
&info_hash,
&handle.shared().info,
handle.metadata.load().as_ref().map(|r| &r.info),
handle.name().as_deref(),
only_files.as_deref(),
output_folder,
)
Expand All @@ -261,8 +262,7 @@ impl Api {
file_idx: usize,
) -> Result<&'static str> {
let handle = self.mgr_handle(idx)?;
let info = &handle.shared().info;
torrent_file_mime_type(info, file_idx)
handle.with_metadata(|r| torrent_file_mime_type(&r.info, file_idx))?
}

pub fn api_peer_stats(
Expand Down Expand Up @@ -380,7 +380,8 @@ impl Api {
let details = make_torrent_details(
Some(id),
&handle.info_hash(),
&handle.shared().info,
handle.metadata.load().as_ref().map(|r| &r.info),
handle.name().as_deref(),
handle.only_files().as_deref(),
handle
.shared()
Expand Down Expand Up @@ -416,7 +417,8 @@ impl Api {
details: make_torrent_details(
None,
&info_hash,
&info,
Some(&info),
None,
only_files.as_deref(),
output_folder.to_string_lossy().into_owned().to_string(),
)
Expand All @@ -426,7 +428,8 @@ impl Api {
let details = make_torrent_details(
Some(id),
&handle.info_hash(),
&handle.shared().info,
handle.metadata.load().as_ref().map(|r| &r.info),
handle.name().as_deref(),
handle.only_files().as_deref(),
handle
.shared()
Expand Down Expand Up @@ -529,37 +532,43 @@ pub struct ApiAddTorrentResponse {
fn make_torrent_details(
id: Option<TorrentId>,
info_hash: &Id20,
info: &TorrentMetaV1Info<ByteBufOwned>,
info: Option<&TorrentMetaV1Info<ByteBufOwned>>,
name: Option<&str>,
only_files: Option<&[usize]>,
output_folder: String,
) -> Result<TorrentDetailsResponse> {
let files = info
.iter_file_details()
.context("error iterating filenames and lengths")?
.enumerate()
.map(|(idx, d)| {
let name = match d.filename.to_string() {
Ok(s) => s,
Err(err) => {
warn!("error reading filename: {:?}", err);
"<INVALID NAME>".to_string()
let files = match info {
Some(info) => info
.iter_file_details()
.context("error iterating filenames and lengths")?
.enumerate()
.map(|(idx, d)| {
let name = match d.filename.to_string() {
Ok(s) => s,
Err(err) => {
warn!("error reading filename: {:?}", err);
"<INVALID NAME>".to_string()
}
};
let components = d.filename.to_vec().unwrap_or_default();
let included = only_files.map(|o| o.contains(&idx)).unwrap_or(true);
TorrentDetailsResponseFile {
name,
components,
length: d.len,
included,
attributes: d.attrs(),
}
};
let components = d.filename.to_vec().unwrap_or_default();
let included = only_files.map(|o| o.contains(&idx)).unwrap_or(true);
TorrentDetailsResponseFile {
name,
components,
length: d.len,
included,
attributes: d.attrs(),
}
})
.collect();
})
.collect(),
None => Default::default(),
};
Ok(TorrentDetailsResponse {
id,
info_hash: info_hash.as_string(),
name: info.name.as_ref().map(|b| b.to_string()),
name: name
.map(|s| s.to_owned())
.or_else(|| info.and_then(|i| i.name.as_ref().map(|b| b.to_string()))),
files: Some(files),
output_folder,
stats: None,
Expand Down
12 changes: 7 additions & 5 deletions crates/librqbit/src/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ impl HttpApi {

fn torrent_playlist_items(handle: &ManagedTorrent) -> Result<Vec<(usize, String)>> {
let mut playlist_items = handle
.shared()
.metadata
.load()
.as_ref()
.context("torrent metadata not resolved")?
.info
.iter_file_details()?
.enumerate()
Expand Down Expand Up @@ -340,10 +343,9 @@ impl HttpApi {
.context("timeout")??;

let (info, content) = match added {
crate::AddTorrentResponse::AlreadyManaged(_, handle) => (
handle.shared().info.clone(),
handle.shared().torrent_bytes.clone(),
),
crate::AddTorrentResponse::AlreadyManaged(_, handle) => {
handle.with_metadata(|r| (r.info.clone(), r.torrent_bytes.clone()))?
}
crate::AddTorrentResponse::ListOnly(ListOnlyResponse {
info,
torrent_bytes,
Expand Down
3 changes: 2 additions & 1 deletion crates/librqbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ pub use session::{
};
pub use spawn_utils::spawn as librqbit_spawn;
pub use torrent_state::{
ManagedTorrent, ManagedTorrentShared, ManagedTorrentState, TorrentStats, TorrentStatsState,
ManagedTorrent, ManagedTorrentShared, ManagedTorrentState, TorrentMetadata, TorrentStats,
TorrentStatsState,
};
pub use type_aliases::FileInfos;

Expand Down
Loading