Backend now responsed to all correct API calls for video file pages

This commit is contained in:
shockrah 2025-08-15 20:45:04 -07:00
parent 150d34c38e
commit 1309ab2324
4 changed files with 18 additions and 18 deletions

View File

@ -15,7 +15,7 @@ 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)
@ -23,7 +23,7 @@ pub fn get_video(id: String) -> Option<Row> {
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

@ -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;
}

View File

@ -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("/<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"
})
}
@ -25,9 +24,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
pub async fn file(path: PathBuf) -> String {
let path = path.to_str().unwrap_or("");
println!("{}", path);
format!("{}{VIDEO_BASE_URI}/{path}", CONFIG.root)
}

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>