opendal/services/pcloud/
writer.rs1use std::sync::Arc;
19
20use bytes::Buf;
21use http::StatusCode;
22
23use super::core::PcloudCore;
24use super::error::parse_error;
25use super::error::PcloudError;
26use crate::raw::*;
27use crate::*;
28
29pub type PcloudWriters = oio::OneShotWriter<PcloudWriter>;
30
31pub struct PcloudWriter {
32 core: Arc<PcloudCore>,
33 path: String,
34}
35
36impl PcloudWriter {
37 pub fn new(core: Arc<PcloudCore>, path: String) -> Self {
38 PcloudWriter { core, path }
39 }
40}
41
42impl oio::OneShotWrite for PcloudWriter {
43 async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
44 self.core.ensure_dir_exists(&self.path).await?;
45
46 let resp = self.core.upload_file(&self.path, bs).await?;
47
48 let status = resp.status();
49
50 match status {
51 StatusCode::OK => {
52 let bs = resp.into_body();
53 let resp: PcloudError =
54 serde_json::from_reader(bs.reader()).map_err(new_json_deserialize_error)?;
55 let result = resp.result;
56
57 if result != 0 {
58 return Err(Error::new(ErrorKind::Unexpected, format!("{resp:?}")));
59 }
60
61 Ok(Metadata::default())
62 }
63 _ => Err(parse_error(resp)),
64 }
65 }
66}