use crate::system::LocalPath;
use crate::Operation;
use crate::Reason::UnknownFileSystem;
use crate::{Error, FilePath, Headers};
use std::io::Write;
impl FilePath {
#[cfg_attr(not(feature = "r2"), allow(unused_variables))]
pub fn write_with_headers<D>(&self, data: D, headers: &Headers) -> Result<(), Error>
where
D: AsRef<[u8]>,
{
if let Some(path) = LocalPath::new(self.path()) {
return path.overwrite(data.as_ref());
}
#[cfg(feature = "r2")]
if let Some(path) = crate::R2Path::new(self.path()) {
return path.write_with_headers(data, headers);
}
Err(Error::new(self.clone(), Operation::Write, UnknownFileSystem))
}
#[cfg_attr(not(feature = "r2"), allow(unused_variables))]
pub fn write_with_headers_if_not_exists<D>(
&self,
data: D,
headers: &Headers,
) -> Result<bool, Error>
where
D: AsRef<[u8]>,
{
if let Some(path) = LocalPath::new(self.path()) {
return if let Some(mut write) = path.write_if_not_exists()? {
write
.write_all(data.as_ref())
.map_err(|e| Error::from_source(self.clone(), Operation::Write, e))?;
write
.close()
.map_err(|e| Error::from_source(self.clone(), Operation::Write, e))?;
Ok(true)
} else {
Ok(false)
};
}
#[cfg(feature = "r2")]
if let Some(path) = crate::R2Path::new(self.path()) {
return path.write_with_headers_if_not_exists(data, headers);
}
Err(Error::new(self.clone(), Operation::Write, UnknownFileSystem))
}
}