Skip to content

Commit

Permalink
feat: allow creating UploadedPart
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelFreeze committed Oct 22, 2023
1 parent ebc64e6 commit a72dddb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
2 changes: 0 additions & 2 deletions lib/worker-sys/src/types/r2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod object;
mod object_body;
mod objects;
mod range;
mod uploaded_part;

pub use bucket::*;
pub use http_metadata::*;
Expand All @@ -14,4 +13,3 @@ pub use object::*;
pub use object_body::*;
pub use objects::*;
pub use range::*;
pub use uploaded_part::*;
14 changes: 0 additions & 14 deletions lib/worker-sys/src/types/r2/uploaded_part.rs

This file was deleted.

2 changes: 1 addition & 1 deletion lib/worker/src/durable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ pub struct SetAlarmOptions {
/// }
/// }
/// ```
// #[async_trait(?Send)]
#[allow(async_fn_in_trait)]
pub trait DurableObject {
fn new(state: State, env: Env) -> Self;

Expand Down
56 changes: 38 additions & 18 deletions lib/worker/src/r2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use betterworker_sys::{
FixedLengthStream as EdgeFixedLengthStream, R2Bucket as EdgeR2Bucket,
R2MultipartUpload as EdgeR2MultipartUpload, R2Object as EdgeR2Object,
R2ObjectBody as EdgeR2ObjectBody, R2Objects as EdgeR2Objects,
R2UploadedPart as EdgeR2UploadedPart,
};
pub use builder::*;
use js_sys::{JsString, Object, Reflect, Uint8Array};
use send_wrapper::SendWrapper;
use serde::{Deserialize, Serialize};
use wasm_bindgen::{JsCast, JsValue};

use crate::date::Date;
Expand Down Expand Up @@ -328,17 +328,40 @@ impl<'body> ObjectBody<'body> {
/// [UploadedPart] objects are returned from
/// [upload_part](MultipartUpload::upload_part) operations and must be passed to
/// the [complete](MultipartUpload::complete) operation.
#[derive(Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UploadedPart {
inner: SendWrapper<EdgeR2UploadedPart>,
part_number: u16,
etag: String,
}

impl UploadedPart {
pub fn new(part_number: u16, etag: String) -> Self {
Self { part_number, etag }
}

pub fn part_number(&self) -> u16 {
self.inner.part_number()
self.part_number
}

pub fn etag(&self) -> String {
self.inner.etag()
pub fn etag(&self) -> &str {
&self.etag
}
}

impl TryFrom<JsValue> for UploadedPart {
type Error = WorkerError;

fn try_from(value: JsValue) -> std::result::Result<Self, Self::Error> {
serde_wasm_bindgen::from_value(value).map_err(WorkerError::from)
}
}

impl TryInto<JsValue> for UploadedPart {
type Error = WorkerError;

fn try_into(self) -> std::result::Result<JsValue, Self::Error> {
serde_wasm_bindgen::to_value(&self).map_err(WorkerError::from)
}
}

Expand Down Expand Up @@ -371,11 +394,11 @@ impl MultipartUpload {
&self, part_number: u16, value: impl Into<Data>,
) -> Result<UploadedPart> {
let fut = future_from_promise(self.inner.upload_part(part_number, value.into().into()));
let uploaded_part = fut.await.map_err(WorkerError::from_promise_err)?;

Ok(UploadedPart {
inner: SendWrapper::new(uploaded_part.into()),
})
let uploaded_part = fut
.await
.map_err(WorkerError::from_promise_err)?
.try_into()?;
Ok(uploaded_part)
}

/// Aborts the multipart upload.
Expand All @@ -391,14 +414,11 @@ impl MultipartUpload {
pub async fn complete(
self, uploaded_parts: impl IntoIterator<Item = UploadedPart>,
) -> Result<R2Object> {
let fut = future_from_promise(
self.inner.complete(
uploaded_parts
.into_iter()
.map(|part| part.inner.take().into())
.collect(),
),
);
let uploaded_parts = uploaded_parts
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>>>()?;
let fut = future_from_promise(self.inner.complete(uploaded_parts));
let object = fut.await.map_err(WorkerError::from_promise_err)?;
Ok(R2Object {
inner: ObjectInner::Body(SendWrapper::new(object.into())),
Expand Down

0 comments on commit a72dddb

Please sign in to comment.