Compare commits

...

3 Commits

4 changed files with 21 additions and 27 deletions

View File

@ -4,7 +4,7 @@ use rocket::serde::json;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use crate::CONFIG; use crate::DB_FILE;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Row { pub struct Row {
@ -15,15 +15,15 @@ pub struct Row {
type Data = HashMap<String, Row>; type Data = HashMap<String, Row>;
pub fn get_video(id: String) -> Option<Row> { pub fn get_video(id: &str) -> Option<Row> {
// Used to fetch video information from the db for client's to request // Used to fetch video information from the db for client's to request
// video and thumbnail files from us // video and thumbnail files from us
let db_file = fs::read_to_string(CONFIG.db_file) let db_file = fs::read_to_string(DB_FILE)
.expect("Unable to read DB_FILE"); .expect("Unable to read DB_FILE");
let db_data: Data = json::from_str(&db_file) let db_data: Data = json::from_str(&db_file)
.expect("Unable to parse DB_FILE content"); .expect("Unable to parse DB_FILE content");
match db_data.get(&id) { match db_data.get(id) {
Some(video) => Some(Row { Some(video) => Some(Row {
file: video.file.clone(), file: video.file.clone(),
thumbnail: video.thumbnail.clone() thumbnail: video.thumbnail.clone()

View File

@ -5,15 +5,10 @@ use rocket::fs::FileServer;
mod video; mod video;
mod db; mod db;
pub struct BackendConfig { const FILES_VIDEO: &'static str = "/opt/clippable/file/video";
pub root: &'static str, const DB_FILE: &'static str = "/opt/clippable/db.json";
pub db_file: &'static str, const VIDEO_BASE_URI: &'static str = "/file/video";
}
const CONFIG: BackendConfig = BackendConfig {
root: "/opt/clippable",
db_file: "/opt/clippable/db.json"
};
#[rocket::main] #[rocket::main]
async fn main() { async fn main() {
@ -21,7 +16,7 @@ async fn main() {
let _ = rocket::build() let _ = rocket::build()
.mount("/static", FileServer::from("static")) .mount("/static", FileServer::from("static"))
.mount("/c", routes![video::index]) .mount("/c", routes![video::index])
.mount("/video", routes![video::file]) .mount(VIDEO_BASE_URI, routes![video::file])
.attach(Template::fairing()) .attach(Template::fairing())
.launch().await; .launch().await;
} }

View File

@ -1,23 +1,22 @@
use crate::db; use crate::{FILES_VIDEO, VIDEO_BASE_URI, db};
use std::path::PathBuf; use std::path::PathBuf;
use rocket::fs::NamedFile; use rocket::fs::NamedFile;
use rocket_dyn_templates::Template; use rocket_dyn_templates::{Template, context};
use rocket_dyn_templates::context;
#[get("/<video_id>")] #[get("/<video_id>")]
pub fn index(video_id: String) -> Template { pub fn index(video_id: &str) -> Template {
// Read the db file we have to get the ID // Read the db file we have to get the ID
// Now we can fetch the row from the DB content // Now we can fetch the row from the DB content
match db::get_video(video_id) { match db::get_video(video_id) {
Some(video) => { Some(video) => {
let path = PathBuf::from(&video.file); let path = PathBuf::from(&video.file);
Template::render("index", context! { Template::render("video", context! {
title: path.file_name(), title: path.to_str().unwrap(),
kind: path.extension(), kind: path.extension().expect("No extension found").to_str().unwrap(),
video: video.file video: format!("{}/{}", VIDEO_BASE_URI, video.file)
}) })
}, },
None => Template::render("index", context! { None => Template::render("video", context! {
title: "Not found" title: "Not found"
}) })
} }
@ -26,8 +25,8 @@ pub fn index(video_id: String) -> Template {
#[get("/<path>")] #[get("/<path>")]
pub async fn file(path: PathBuf) -> Option<NamedFile> { pub async fn file(path: PathBuf) -> Option<NamedFile> {
if path.is_file() { let disk = PathBuf::from(FILES_VIDEO).join(path);
return NamedFile::open(path).await.ok();
} println!("disk path: {disk:?}");
None NamedFile::open(disk).await.ok()
} }

View File

@ -7,7 +7,7 @@
<h1 class="title">{{ title }}</h1> <h1 class="title">{{ title }}</h1>
{% if video %} {% if video %}
<video controls> <video controls>
<source src="/video/{{ video }}" type="{{ kind }}"> <source src="{{ video }}" type="video/{{ kind }}">
</video> </video>
{% else %} {% else %}
<p>Nothing to see here</p> <p>Nothing to see here</p>