2023-09-19 22:55:53 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
mod file;
|
|
|
|
mod fileinfo;
|
|
|
|
mod thumbnail;
|
2023-09-21 03:06:34 +00:00
|
|
|
pub mod utils;
|
2023-09-19 22:55:53 +00:00
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
pub use file::{File, FileError};
|
2023-09-19 22:55:53 +00:00
|
|
|
pub use fileinfo::FileInfo;
|
|
|
|
pub use thumbnail::Thumbnail;
|
|
|
|
|
|
|
|
pub struct App {
|
|
|
|
files_root: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
pub fn new(files_root: &Path) -> App {
|
|
|
|
App {
|
|
|
|
files_root: PathBuf::from(files_root),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
pub fn list_files(&self) -> Vec<Result<File, FileError>> {
|
2023-09-19 22:55:53 +00:00
|
|
|
File::list(&self.files_root)
|
|
|
|
}
|
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
pub fn add_file(
|
|
|
|
&mut self,
|
|
|
|
temp_path: &PathBuf,
|
|
|
|
filename: &Option<PathBuf>,
|
|
|
|
) -> Result<File, FileError> {
|
2023-09-19 22:55:53 +00:00
|
|
|
let id = Uuid::new_v4().hyphenated().to_string();
|
|
|
|
File::new(&id, &self.files_root, temp_path, filename)
|
|
|
|
}
|
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
pub fn delete_file(&mut self, id: String) -> Result<(), FileError> {
|
2023-09-19 22:55:53 +00:00
|
|
|
let f = File::open(&id, &self.files_root)?;
|
|
|
|
f.delete()
|
|
|
|
}
|
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
pub fn get_metadata(&self, id: String) -> Result<FileInfo, FileError> {
|
2023-09-19 22:55:53 +00:00
|
|
|
FileInfo::open(&id, &self.files_root)
|
|
|
|
}
|
|
|
|
|
2023-09-22 02:15:58 +00:00
|
|
|
pub fn get_file(&self, id: &str) -> Result<(FileInfo, std::fs::File), FileError> {
|
2023-09-19 22:55:53 +00:00
|
|
|
let f = File::open(&id, &self.files_root)?;
|
|
|
|
let info = f.info();
|
|
|
|
let stream = f.stream()?;
|
|
|
|
Ok((info, stream))
|
|
|
|
}
|
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
pub fn get_thumbnail(&self, id: &str) -> Result<(FileInfo, std::fs::File), FileError> {
|
2023-09-19 22:55:53 +00:00
|
|
|
let f = File::open(id, &self.files_root)?;
|
|
|
|
let stream = f.thumbnail().stream()?;
|
|
|
|
Ok((f.info(), stream))
|
|
|
|
}
|
|
|
|
}
|