Initial API setup

Right now we are just listing files in a given subdirectory with not
authentication.
This commit is contained in:
Cameron Cordes
2020-07-07 19:53:12 -04:00
commit 36f7351627
6 changed files with 1907 additions and 0 deletions

33
src/files.rs Normal file
View 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";
}