monorepo/file-service/src/lib/mod.rs

59 lines
1.5 KiB
Rust
Raw Normal View History

use std::path::{Path, PathBuf};
use uuid::Uuid;
mod file;
mod fileinfo;
mod thumbnail;
pub mod utils;
pub use file::{File, FileError};
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),
}
}
pub fn list_files(&self) -> Vec<Result<File, FileError>> {
File::list(&self.files_root)
}
pub fn add_file(
&mut self,
temp_path: &PathBuf,
filename: &Option<PathBuf>,
) -> Result<File, FileError> {
let id = Uuid::new_v4().hyphenated().to_string();
File::new(&id, &self.files_root, temp_path, filename)
}
pub fn delete_file(&mut self, id: String) -> Result<(), FileError> {
let f = File::open(&id, &self.files_root)?;
f.delete()
}
pub fn get_metadata(&self, id: String) -> Result<FileInfo, FileError> {
FileInfo::open(&id, &self.files_root)
}
pub fn get_file(&self, id: &str) -> Result<(FileInfo, std::fs::File), FileError> {
let f = File::open(&id, &self.files_root)?;
let info = f.info();
let stream = f.stream()?;
Ok((info, stream))
}
pub fn get_thumbnail(&self, id: &str) -> Result<(FileInfo, std::fs::File), FileError> {
let f = File::open(id, &self.files_root)?;
let stream = f.thumbnail().stream()?;
Ok((f.info(), stream))
}
}