Add access to new fields
This commit is contained in:
parent
98f011673e
commit
18755d8971
7 changed files with 78 additions and 70 deletions
|
|
@ -533,23 +533,23 @@ fn make_torrent_details(
|
||||||
output_folder: String,
|
output_folder: String,
|
||||||
) -> Result<TorrentDetailsResponse> {
|
) -> Result<TorrentDetailsResponse> {
|
||||||
let files = info
|
let files = info
|
||||||
.iter_filenames_and_lengths()
|
.iter_file_details()
|
||||||
.context("error iterating filenames and lengths")?
|
.context("error iterating filenames and lengths")?
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(idx, (filename_it, length))| {
|
.map(|(idx, d)| {
|
||||||
let name = match filename_it.to_string() {
|
let name = match d.filename.to_string() {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("error reading filename: {:?}", err);
|
warn!("error reading filename: {:?}", err);
|
||||||
"<INVALID NAME>".to_string()
|
"<INVALID NAME>".to_string()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let components = filename_it.to_vec().unwrap_or_default();
|
let components = d.filename.to_vec().unwrap_or_default();
|
||||||
let included = only_files.map(|o| o.contains(&idx)).unwrap_or(true);
|
let included = only_files.map(|o| o.contains(&idx)).unwrap_or(true);
|
||||||
TorrentDetailsResponseFile {
|
TorrentDetailsResponseFile {
|
||||||
name,
|
name,
|
||||||
components,
|
components,
|
||||||
length,
|
length: d.len,
|
||||||
included,
|
included,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -568,10 +568,11 @@ fn torrent_file_mime_type(
|
||||||
info: &TorrentMetaV1Info<ByteBufOwned>,
|
info: &TorrentMetaV1Info<ByteBufOwned>,
|
||||||
file_idx: usize,
|
file_idx: usize,
|
||||||
) -> Result<&'static str> {
|
) -> Result<&'static str> {
|
||||||
info.iter_filenames_and_lengths()?
|
info.iter_file_details()?
|
||||||
.nth(file_idx)
|
.nth(file_idx)
|
||||||
.and_then(|(f, _)| {
|
.and_then(|d| {
|
||||||
f.iter_components()
|
d.filename
|
||||||
|
.iter_components()
|
||||||
.last()
|
.last()
|
||||||
.and_then(|r| r.ok())
|
.and_then(|r| r.ok())
|
||||||
.and_then(|s| mime_guess::from_path(s).first_raw())
|
.and_then(|s| mime_guess::from_path(s).first_raw())
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,8 @@ impl<'a> FileOps<'a> {
|
||||||
|
|
||||||
let mut piece_remaining_bytes = piece_length as usize;
|
let mut piece_remaining_bytes = piece_length as usize;
|
||||||
|
|
||||||
for (file_idx, (name, file_len)) in self.torrent.iter_filenames_and_lengths()?.enumerate() {
|
for (file_idx, file_details) in self.torrent.iter_file_details()?.enumerate() {
|
||||||
|
let file_len = file_details.len;
|
||||||
if absolute_offset > file_len {
|
if absolute_offset > file_len {
|
||||||
absolute_offset -= file_len;
|
absolute_offset -= file_len;
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -205,7 +206,10 @@ impl<'a> FileOps<'a> {
|
||||||
to_read_in_file,
|
to_read_in_file,
|
||||||
)
|
)
|
||||||
.with_context(|| {
|
.with_context(|| {
|
||||||
format!("error reading {to_read_in_file} bytes, file_id: {file_idx} (\"{name:?}\")")
|
format!(
|
||||||
|
"error reading {to_read_in_file} bytes, file_id: {file_idx} (\"{:?}\")",
|
||||||
|
file_details.filename
|
||||||
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
piece_remaining_bytes -= to_read_in_file;
|
piece_remaining_bytes -= to_read_in_file;
|
||||||
|
|
@ -292,7 +296,8 @@ impl<'a> FileOps<'a> {
|
||||||
let mut buf = data.block.as_ref();
|
let mut buf = data.block.as_ref();
|
||||||
let mut absolute_offset = self.lengths.chunk_absolute_offset(chunk_info);
|
let mut absolute_offset = self.lengths.chunk_absolute_offset(chunk_info);
|
||||||
|
|
||||||
for (file_idx, (name, file_len)) in self.torrent.iter_filenames_and_lengths()?.enumerate() {
|
for (file_idx, file_details) in self.torrent.iter_file_details()?.enumerate() {
|
||||||
|
let file_len = file_details.len;
|
||||||
if absolute_offset > file_len {
|
if absolute_offset > file_len {
|
||||||
absolute_offset -= file_len;
|
absolute_offset -= file_len;
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -313,7 +318,12 @@ impl<'a> FileOps<'a> {
|
||||||
);
|
);
|
||||||
self.files
|
self.files
|
||||||
.pwrite_all(file_idx, absolute_offset, &buf[..to_write])
|
.pwrite_all(file_idx, absolute_offset, &buf[..to_write])
|
||||||
.with_context(|| format!("error writing to file {file_idx} (\"{name:?}\")"))?;
|
.with_context(|| {
|
||||||
|
format!(
|
||||||
|
"error writing to file {file_idx} (\"{:?}\")",
|
||||||
|
file_details.filename
|
||||||
|
)
|
||||||
|
})?;
|
||||||
buf = &buf[to_write..];
|
buf = &buf[to_write..];
|
||||||
if buf.is_empty() {
|
if buf.is_empty() {
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -153,10 +153,10 @@ impl HttpApi {
|
||||||
let mut playlist_items = handle
|
let mut playlist_items = handle
|
||||||
.shared()
|
.shared()
|
||||||
.info
|
.info
|
||||||
.iter_filenames_and_lengths()?
|
.iter_file_details()?
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(file_idx, (filename, _))| {
|
.filter_map(|(file_idx, file_details)| {
|
||||||
let filename = filename.to_vec().ok()?.join("/");
|
let filename = file_details.filename.to_vec().ok()?.join("/");
|
||||||
let is_playable = mime_guess::from_path(&filename)
|
let is_playable = mime_guess::from_path(&filename)
|
||||||
.first()
|
.first()
|
||||||
.map(|mime| {
|
.map(|mime| {
|
||||||
|
|
|
||||||
|
|
@ -156,8 +156,9 @@ fn compute_only_files_regex<ByteBuf: AsRef<[u8]>>(
|
||||||
) -> anyhow::Result<Vec<usize>> {
|
) -> anyhow::Result<Vec<usize>> {
|
||||||
let filename_re = regex::Regex::new(filename_re).context("filename regex is incorrect")?;
|
let filename_re = regex::Regex::new(filename_re).context("filename regex is incorrect")?;
|
||||||
let mut only_files = Vec::new();
|
let mut only_files = Vec::new();
|
||||||
for (idx, (filename, _)) in torrent.iter_filenames_and_lengths()?.enumerate() {
|
for (idx, fd) in torrent.iter_file_details()?.enumerate() {
|
||||||
let full_path = filename
|
let full_path = fd
|
||||||
|
.filename
|
||||||
.to_pathbuf()
|
.to_pathbuf()
|
||||||
.with_context(|| format!("filename of file {idx} is not valid utf8"))?;
|
.with_context(|| format!("filename of file {idx} is not valid utf8"))?;
|
||||||
if filename_re.is_match(full_path.to_str().unwrap()) {
|
if filename_re.is_match(full_path.to_str().unwrap()) {
|
||||||
|
|
@ -191,12 +192,12 @@ fn compute_only_files(
|
||||||
}
|
}
|
||||||
(None, Some(filename_re)) => {
|
(None, Some(filename_re)) => {
|
||||||
let only_files = compute_only_files_regex(info, &filename_re)?;
|
let only_files = compute_only_files_regex(info, &filename_re)?;
|
||||||
for (idx, (filename, _)) in info.iter_filenames_and_lengths()?.enumerate() {
|
for (idx, fd) in info.iter_file_details()?.enumerate() {
|
||||||
if !only_files.contains(&idx) {
|
if !only_files.contains(&idx) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if !list_only {
|
if !list_only {
|
||||||
info!(?filename, "will download");
|
info!(filename=?fd.filename, "will download");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Some(only_files))
|
Ok(Some(only_files))
|
||||||
|
|
@ -1043,8 +1044,8 @@ impl Session {
|
||||||
info: &TorrentMetaV1Info<ByteBufOwned>,
|
info: &TorrentMetaV1Info<ByteBufOwned>,
|
||||||
) -> anyhow::Result<Option<PathBuf>> {
|
) -> anyhow::Result<Option<PathBuf>> {
|
||||||
let files = info
|
let files = info
|
||||||
.iter_filenames_and_lengths()?
|
.iter_file_details()?
|
||||||
.map(|(f, l)| Ok((f.to_pathbuf()?, l)))
|
.map(|fd| Ok((fd.filename.to_pathbuf()?, fd.len)))
|
||||||
.collect::<anyhow::Result<Vec<(PathBuf, u64)>>>()?;
|
.collect::<anyhow::Result<Vec<(PathBuf, u64)>>>()?;
|
||||||
if files.len() < 2 {
|
if files.len() < 2 {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
|
@ -1145,7 +1146,7 @@ impl Session {
|
||||||
.map(|fd| {
|
.map(|fd| {
|
||||||
Ok::<_, anyhow::Error>(FileInfo {
|
Ok::<_, anyhow::Error>(FileInfo {
|
||||||
relative_filename: fd.details.filename.to_pathbuf()?,
|
relative_filename: fd.details.filename.to_pathbuf()?,
|
||||||
offset_in_torrent: fd.details.offset,
|
offset_in_torrent: fd.offset,
|
||||||
piece_range: fd.pieces,
|
piece_range: fd.pieces,
|
||||||
len: fd.details.len,
|
len: fd.details.len,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -68,10 +68,10 @@ impl TorrentFileTreeNode {
|
||||||
let last_url_bit = torrent
|
let last_url_bit = torrent
|
||||||
.shared()
|
.shared()
|
||||||
.info
|
.info
|
||||||
.iter_filenames_and_lengths()
|
.iter_file_details()
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|mut it| it.nth(fid))
|
.and_then(|mut it| it.nth(fid))
|
||||||
.and_then(|(fi, _)| fi.to_vec().ok())
|
.and_then(|fd| fd.filename.to_vec().ok())
|
||||||
.map(|components| {
|
.map(|components| {
|
||||||
components
|
components
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
@ -111,10 +111,10 @@ struct TorrentFileTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_single_file_at_root(info: &TorrentMetaV1Info<ByteBufOwned>) -> bool {
|
fn is_single_file_at_root(info: &TorrentMetaV1Info<ByteBufOwned>) -> bool {
|
||||||
info.iter_filenames_and_lengths()
|
info.iter_file_details()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flatten()
|
.flatten()
|
||||||
.flat_map(|(f, _)| f.iter_components())
|
.flat_map(|fd| fd.filename.iter_components())
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.is_none()
|
.is_none()
|
||||||
}
|
}
|
||||||
|
|
@ -123,10 +123,10 @@ impl TorrentFileTree {
|
||||||
fn build(torent_id: TorrentId, info: &TorrentMetaV1Info<ByteBufOwned>) -> anyhow::Result<Self> {
|
fn build(torent_id: TorrentId, info: &TorrentMetaV1Info<ByteBufOwned>) -> anyhow::Result<Self> {
|
||||||
if is_single_file_at_root(info) {
|
if is_single_file_at_root(info) {
|
||||||
let filename = info
|
let filename = info
|
||||||
.iter_filenames_and_lengths()?
|
.iter_file_details()?
|
||||||
.next()
|
.next()
|
||||||
.context("bug")?
|
.context("bug")?
|
||||||
.0
|
.filename
|
||||||
.iter_components()
|
.iter_components()
|
||||||
.last()
|
.last()
|
||||||
.context("bug")??;
|
.context("bug")??;
|
||||||
|
|
@ -159,8 +159,8 @@ impl TorrentFileTree {
|
||||||
|
|
||||||
let mut name_cache = HashMap::new();
|
let mut name_cache = HashMap::new();
|
||||||
|
|
||||||
for (fid, (fi, _)) in info.iter_filenames_and_lengths()?.enumerate() {
|
for (fid, fd) in info.iter_file_details()?.enumerate() {
|
||||||
let components = match fi.to_vec() {
|
let components = match fd.filename.to_vec() {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(_) => continue,
|
Err(_) => continue,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -182,18 +182,19 @@ where
|
||||||
|
|
||||||
pub struct FileDetails<'a, BufType> {
|
pub struct FileDetails<'a, BufType> {
|
||||||
pub filename: FileIteratorName<'a, BufType>,
|
pub filename: FileIteratorName<'a, BufType>,
|
||||||
// absolute offset in torrent if it was a flat blob of bytes
|
|
||||||
pub offset: u64,
|
|
||||||
pub len: u64,
|
pub len: u64,
|
||||||
|
|
||||||
// bep-47
|
// bep-47
|
||||||
pub attr: Option<BufType>,
|
pub attr: Option<&'a BufType>,
|
||||||
pub sha1: Option<BufType>,
|
pub sha1: Option<&'a BufType>,
|
||||||
pub symlink_path: Option<Vec<BufType>>,
|
pub symlink_path: Option<&'a [BufType]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FileDetailsExt<'a, BufType> {
|
pub struct FileDetailsExt<'a, BufType> {
|
||||||
pub details: FileDetails<'a, BufType>,
|
pub details: FileDetails<'a, BufType>,
|
||||||
|
// absolute offset in torrent if it was a flat blob of bytes
|
||||||
|
pub offset: u64,
|
||||||
|
|
||||||
// the pieces that contain this file
|
// the pieces that contain this file
|
||||||
pub pieces: std::ops::Range<u32>,
|
pub pieces: std::ops::Range<u32>,
|
||||||
}
|
}
|
||||||
|
|
@ -220,33 +221,38 @@ impl<BufType: AsRef<[u8]>> TorrentMetaV1Info<BufType> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
pub fn iter_filenames_and_lengths(
|
pub fn iter_file_details(
|
||||||
&self,
|
&self,
|
||||||
) -> anyhow::Result<impl Iterator<Item = (FileIteratorName<'_, BufType>, u64)>> {
|
) -> anyhow::Result<impl Iterator<Item = FileDetails<'_, BufType>>> {
|
||||||
match (self.length, self.files.as_ref()) {
|
match (self.length, self.files.as_ref()) {
|
||||||
// Single-file
|
// Single-file
|
||||||
(Some(length), None) => Ok(Either::Left(once((
|
(Some(length), None) => Ok(Either::Left(once(FileDetails {
|
||||||
FileIteratorName::Single(self.name.as_ref()),
|
filename: FileIteratorName::Single(self.name.as_ref()),
|
||||||
length,
|
len: length,
|
||||||
)))),
|
attr: self.attr.as_ref(),
|
||||||
|
sha1: self.sha1.as_ref(),
|
||||||
|
symlink_path: self.symlink_path.as_deref(),
|
||||||
|
}))),
|
||||||
|
|
||||||
// Multi-file
|
// Multi-file
|
||||||
(None, Some(files)) => {
|
(None, Some(files)) => {
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
anyhow::bail!("expected multi-file torrent to have at least one file")
|
anyhow::bail!("expected multi-file torrent to have at least one file")
|
||||||
}
|
}
|
||||||
Ok(Either::Right(
|
Ok(Either::Right(files.iter().map(|f| FileDetails {
|
||||||
files
|
filename: FileIteratorName::Tree(&f.path),
|
||||||
.iter()
|
len: f.length,
|
||||||
.map(|f| (FileIteratorName::Tree(&f.path), f.length)),
|
attr: f.attr.as_ref(),
|
||||||
))
|
sha1: f.sha1.as_ref(),
|
||||||
|
symlink_path: f.symlink_path.as_deref(),
|
||||||
|
})))
|
||||||
}
|
}
|
||||||
_ => anyhow::bail!("torrent can't be both in single and multi-file mode"),
|
_ => anyhow::bail!("torrent can't be both in single and multi-file mode"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter_file_lengths(&self) -> anyhow::Result<impl Iterator<Item = u64> + '_> {
|
pub fn iter_file_lengths(&self) -> anyhow::Result<impl Iterator<Item = u64> + '_> {
|
||||||
Ok(self.iter_filenames_and_lengths()?.map(|(_, l)| l))
|
Ok(self.iter_file_details()?.map(|d| d.len))
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: lenghts MUST be construced with Lenghts::from_torrent, otherwise
|
// NOTE: lenghts MUST be construced with Lenghts::from_torrent, otherwise
|
||||||
|
|
@ -255,23 +261,15 @@ impl<BufType: AsRef<[u8]>> TorrentMetaV1Info<BufType> {
|
||||||
&'a self,
|
&'a self,
|
||||||
lengths: &'a Lengths,
|
lengths: &'a Lengths,
|
||||||
) -> anyhow::Result<impl Iterator<Item = FileDetailsExt<'a, BufType>> + 'a> {
|
) -> anyhow::Result<impl Iterator<Item = FileDetailsExt<'a, BufType>> + 'a> {
|
||||||
Ok(self
|
Ok(self.iter_file_details()?.scan(0u64, |acc_offset, details| {
|
||||||
.iter_filenames_and_lengths()?
|
let offset = *acc_offset;
|
||||||
.scan(0u64, |acc_offset, (filename, len)| {
|
*acc_offset += details.len;
|
||||||
let offset = *acc_offset;
|
Some(FileDetailsExt {
|
||||||
*acc_offset += len;
|
pieces: lengths.iter_pieces_within_offset(offset, details.len),
|
||||||
Some(FileDetailsExt {
|
details,
|
||||||
details: FileDetails {
|
offset,
|
||||||
filename,
|
})
|
||||||
offset,
|
}))
|
||||||
len,
|
|
||||||
attr: None,
|
|
||||||
sha1: None,
|
|
||||||
symlink_path: None,
|
|
||||||
},
|
|
||||||
pieces: lengths.iter_pieces_within_offset(offset, len),
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -762,17 +762,15 @@ async fn async_main(opts: Opts, cancel: CancellationToken) -> anyhow::Result<()>
|
||||||
only_files,
|
only_files,
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
for (idx, (filename, len)) in
|
for (idx, fd) in info.iter_file_details()?.enumerate() {
|
||||||
info.iter_filenames_and_lengths()?.enumerate()
|
|
||||||
{
|
|
||||||
let included = match &only_files {
|
let included = match &only_files {
|
||||||
Some(files) => files.contains(&idx),
|
Some(files) => files.contains(&idx),
|
||||||
None => true,
|
None => true,
|
||||||
};
|
};
|
||||||
info!(
|
info!(
|
||||||
"File {}, size {}{}",
|
"File {:?}, size {}{}",
|
||||||
filename.to_string()?,
|
fd.filename,
|
||||||
SF::new(len),
|
SF::new(fd.len),
|
||||||
if included { "" } else { ", will skip" }
|
if included { "" } else { ", will skip" }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue