Compare commits

..

No commits in common. "eb99d97e7b58eda668915c6334a8065fba3c12f9" and "150d34c38e1b8244a2a23e254d4d4d2f41308438" have entirely different histories.

4 changed files with 27 additions and 21 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::DB_FILE; use crate::CONFIG;
#[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: &str) -> Option<Row> { pub fn get_video(id: String) -> 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(DB_FILE) let db_file = fs::read_to_string(CONFIG.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,10 +5,15 @@ use rocket::fs::FileServer;
mod video; mod video;
mod db; mod db;
const FILES_VIDEO: &'static str = "/opt/clippable/file/video"; pub struct BackendConfig {
const DB_FILE: &'static str = "/opt/clippable/db.json"; pub root: &'static str,
const VIDEO_BASE_URI: &'static str = "/file/video"; pub db_file: &'static str,
}
const CONFIG: BackendConfig = BackendConfig {
root: "/opt/clippable",
db_file: "/opt/clippable/db.json"
};
#[rocket::main] #[rocket::main]
async fn main() { async fn main() {
@ -16,7 +21,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_BASE_URI, routes![video::file]) .mount("/video", routes![video::file])
.attach(Template::fairing()) .attach(Template::fairing())
.launch().await; .launch().await;
} }

View File

@ -1,22 +1,23 @@
use crate::{FILES_VIDEO, VIDEO_BASE_URI, db}; use crate::db;
use std::path::PathBuf; use std::path::PathBuf;
use rocket::fs::NamedFile; use rocket::fs::NamedFile;
use rocket_dyn_templates::{Template, context}; use rocket_dyn_templates::Template;
use rocket_dyn_templates::context;
#[get("/<video_id>")] #[get("/<video_id>")]
pub fn index(video_id: &str) -> Template { pub fn index(video_id: String) -> 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("video", context! { Template::render("index", context! {
title: path.to_str().unwrap(), title: path.file_name(),
kind: path.extension().expect("No extension found").to_str().unwrap(), kind: path.extension(),
video: format!("{}/{}", VIDEO_BASE_URI, video.file) video: video.file
}) })
}, },
None => Template::render("video", context! { None => Template::render("index", context! {
title: "Not found" title: "Not found"
}) })
} }
@ -25,8 +26,8 @@ pub fn index(video_id: &str) -> Template {
#[get("/<path>")] #[get("/<path>")]
pub async fn file(path: PathBuf) -> Option<NamedFile> { pub async fn file(path: PathBuf) -> Option<NamedFile> {
let disk = PathBuf::from(FILES_VIDEO).join(path); if path.is_file() {
return NamedFile::open(path).await.ok();
println!("disk path: {disk:?}"); }
NamedFile::open(disk).await.ok() None
} }

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 }}" type="video/{{ kind }}"> <source src="/video/{{ video }}" type="{{ kind }}">
</video> </video>
{% else %} {% else %}
<p>Nothing to see here</p> <p>Nothing to see here</p>