diff --git a/service/src/db.rs b/service/src/db.rs index 76aa9b5..84c33ba 100644 --- a/service/src/db.rs +++ b/service/src/db.rs @@ -15,7 +15,7 @@ pub struct Row { type Data = HashMap; -pub fn get_video(id: String) -> Option { +pub fn get_video(id: &str) -> Option { // Used to fetch video information from the db for client's to request // video and thumbnail files from us let db_file = fs::read_to_string(CONFIG.db_file) @@ -23,7 +23,7 @@ pub fn get_video(id: String) -> Option { let db_data: Data = json::from_str(&db_file) .expect("Unable to parse DB_FILE content"); - match db_data.get(&id) { + match db_data.get(id) { Some(video) => Some(Row { file: video.file.clone(), thumbnail: video.thumbnail.clone() diff --git a/service/src/main.rs b/service/src/main.rs index 66b0ac0..10793a0 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -14,6 +14,8 @@ const CONFIG: BackendConfig = BackendConfig { root: "/opt/clippable", db_file: "/opt/clippable/db.json" }; +const VIDEO_BASE_URI: &'static str = "/file/video"; + #[rocket::main] async fn main() { @@ -21,7 +23,7 @@ async fn main() { let _ = rocket::build() .mount("/static", FileServer::from("static")) .mount("/c", routes![video::index]) - .mount("/video", routes![video::file]) + .mount(VIDEO_BASE_URI, routes![video::file]) .attach(Template::fairing()) .launch().await; } diff --git a/service/src/video.rs b/service/src/video.rs index 4b852a2..a9d40a0 100644 --- a/service/src/video.rs +++ b/service/src/video.rs @@ -1,23 +1,22 @@ -use crate::db; +use crate::{CONFIG, VIDEO_BASE_URI, db}; use std::path::PathBuf; use rocket::fs::NamedFile; -use rocket_dyn_templates::Template; -use rocket_dyn_templates::context; +use rocket_dyn_templates::{Template, context}; #[get("/")] -pub fn index(video_id: String) -> Template { +pub fn index(video_id: &str) -> Template { // Read the db file we have to get the ID // Now we can fetch the row from the DB content match db::get_video(video_id) { Some(video) => { let path = PathBuf::from(&video.file); - Template::render("index", context! { - title: path.file_name(), - kind: path.extension(), - video: video.file + Template::render("video", context! { + title: path.to_str().unwrap(), + kind: path.extension().expect("No extension found").to_str().unwrap(), + video: format!("{}/{}", VIDEO_BASE_URI, video.file) }) }, - None => Template::render("index", context! { + None => Template::render("video", context! { title: "Not found" }) } @@ -25,9 +24,8 @@ pub fn index(video_id: String) -> Template { #[get("/")] -pub async fn file(path: PathBuf) -> Option { - if path.is_file() { - return NamedFile::open(path).await.ok(); - } - None +pub async fn file(path: PathBuf) -> String { + let path = path.to_str().unwrap_or(""); + println!("{}", path); + format!("{}{VIDEO_BASE_URI}/{path}", CONFIG.root) } \ No newline at end of file diff --git a/service/templates/index.html.tera b/service/templates/video.html.tera similarity index 83% rename from service/templates/index.html.tera rename to service/templates/video.html.tera index 299521e..0784428 100644 --- a/service/templates/index.html.tera +++ b/service/templates/video.html.tera @@ -7,7 +7,7 @@

{{ title }}

{% if video %} {% else %}

Nothing to see here