shrupl/src/sharry/file/checked.rs

50 lines
1.1 KiB
Rust
Raw Normal View History

use std::{
ffi::OsStr,
2025-06-05 22:08:24 +00:00
fs, io,
path::{Path, PathBuf},
};
use log::debug;
use serde::{Deserialize, Serialize};
2025-06-06 22:25:39 +00:00
use ureq::http::{HeaderValue, StatusCode};
use super::{FileUploading, SharryFile};
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(
2025-06-05 22:08:24 +00:00
io::ErrorKind::InvalidInput,
"Not a regular file",
))
}
}
}
2025-06-06 15:21:49 +00:00
impl<'t> SharryFile<'t> for FileChecked {
2025-06-08 17:13:01 +00:00
fn into_path(self) -> PathBuf {
self.path
}
2025-06-06 15:21:49 +00:00
/// get a reference to the file's name
///
2025-06-06 22:25:39 +00:00
/// Uses `SharryFile::extract_file_name`, which may **panic**!
2025-06-06 23:42:18 +00:00
fn get_name(&'t self) -> &'t str {
2025-06-06 15:21:49 +00:00
<Self as SharryFile>::extract_file_name(&self.path)
}
2025-06-06 23:42:18 +00:00
fn get_size(&self) -> u64 {
fs::metadata(&self.path).unwrap().len()
2025-06-06 15:21:49 +00:00
}
}