Skip to content

Commit

Permalink
feat(services/cos): Rewrite the method signatures using OpWrite (#3070)
Browse files Browse the repository at this point in the history
* feat(services/cos)!: Rewrite the method signatures using OpWrite

* chore(services/cos): change Default::default to OpWrite::default
  • Loading branch information
acehinnnqru authored Sep 14, 2023
1 parent 6187b6d commit 4ebb69b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 37 deletions.
21 changes: 10 additions & 11 deletions core/src/services/cos/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,12 @@ impl Accessor for CosBackend {
}

async fn create_dir(&self, path: &str, _: OpCreateDir) -> Result<RpCreateDir> {
let mut req =
self.core
.cos_put_object_request(path, Some(0), None, None, None, AsyncBody::Empty)?;
let mut req = self.core.cos_put_object_request(
path,
Some(0),
&OpWrite::default(),
AsyncBody::Empty,
)?;

self.core.sign(&mut req).await?;

Expand Down Expand Up @@ -409,14 +412,10 @@ impl Accessor for CosBackend {
v.if_match(),
v.if_none_match(),
)?,
PresignOperation::Write(v) => self.core.cos_put_object_request(
path,
None,
v.content_type(),
v.content_disposition(),
v.cache_control(),
AsyncBody::Empty,
)?,
PresignOperation::Write(v) => {
self.core
.cos_put_object_request(path, None, v, AsyncBody::Empty)?
}
};
self.core.sign_query(&mut req, args.expire()).await?;

Expand Down
20 changes: 8 additions & 12 deletions core/src/services/cos/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ impl CosCore {
&self,
path: &str,
size: Option<u64>,
content_type: Option<&str>,
content_disposition: Option<&str>,
cache_control: Option<&str>,
args: &OpWrite,
body: AsyncBody,
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);
Expand All @@ -165,13 +163,13 @@ impl CosCore {
if let Some(size) = size {
req = req.header(CONTENT_LENGTH, size)
}
if let Some(cache_control) = cache_control {
if let Some(cache_control) = args.cache_control() {
req = req.header(CACHE_CONTROL, cache_control)
}
if let Some(pos) = content_disposition {
if let Some(pos) = args.content_disposition() {
req = req.header(CONTENT_DISPOSITION, pos)
}
if let Some(mime) = content_type {
if let Some(mime) = args.content_type() {
req = req.header(CONTENT_TYPE, mime)
}

Expand Down Expand Up @@ -334,25 +332,23 @@ impl CosCore {
pub async fn cos_initiate_multipart_upload(
&self,
path: &str,
content_type: Option<&str>,
content_disposition: Option<&str>,
cache_control: Option<&str>,
args: &OpWrite,
) -> Result<Response<IncomingAsyncBody>> {
let p = build_abs_path(&self.root, path);

let url = format!("{}/{}?uploads", self.endpoint, percent_encode_path(&p));

let mut req = Request::post(&url);

if let Some(mime) = content_type {
if let Some(mime) = args.content_type() {
req = req.header(CONTENT_TYPE, mime)
}

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

if let Some(cache_control) = cache_control {
if let Some(cache_control) = args.cache_control() {
req = req.header(CACHE_CONTROL, cache_control)
}

Expand Down
18 changes: 4 additions & 14 deletions core/src/services/cos/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,9 @@ impl CosWriter {
#[async_trait]
impl oio::MultipartUploadWrite for CosWriter {
async fn write_once(&self, size: u64, body: AsyncBody) -> Result<()> {
let mut req = self.core.cos_put_object_request(
&self.path,
Some(size),
self.op.content_type(),
self.op.content_disposition(),
self.op.cache_control(),
body,
)?;
let mut req = self
.core
.cos_put_object_request(&self.path, Some(size), &self.op, body)?;

self.core.sign(&mut req).await?;

Expand All @@ -75,12 +70,7 @@ impl oio::MultipartUploadWrite for CosWriter {
async fn initiate_part(&self) -> Result<String> {
let resp = self
.core
.cos_initiate_multipart_upload(
&self.path,
self.op.content_type(),
self.op.content_disposition(),
self.op.cache_control(),
)
.cos_initiate_multipart_upload(&self.path, &self.op)
.await?;

let status = resp.status();
Expand Down

0 comments on commit 4ebb69b

Please sign in to comment.