use std::pin::Pin;
use bytes::Buf;
use openssh_sftp_client::file::File;
use openssh_sftp_client::file::TokioCompatFile;
use tokio::io::AsyncWriteExt;
use opendal_core::raw::*;
use opendal_core::*;
pub struct SftpWriter {
file: Pin<Box<TokioCompatFile>>,
}
impl SftpWriter {
pub fn new(file: File) -> Self {
SftpWriter {
file: Box::pin(TokioCompatFile::new(file)),
}
}
}
impl oio::Write for SftpWriter {
async fn write(&mut self, mut bs: Buffer) -> Result<()> {
while bs.has_remaining() {
let n = self
.file
.write(bs.chunk())
.await
.map_err(new_std_io_error)?;
bs.advance(n);
}
Ok(())
}
async fn close(&mut self) -> Result<Metadata> {
self.file.shutdown().await.map_err(new_std_io_error)?;
Ok(Metadata::default())
}
async fn abort(&mut self) -> Result<()> {
Err(Error::new(
ErrorKind::Unsupported,
"SftpWriter doesn't support abort",
))
}
}