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

feat: add content-disposition support for services #1350

Merged
merged 1 commit into from
Feb 15, 2023
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
20 changes: 20 additions & 0 deletions src/raw/http_util/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use http::header::HeaderName;
use http::header::CONTENT_DISPOSITION;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_RANGE;
use http::header::CONTENT_TYPE;
Expand Down Expand Up @@ -144,6 +145,21 @@ pub fn parse_etag(headers: &HeaderMap) -> Result<Option<&str>> {
}
}

/// Parse Content-Disposition for header map
pub fn parse_content_disposition(headers: &HeaderMap) -> Result<Option<&str>> {
match headers.get(CONTENT_DISPOSITION) {
None => Ok(None),
Some(v) => Ok(Some(v.to_str().map_err(|e| {
Error::new(
ErrorKind::Unexpected,
"header value has to be valid utf-8 string",
)
.with_operation("http_util::parse_content_disposition")
.set_source(e)
})?)),
}
}

/// parse_into_object_metadata will parse standards http headers into ObjectMetadata.
///
/// # Notes
Expand Down Expand Up @@ -183,5 +199,9 @@ pub fn parse_into_object_metadata(path: &str, headers: &HeaderMap) -> Result<Obj
m.set_last_modified(v);
}

if let Some(v) = parse_content_disposition(headers)? {
m.set_content_disposition(v);
}

Ok(m)
}
1 change: 1 addition & 0 deletions src/raw/http_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub use body::Body;
pub use body::IncomingAsyncBody;

mod header;
pub use header::parse_content_disposition;
pub use header::parse_content_length;
pub use header::parse_content_md5;
pub use header::parse_content_range;
Expand Down
17 changes: 14 additions & 3 deletions src/services/azdfs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::mem;
use std::sync::Arc;

use async_trait::async_trait;
use http::header::CONTENT_DISPOSITION;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::Request;
Expand Down Expand Up @@ -324,7 +325,7 @@ impl Accessor for AzdfsBackend {
_ => unimplemented!("not supported object mode"),
};

let mut req = self.azdfs_create_request(path, resource, None, AsyncBody::Empty)?;
let mut req = self.azdfs_create_request(path, resource, None, None, AsyncBody::Empty)?;

self.signer.sign(&mut req).map_err(new_request_sign_error)?;

Expand Down Expand Up @@ -356,8 +357,13 @@ impl Accessor for AzdfsBackend {
}

