use byte::Bytes instead of ByteBufOwned to store torrent bytes

This commit is contained in:
Igor Katson 2024-08-13 06:43:52 +01:00
parent 5193153e09
commit 55aeb07994
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
3 changed files with 17 additions and 19 deletions

View file

@ -205,13 +205,13 @@ impl HttpApi {
let (info, content) = match added { let (info, content) = match added {
crate::AddTorrentResponse::AlreadyManaged(_, handle) => ( crate::AddTorrentResponse::AlreadyManaged(_, handle) => (
handle.info().info.clone(), handle.info().info.clone(),
handle.info().torrent_bytes.clone().0, handle.info().torrent_bytes.clone(),
), ),
crate::AddTorrentResponse::ListOnly(ListOnlyResponse { crate::AddTorrentResponse::ListOnly(ListOnlyResponse {
info, info,
torrent_bytes, torrent_bytes,
.. ..
}) => (info, torrent_bytes.0), }) => (info, torrent_bytes),
crate::AddTorrentResponse::Added(_, _) => { crate::AddTorrentResponse::Added(_, _) => {
return Err(ApiError::new_from_text( return Err(ApiError::new_from_text(
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
@ -226,7 +226,7 @@ impl HttpApi {
); );
if let Some(name) = info.name.as_ref() { if let Some(name) = info.name.as_ref() {
if let Ok(name) = std::str::from_utf8(&name) { if let Ok(name) = std::str::from_utf8(name) {
if let Ok(h) = if let Ok(h) =
HeaderValue::from_str(&format!("attachment; filename=\"{}.torrent\"", name)) HeaderValue::from_str(&format!("attachment; filename=\"{}.torrent\"", name))
{ {

View file

@ -29,6 +29,7 @@ use crate::{
use anyhow::{bail, Context}; use anyhow::{bail, Context};
use bencode::{bencode_serialize_to_writer, BencodeDeserializer}; use bencode::{bencode_serialize_to_writer, BencodeDeserializer};
use buffers::{ByteBuf, ByteBufOwned, ByteBufT}; use buffers::{ByteBuf, ByteBufOwned, ByteBufT};
use bytes::Bytes;
use clone_to_owned::CloneToOwned; use clone_to_owned::CloneToOwned;
use dht::{Dht, DhtBuilder, DhtConfig, Id20, PersistentDht, PersistentDhtConfig}; use dht::{Dht, DhtBuilder, DhtConfig, Id20, PersistentDht, PersistentDhtConfig};
use futures::{ use futures::{
@ -144,9 +145,9 @@ struct SerializedTorrent {
#[serde( #[serde(
serialize_with = "serialize_torrent_bytes", serialize_with = "serialize_torrent_bytes",
deserialize_with = "deserialize_torrent_bytes", deserialize_with = "deserialize_torrent_bytes",
default = "empty_bytes" default
)] )]
torrent_bytes: ByteBufOwned, torrent_bytes: Bytes,
trackers: HashSet<String>, trackers: HashSet<String>,
output_folder: PathBuf, output_folder: PathBuf,
only_files: Option<Vec<usize>>, only_files: Option<Vec<usize>>,
@ -182,16 +183,16 @@ where
.map_err(D::Error::custom) .map_err(D::Error::custom)
} }
fn serialize_torrent_bytes<S>(t: &ByteBufOwned, serializer: S) -> Result<S::Ok, S::Error> fn serialize_torrent_bytes<S>(t: &Bytes, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
{ {
use base64::{engine::general_purpose, Engine as _}; use base64::{engine::general_purpose, Engine as _};
let s = general_purpose::STANDARD_NO_PAD.encode(&t.0); let s = general_purpose::STANDARD_NO_PAD.encode(t);
s.serialize(serializer) s.serialize(serializer)
} }
fn deserialize_torrent_bytes<'de, D>(deserializer: D) -> Result<ByteBufOwned, D::Error> fn deserialize_torrent_bytes<'de, D>(deserializer: D) -> Result<Bytes, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
@ -204,10 +205,6 @@ where
Ok(b.into()) Ok(b.into())
} }
fn empty_bytes() -> ByteBufOwned {
ByteBufOwned(Vec::new().into_boxed_slice())
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct SerializedSessionDatabase { struct SerializedSessionDatabase {
torrents: HashMap<usize, SerializedTorrent>, torrents: HashMap<usize, SerializedTorrent>,
@ -380,7 +377,7 @@ pub struct ListOnlyResponse {
pub only_files: Option<Vec<usize>>, pub only_files: Option<Vec<usize>>,
pub output_folder: PathBuf, pub output_folder: PathBuf,
pub seen_peers: Vec<SocketAddr>, pub seen_peers: Vec<SocketAddr>,
pub torrent_bytes: ByteBufOwned, pub torrent_bytes: Bytes,
} }
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
@ -515,7 +512,7 @@ pub(crate) struct CheckedIncomingConnection {
struct InternalAddResult { struct InternalAddResult {
info_hash: Id20, info_hash: Id20,
info: TorrentMetaV1Info<ByteBufOwned>, info: TorrentMetaV1Info<ByteBufOwned>,
torrent_bytes: ByteBufOwned, torrent_bytes: Bytes,
trackers: Vec<String>, trackers: Vec<String>,
peer_rx: Option<PeerStream>, peer_rx: Option<PeerStream>,
initial_peers: Vec<SocketAddr>, initial_peers: Vec<SocketAddr>,
@ -1000,7 +997,7 @@ impl Session {
InternalAddResult { InternalAddResult {
info_hash, info_hash,
info, info,
torrent_bytes: bytes, torrent_bytes: Bytes::from(bytes.0),
trackers: magnet.trackers.into_iter().unique().collect(), trackers: magnet.trackers.into_iter().unique().collect(),
peer_rx: Some(rx), peer_rx: Some(rx),
initial_peers: seen.into_iter().collect(), initial_peers: seen.into_iter().collect(),
@ -1064,7 +1061,7 @@ impl Session {
InternalAddResult { InternalAddResult {
info_hash: torrent.info_hash, info_hash: torrent.info_hash,
info: torrent.info, info: torrent.info,
torrent_bytes: bytes, torrent_bytes: Bytes::from(bytes.0),
trackers, trackers,
peer_rx, peer_rx,
initial_peers: opts initial_peers: opts

View file

@ -14,6 +14,7 @@ use std::time::Duration;
use anyhow::bail; use anyhow::bail;
use anyhow::Context; use anyhow::Context;
use buffers::ByteBufOwned; use buffers::ByteBufOwned;
use bytes::Bytes;
use futures::future::BoxFuture; use futures::future::BoxFuture;
use futures::FutureExt; use futures::FutureExt;
use librqbit_core::hash_id::Id20; use librqbit_core::hash_id::Id20;
@ -99,7 +100,7 @@ pub(crate) struct ManagedTorrentOptions {
pub struct ManagedTorrentInfo { pub struct ManagedTorrentInfo {
pub info: TorrentMetaV1Info<ByteBufOwned>, pub info: TorrentMetaV1Info<ByteBufOwned>,
pub torrent_bytes: ByteBufOwned, pub torrent_bytes: Bytes,
pub info_hash: Id20, pub info_hash: Id20,
pub(crate) spawner: BlockingSpawner, pub(crate) spawner: BlockingSpawner,
pub trackers: HashSet<String>, pub trackers: HashSet<String>,
@ -502,7 +503,7 @@ pub(crate) struct ManagedTorrentBuilder {
info: TorrentMetaV1Info<ByteBufOwned>, info: TorrentMetaV1Info<ByteBufOwned>,
output_folder: PathBuf, output_folder: PathBuf,
info_hash: Id20, info_hash: Id20,
torrent_bytes: ByteBufOwned, torrent_bytes: Bytes,
force_tracker_interval: Option<Duration>, force_tracker_interval: Option<Duration>,
peer_connect_timeout: Option<Duration>, peer_connect_timeout: Option<Duration>,
peer_read_write_timeout: Option<Duration>, peer_read_write_timeout: Option<Duration>,
@ -520,7 +521,7 @@ impl ManagedTorrentBuilder {
pub fn new( pub fn new(
info: TorrentMetaV1Info<ByteBufOwned>, info: TorrentMetaV1Info<ByteBufOwned>,
info_hash: Id20, info_hash: Id20,
torrent_bytes: ByteBufOwned, torrent_bytes: Bytes,
output_folder: PathBuf, output_folder: PathBuf,
storage_factory: BoxStorageFactory, storage_factory: BoxStorageFactory,
) -> Self { ) -> Self {