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::Serialize;
use crate::CONFIG;
use crate::DB_FILE;
#[derive(Serialize, Deserialize)]
pub struct Row {
@ -15,15 +15,15 @@ pub struct 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
// 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");
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()

View File

@ -5,15 +5,10 @@ use rocket::fs::FileServer;
mod video;
mod db;
pub struct BackendConfig {
pub root: &'static str,
pub db_file: &'static str,
}
const FILES_VIDEO: &'static str = "/opt/clippable/file/video";
const DB_FILE: &'static str = "/opt/clippable/db.json";
const VIDEO_BASE_URI: &'static str = "/file/video";
const CONFIG: BackendConfig = BackendConfig {
root: "/opt/clippable",
db_file: "/opt/clippable/db.json"
};
#[rocket::main]
async fn main() {
@ -21,7 +16,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;
}

View File

@ -1,23 +1,22 @@
use crate::db;
use crate::{FILES_VIDEO, 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("/<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
// 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"
})
}
@ -26,8 +25,8 @@ pub fn index(video_id: String) -> Template {
#[get("/<path>")]
pub async fn file(path: PathBuf) -> Option<NamedFile> {
if path.is_file() {
return NamedFile::open(path).await.ok();
}
None
let disk = PathBuf::from(FILES_VIDEO).join(path);
println!("disk path: {disk:?}");
NamedFile::open(disk).await.ok()
}

View File

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