Skip to content

Commit 2ce4fef

Browse files
authored
Respect content_type prop (#100)
1 parent b254b2f commit 2ce4fef

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/s3/builders/put_object.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub struct CreateMultipartUpload {
5252
tags: Option<HashMap<String, String>>,
5353
retention: Option<Retention>,
5454
legal_hold: bool,
55+
content_type: Option<String>,
5556
}
5657

5758
impl CreateMultipartUpload {
@@ -108,6 +109,11 @@ impl CreateMultipartUpload {
108109
self
109110
}
110111

112+
pub fn content_type(mut self, content_type: Option<String>) -> Self {
113+
self.content_type = content_type;
114+
self
115+
}
116+
111117
fn get_headers(&self) -> Result<Multimap, Error> {
112118
object_write_args_headers(
113119
self.extra_headers.as_ref(),
@@ -116,6 +122,7 @@ impl CreateMultipartUpload {
116122
self.tags.as_ref(),
117123
self.retention.as_ref(),
118124
self.legal_hold,
125+
self.content_type.as_ref(),
119126
)
120127
}
121128

@@ -376,6 +383,7 @@ pub struct UploadPart {
376383
retention: Option<Retention>,
377384
legal_hold: bool,
378385
data: SegmentedBytes,
386+
content_type: Option<String>,
379387

380388
// This is used only when this struct is used for PutObject.
381389
user_metadata: Option<Multimap>,
@@ -452,6 +460,7 @@ impl UploadPart {
452460
self.tags.as_ref(),
453461
self.retention.as_ref(),
454462
self.legal_hold,
463+
self.content_type.as_ref(),
455464
)
456465
}
457466

@@ -597,6 +606,7 @@ fn object_write_args_headers(
597606
tags: Option<&HashMap<String, String>>,
598607
retention: Option<&Retention>,
599608
legal_hold: bool,
609+
content_type: Option<&String>,
600610
) -> Result<Multimap, Error> {
601611
let mut map = Multimap::new();
602612

@@ -662,7 +672,10 @@ fn object_write_args_headers(
662672
if !map.contains_key("Content-Type") {
663673
map.insert(
664674
String::from("Content-Type"),
665-
String::from("application/octet-stream"),
675+
match content_type {
676+
Some(content_type) => content_type.clone(),
677+
None => String::from("application/octet-stream"),
678+
},
666679
);
667680
}
668681

@@ -686,7 +699,7 @@ pub struct PutObjectContent {
686699
retention: Option<Retention>,
687700
legal_hold: bool,
688701
part_size: Size,
689-
content_type: String,
702+
content_type: Option<String>,
690703

691704
// source data
692705
input_content: ObjectContent,
@@ -713,7 +726,7 @@ impl PutObjectContent {
713726
retention: None,
714727
legal_hold: false,
715728
part_size: Size::Unknown,
716-
content_type: String::from("application/octet-stream"),
729+
content_type: None,
717730
content_stream: ContentStream::empty(),
718731
part_count: None,
719732
}
@@ -770,7 +783,7 @@ impl PutObjectContent {
770783
}
771784

772785
pub fn content_type(mut self, content_type: String) -> Self {
773-
self.content_type = content_type;
786+
self.content_type = Some(content_type);
774787
self
775788
}
776789

@@ -965,6 +978,7 @@ impl PutObjectContent {
965978
part_number: None,
966979
upload_id: None,
967980
data,
981+
content_type: self.content_type.clone(),
968982
})
969983
}
970984

@@ -990,6 +1004,7 @@ impl PutObjectContent {
9901004
part_number: Some(part_number),
9911005
upload_id: Some(upload_id.to_string()),
9921006
data,
1007+
content_type: self.content_type.clone(),
9931008
}
9941009
}
9951010

@@ -1023,6 +1038,7 @@ impl PutObjectContent {
10231038
tags: self.tags.clone(),
10241039
retention: self.retention.clone(),
10251040
legal_hold: self.legal_hold,
1041+
content_type: self.content_type.clone(),
10261042
}
10271043
}
10281044
}

tests/tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use async_std::task;
1717
use bytes::Bytes;
1818
use chrono::Duration;
1919
use futures_util::Stream;
20+
use http::header;
2021
use hyper::http::Method;
2122

2223
use minio::s3::builders::{ObjectContent, ObjectToDelete};
@@ -339,6 +340,7 @@ impl ClientTest {
339340
&object_name,
340341
ObjectContent::new_from_stream(data_src, Some(*size)),
341342
)
343+
.content_type(String::from("image/jpeg"))
342344
.send()
343345
.await
344346
.unwrap();
@@ -351,6 +353,10 @@ impl ClientTest {
351353
.unwrap();
352354
assert_eq!(resp.size, *size as usize);
353355
assert_eq!(resp.etag, etag);
356+
assert_eq!(
357+
resp.headers.get(header::CONTENT_TYPE).unwrap(),
358+
"image/jpeg"
359+
);
354360
self.client
355361
.remove_object(&self.test_bucket, object_name.as_str())
356362
.send()

0 commit comments

Comments
 (0)