shrupl/src/sharry/file/checked.rs

76 lines
2.1 KiB
Rust
Raw Normal View History

use std::{
ffi::OsStr,
fs,
io::{self, ErrorKind},
path::{Path, PathBuf},
};
use log::debug;
use serde::{Deserialize, Serialize};
use ureq::http::StatusCode;
use super::{Alias, FileUploading, Share, SharryAlias};
2025-06-04 16:44:24 +00:00
#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct FileChecked {
path: PathBuf,
}
impl FileChecked {
pub fn new(value: impl AsRef<Path>) -> io::Result<Self> {
let meta = fs::metadata(&value)?;
if meta.is_file() {
Ok(Self {
path: fs::canonicalize(&value)?,
})
} else {
Err(io::Error::new(
ErrorKind::InvalidInput,
"Not a regular file",
))
}
}
2025-06-05 17:37:35 +00:00
pub fn file_name(&self) -> &str {
self.path.file_name().unwrap().to_str().unwrap()
}
pub fn start_upload(
self,
http: &ureq::Agent,
alias: &Alias,
share: &Share,
) -> io::Result<FileUploading> {
let size = usize::try_from(fs::metadata(&self.path)?.len()).map_err(io::Error::other)?;
let res = {
let endpoint = alias.get_endpoint(format!("alias/upload/{}/files/tus", share.id));
let name = (self.path.file_name().and_then(OsStr::to_str))
.ok_or_else(|| io::Error::new(ErrorKind::NotFound, "bad file name"))?
.to_string();
(http.post(endpoint))
.sharry_header(alias)
.header("Sharry-File-Name", &name)
.header("Upload-Length", size)
.send_empty()
.map_err(io::Error::other)?
};
if res.status() != StatusCode::CREATED {
return Err(io::Error::other("unexpected response status"));
}
let location = (res.headers().get("Location"))
.ok_or_else(|| io::Error::other("Location header not found"))?
.to_str()
.map_err(|_| io::Error::other("Location header invalid"))?
.to_string();
debug!("patch uri: {location}");
2025-06-04 16:58:18 +00:00
Ok(FileUploading::new(self.path, size, location))
}
}