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

57 lines
1.4 KiB
Rust
Raw Normal View History

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