async fn write(&self, path: &str, args: OpWrite, r: input::Reader) -> Result<RpWrite> {
let mut req =
self.azdfs_create_request(path, "file", args.content_type(), AsyncBody::Empty)?;
let mut req = self.azdfs_create_request(
path,
"file",
args.content_type(),
args.content_disposition(),
AsyncBody::Empty,
)?;

self.signer.sign(&mut req).map_err(new_request_sign_error)?;

Expand Down Expand Up @@ -483,6 +489,7 @@ impl AzdfsBackend {
path: &str,
resource: &str,
content_type: Option<&str>,
content_disposition: Option<&str>,
body: AsyncBody,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path)
Expand All @@ -505,6 +512,10 @@ impl AzdfsBackend {
req = req.header(CONTENT_TYPE, ty)
}

if let Some(pos) = content_disposition {
req = req.header(CONTENT_DISPOSITION, pos)
}

// Set body
let req = req.body(body).map_err(new_request_build_error)?;

Expand Down
4 changes: 4 additions & 0 deletions src/services/ipfs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ impl Accessor for IpfsBackend {
m.set_mode(ObjectMode::DIR);
}

if let Some(v) = parse_content_disposition(resp.headers())? {
m.set_content_disposition(v);
}

Ok(RpStat::new(m))
}
StatusCode::FOUND | StatusCode::MOVED_PERMANENTLY => {
Expand Down
30 changes: 25 additions & 5 deletions src/services/oss/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::fmt::Formatter;
use std::sync::Arc;

use async_trait::async_trait;
use http::header::CONTENT_DISPOSITION;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::header::RANGE;
Expand Down Expand Up @@ -407,7 +408,7 @@ impl Accessor for OssBackend {

async fn create(&self, path: &str, _: OpCreate) -> Result<RpCreate> {
let resp = self
.oss_put_object(path, None, None, AsyncBody::Empty)
.oss_put_object(path, None, None, None, AsyncBody::Empty)
.await?;
let status = resp.status();

Expand Down Expand Up @@ -440,6 +441,7 @@ impl Accessor for OssBackend {
path,
Some(args.size()),
args.content_type(),
args.content_disposition(),
AsyncBody::Reader(r),
)
.await?;
Expand Down Expand Up @@ -506,9 +508,14 @@ impl Accessor for OssBackend {
let mut req = match args.operation() {
PresignOperation::Stat(_) => self.oss_head_object_request(path, true)?,
PresignOperation::Read(v) => self.oss_get_object_request(path, v.range(), true)?,
PresignOperation::Write(v) => {
self.oss_put_object_request(path, None, v.content_type(), AsyncBody::Empty, true)?
}
PresignOperation::Write(v) => self.oss_put_object_request(
path,
None,
v.content_type(),
v.content_disposition(),
AsyncBody::Empty,
true,
)?,
_ => {
return Err(Error::new(
ErrorKind::Unsupported,
Expand Down Expand Up @@ -538,6 +545,7 @@ impl OssBackend {
path: &str,
size: Option<u64>,
content_type: Option<&str>,
content_disposition: Option<&str>,
body: AsyncBody,
is_presign: bool,
) -> Result<Request<AsyncBody>> {
Expand All @@ -553,6 +561,10 @@ impl OssBackend {
req = req.header(CONTENT_TYPE, mime);
}

if let Some(pos) = content_disposition {
req = req.header(CONTENT_DISPOSITION, pos);
}

let req = req.body(body).map_err(new_request_build_error)?;
Ok(req)
}
Expand Down Expand Up @@ -659,9 +671,17 @@ impl OssBackend {
path: &str,
size: Option<u64>,
content_type: Option<&str>,
content_disposition: Option<&str>,
body: AsyncBody,
) -> Result<Response<IncomingAsyncBody>> {
let mut req = self.oss_put_object_request(path, size, content_type, body, false)?;
let mut req = self.oss_put_object_request(
path,
size,
content_type,
content_disposition,
body,
false,
)?;

self.signer.sign(&mut req).map_err(new_request_sign_error)?;
self.client.send_async(req).await
Expand Down
11 changes: 9 additions & 2 deletions src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use base64::Engine;
use bytes::Buf;
use bytes::Bytes;
use http::header::HeaderName;
use http::header::CONTENT_DISPOSITION;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::HeaderValue;
Expand Down Expand Up @@ -1126,7 +1127,7 @@ impl Accessor for S3Backend {
}

async fn create(&self, path: &str, _: OpCreate) -> Result<RpCreate> {
let mut req = self.s3_put_object_request(path, Some(0), None, AsyncBody::Empty)?;
let mut req = self.s3_put_object_request(path, Some(0), None, None, AsyncBody::Empty)?;

self.signer.sign(&mut req).map_err(new_request_sign_error)?;

Expand Down Expand Up @@ -1162,6 +1163,7 @@ impl Accessor for S3Backend {
path,
Some(args.size()),
args.content_type(),
args.content_disposition(),
AsyncBody::Reader(r),
)?;

Expand Down Expand Up @@ -1230,7 +1232,7 @@ impl Accessor for S3Backend {
PresignOperation::Stat(_) => self.s3_head_object_request(path)?,
PresignOperation::Read(v) => self.s3_get_object_request(path, v.range())?,
PresignOperation::Write(_) => {
self.s3_put_object_request(path, None, None, AsyncBody::Empty)?
self.s3_put_object_request(path, None, None, None, AsyncBody::Empty)?
}
PresignOperation::WriteMultipart(v) => self.s3_upload_part_request(
path,
Expand Down Expand Up @@ -1415,6 +1417,7 @@ impl S3Backend {
path: &str,
size: Option<u64>,
content_type: Option<&str>,
content_disposition: Option<&str>,
body: AsyncBody,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);
Expand All @@ -1431,6 +1434,10 @@ impl S3Backend {
req = req.header(CONTENT_TYPE, mime)
}

if let Some(pos) = content_disposition {
req = req.header(CONTENT_DISPOSITION, pos)
}

// Set SSE headers.
req = self.insert_sse_headers(req, true);

Expand Down
9 changes: 8 additions & 1 deletion src/services/webdav/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use async_trait::async_trait;
use base64::engine::general_purpose;
use base64::Engine;
use http::header::AUTHORIZATION;
use http::header::CONTENT_DISPOSITION;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::Request;
Expand Down Expand Up @@ -272,7 +273,7 @@ impl Accessor for WebdavBackend {

async fn create(&self, path: &str, _: OpCreate) -> Result<RpCreate> {
let resp = self
.webdav_put(path, Some(0), None, AsyncBody::Empty)
.webdav_put(path, Some(0), None, None, AsyncBody::Empty)
.await?;

let status = resp.status();
Expand Down Expand Up @@ -311,6 +312,7 @@ impl Accessor for WebdavBackend {
path,
Some(args.size()),
args.content_type(),
args.content_disposition(),
AsyncBody::Reader(r),
)
.await?;
Expand Down Expand Up @@ -387,6 +389,7 @@ impl WebdavBackend {
path: &str,
size: Option<u64>,
content_type: Option<&str>,
content_disposition: Option<&str>,
body: AsyncBody,
) -> Result<Response<IncomingAsyncBody>> {
let p = build_abs_path(&self.root, path);
Expand All @@ -403,6 +406,10 @@ impl WebdavBackend {
req = req.header(CONTENT_TYPE, mime)
}

if let Some(cd) = content_disposition {
req = req.header(CONTENT_DISPOSITION, cd)
}

// Set body
let req = req.body(body).map_err(new_request_build_error)?;

Expand Down