use rocket::fs::NamedFile;
use std::path::{Path, PathBuf};
use crate::common::thumbs_dir;

#[get("/<file..>")]
pub async fn get(file: PathBuf) -> Option<NamedFile> {
    let clips_dir = thumbs_dir();

    // Only serve jpg's and png's through this route
    let file_path = Path::new(&clips_dir).join(file);
    if file_path.is_file() {
        return match file_path.extension() {
            Some(ext) => {
                if ext == "jpg" || ext == "png" || ext == "jpeg" {
                    NamedFile::open(file_path).await.ok()
                } else {
                    println!("bad extension");
                    None
                }
            },
            None => None
        }
    } else {
        let path = Path::new("static/cantfindshit.gif");
        return NamedFile::open(path).await.ok();
    }
}
