Initial API setup
Right now we are just listing files in a given subdirectory with not authentication.
This commit is contained in:
33
src/files.rs
Normal file
33
src/files.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::read_dir;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub fn list_files(dir: PathBuf) -> io::Result<Vec<PathBuf>> {
|
||||
let files = read_dir(dir)?
|
||||
.map(|res| res.unwrap())
|
||||
.filter(|entry| is_image_or_video(&entry.path()) || entry.file_type().unwrap().is_dir())
|
||||
.map(|entry| entry.path())
|
||||
.map(|path: PathBuf| {
|
||||
let relative = path.strip_prefix(std::env::current_dir().unwrap()).unwrap();
|
||||
relative.to_path_buf()
|
||||
})
|
||||
.collect::<Vec<PathBuf>>();
|
||||
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
fn is_image_or_video(path: &Path) -> bool {
|
||||
let extension = &path
|
||||
.extension()
|
||||
.unwrap_or_else(|| OsStr::new(""))
|
||||
.to_str()
|
||||
.unwrap_or_else(|| "")
|
||||
.to_lowercase();
|
||||
|
||||
return extension == &"png"
|
||||
|| extension == &"jpg"
|
||||
|| extension == &"jpeg"
|
||||
|| extension == &"rs"
|
||||
|| extension == &"mp4";
|
||||
}
|
||||
Reference in New Issue
Block a user