Add contet-type to stream

This commit is contained in:
Ivan 2024-07-27 11:44:15 +02:00
parent 81755c55d2
commit a77c1caace
4 changed files with 59 additions and 0 deletions

View file

@ -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,
@ -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()
.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",
)
})
}

View file

@ -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");