Skip to content

Commit 8a97b5b

Browse files
committed
feat: [#483] allow upload torrent with application/octet-stream
HTTP header Content-Type. Some users are having problems uploading torrents becuase the client (browser or other clients) use the HTTP header Contetnt-Type: `application/octet-stream` instead of: `application/x-bittorrent` It seems that the reason is they don't have any application associated to that extension. So it uses the generic binary file mime type. The MIME type can be inferred from the file extension. If the system or application uploading the file has a specific association for .torrent files, it might set the MIME type to application/x-bittorrent. In the absence of such association, it might fall back to the generic application/octet-stream.
1 parent f7c1aab commit 8a97b5b

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

src/web/api/client/v1/responses.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl BinaryResponse {
7171

7272
#[must_use]
7373
pub fn is_a_bit_torrent_file(&self) -> bool {
74-
self.is_ok() && self.is_bittorrent_content_type()
74+
self.is_ok() && (self.is_bittorrent_content_type() || self.is_octet_stream_content_type())
7575
}
7676

7777
#[must_use]
@@ -82,6 +82,14 @@ impl BinaryResponse {
8282
false
8383
}
8484

85+
#[must_use]
86+
pub fn is_octet_stream_content_type(&self) -> bool {
87+
if let Some(content_type) = &self.content_type {
88+
return content_type == "application/octet-stream";
89+
}
90+
false
91+
}
92+
8593
#[must_use]
8694
pub fn is_ok(&self) -> bool {
8795
self.status == 200

src/web/api/server/v1/contexts/torrent/errors.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ pub enum Request {
2121
#[display(fmt = "torrent tags string is not a valid JSON.")]
2222
TagsArrayIsNotValidJson,
2323

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

2729
#[display(fmt = "cannot write uploaded torrent bytes (binary file) into memory.")]

src/web/api/server/v1/contexts/torrent/handlers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pub async fn create_random_torrent_handler(State(_app_data): State<Arc<AppData>>
301301
///
302302
/// - The text fields do not contain a valid UTF8 string.
303303
/// - The torrent file data is not valid because:
304-
/// - The content type is not `application/x-bittorrent`.
304+
/// - The content type is not `application/x-bittorrent` or `application/octet-stream`.
305305
/// - The multipart content is invalid.
306306
/// - The torrent file pieces key has a length that is not a multiple of 20.
307307
/// - The binary data cannot be decoded as a torrent file.
@@ -350,7 +350,7 @@ async fn build_add_torrent_request_from_payload(mut payload: Multipart) -> Resul
350350
"torrent" => {
351351
let content_type = field.content_type().unwrap();
352352

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

tests/common/responses.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl BinaryResponse {
5454
}
5555
}
5656
pub fn is_a_bit_torrent_file(&self) -> bool {
57-
self.is_ok() && self.is_bittorrent_content_type()
57+
self.is_ok() && (self.is_bittorrent_content_type() || self.is_octet_stream_content_type())
5858
}
5959

6060
pub fn is_bittorrent_content_type(&self) -> bool {
@@ -64,6 +64,13 @@ impl BinaryResponse {
6464
false
6565
}
6666

67+
pub fn is_octet_stream_content_type(&self) -> bool {
68+
if let Some(content_type) = &self.content_type {
69+
return content_type == "application/octet-stream";
70+
}
71+
false
72+
}
73+
6774
pub fn is_ok(&self) -> bool {
6875
self.status == 200
6976
}

0 commit comments

Comments
 (0)