Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add content type to streams #162

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 additions & 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 crates/librqbit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ rlimit = "0.10.1"
async-stream = "0.3.5"
memmap2 = { version = "0.9.4" }
lru = { version = "0.12.3", optional = true }
mime_guess = { version = "2.0.5", default-features = false}

[dev-dependencies]
futures = { version = "0.3" }
Expand Down
31 changes: 31 additions & 0 deletions crates/librqbit/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ impl Api {
make_torrent_details(&info_hash, &handle.info().info, only_files.as_deref())
}

pub fn torrent_file_mime_type(&self, idx: TorrentId, file_idx: usize) -> Result<&'static str> {
let handle = self.mgr_handle(idx)?;
let info = &handle.info().info;
torrent_file_mime_type(info, file_idx)
}

pub fn api_peer_stats(
&self,
idx: TorrentId,
Expand Down Expand Up @@ -320,3 +326,28 @@ fn make_torrent_details(
files,
})
}

fn torrent_file_mime_type(
info: &TorrentMetaV1Info<ByteBufOwned>,
file_idx: usize,
) -> Result<&'static str> {
let file_name = info
.iter_filenames_and_lengths()?
.enumerate()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather then enumerate().find_map(if ...) wouldn't just nth(file_idx) work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, bad me - cannot remember all those cool iterators combinators - fixed in next commit.

.find_map(|(idx, (f, _))| {
if idx == file_idx {
f.iter_components()
.last()
.and_then(|r| r.ok())
.and_then(|s| mime_guess::from_path(s).first_raw())
} else {
None
}
});
file_name.ok_or_else(|| {
ApiError::new_from_text(
StatusCode::INTERNAL_SERVER_ERROR,
"cannot determine mime type for file",
)
})
}
17 changes: 12 additions & 5 deletions crates/librqbit/src/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ impl HttpApi {
let mut output_headers = HeaderMap::new();
output_headers.insert("Accept-Ranges", HeaderValue::from_static("bytes"));

if let Ok(mime) = state.torrent_file_mime_type(idx, file_id) {
output_headers.insert(
http::header::CONTENT_TYPE,
HeaderValue::from_str(mime).context("bug - invalid MIME")?,
);
}

let range_header = headers.get(http::header::RANGE);
trace!(torrent_id=idx, file_id=file_id, range=?range_header, "request for HTTP stream");

Expand Down Expand Up @@ -199,12 +206,12 @@ impl HttpApi {
))
.context("bug")?,
);
} else {
output_headers.insert(
http::header::CONTENT_LENGTH,
HeaderValue::from_str(&format!("{}", stream.len())).context("bug")?,
);
}
} else {
output_headers.insert(
http::header::CONTENT_LENGTH,
HeaderValue::from_str(&format!("{}", stream.len())).context("bug")?,
);
}

let s = tokio_util::io::ReaderStream::new(stream);
Expand Down
3 changes: 1 addition & 2 deletions crates/librqbit/webui/src/components/FileListInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ const FileTreeComponent: React.FC<{
const fileLink = (file: TorrentFileForCheckbox) => {
if (
allowStream &&
torrentId != null &&
/\.(mp4|mkv|avi)$/.test(file.filename)
torrentId != null
) {
return API.getTorrentStreamUrl(torrentId, file.id, file.filename);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const FormCheckbox: React.FC<{
{labelLink ? (
<a
href={labelLink}
target="_blank"
className="text-blue-600 dark:text-blue-500 hover:underline"
>
{label}
Expand Down