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

Allow upload torrent with application/octet-stream HTTP header Content-Type #491

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
10 changes: 9 additions & 1 deletion src/web/api/client/v1/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl BinaryResponse {

#[must_use]
pub fn is_a_bit_torrent_file(&self) -> bool {
self.is_ok() && self.is_bittorrent_content_type()
self.is_ok() && (self.is_bittorrent_content_type() || self.is_octet_stream_content_type())
}

#[must_use]
Expand All @@ -82,6 +82,14 @@ impl BinaryResponse {
false
}

#[must_use]
pub fn is_octet_stream_content_type(&self) -> bool {
if let Some(content_type) = &self.content_type {
return content_type == "application/octet-stream";
}
false
}

#[must_use]
pub fn is_ok(&self) -> bool {
self.status == 200
Expand Down
4 changes: 3 additions & 1 deletion src/web/api/server/v1/contexts/torrent/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub enum Request {
#[display(fmt = "torrent tags string is not a valid JSON.")]
TagsArrayIsNotValidJson,

#[display(fmt = "upload torrent request header `content-type` should be `application/x-bittorrent`.")]
#[display(
fmt = "upload torrent request header `content-type` should be preferably `application/x-bittorrent` or `application/octet-stream`."
)]
InvalidFileType,

#[display(fmt = "cannot write uploaded torrent bytes (binary file) into memory.")]
Expand Down
4 changes: 2 additions & 2 deletions src/web/api/server/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub async fn create_random_torrent_handler(State(_app_data): State<Arc<AppData>>
///
/// - The text fields do not contain a valid UTF8 string.
/// - The torrent file data is not valid because:
/// - The content type is not `application/x-bittorrent`.
/// - The content type is not `application/x-bittorrent` or `application/octet-stream`.
/// - The multipart content is invalid.
/// - The torrent file pieces key has a length that is not a multiple of 20.
/// - The binary data cannot be decoded as a torrent file.
Expand Down Expand Up @@ -350,7 +350,7 @@ async fn build_add_torrent_request_from_payload(mut payload: Multipart) -> Resul
"torrent" => {
let content_type = field.content_type().unwrap();

if content_type != "application/x-bittorrent" {
if content_type != "application/x-bittorrent" && content_type != "application/octet-stream" {
return Err(errors::Request::InvalidFileType);
}

Expand Down
9 changes: 8 additions & 1 deletion tests/common/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl BinaryResponse {
}
}
pub fn is_a_bit_torrent_file(&self) -> bool {
self.is_ok() && self.is_bittorrent_content_type()
self.is_ok() && (self.is_bittorrent_content_type() || self.is_octet_stream_content_type())
}

pub fn is_bittorrent_content_type(&self) -> bool {
Expand All @@ -64,6 +64,13 @@ impl BinaryResponse {
false
}

pub fn is_octet_stream_content_type(&self) -> bool {
if let Some(content_type) = &self.content_type {
return content_type == "application/octet-stream";
}
false
}

pub fn is_ok(&self) -> bool {
self.status == 200
}
Expand Down
Loading