From d56bd6b7cde7786757f736d9f460a099ad0b5fa9 Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 22 Mar 2022 22:09:43 -0700 Subject: [PATCH] + Generally useful helper functions --- api/src/admin/response.rs | 33 ++++++++++++++++++++++++++++++++- api/src/admin/util.rs | 14 ++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 api/src/admin/util.rs diff --git a/api/src/admin/response.rs b/api/src/admin/response.rs index b207fed..51e53b2 100644 --- a/api/src/admin/response.rs +++ b/api/src/admin/response.rs @@ -1,4 +1,35 @@ +/* + * Module handles general responses for the admin feature + * Primarily these are responses for Admin related actions + * like fetching video's, updating videos and deleting them + * as well + */ use serde::Serialize; +use rocket::serde::json::Json; + +const FAIL: &'static str = "fail"; +const OK: &'static str = "fail"; + #[derive(Serialize)] -pub struct ActionResponse(pub &'static str); +pub struct ActionResponse { + status: &'static str, + code: i32, + details: Option<&'static str> +} + +pub fn ok() -> Json { + Json(ActionResponse { + status: OK, + code: 200, + details: None + }) +} + +pub fn bad_request(text: Option<&'static str>) -> Json { + Json(ActionResponse { + status: FAIL, + code: 400, + details: text + }) +} \ No newline at end of file diff --git a/api/src/admin/util.rs b/api/src/admin/util.rs new file mode 100644 index 0000000..eb9e305 --- /dev/null +++ b/api/src/admin/util.rs @@ -0,0 +1,14 @@ +use std::path::PathBuf; + +pub fn valid_filename(p: &PathBuf) -> bool { + // Checks if a given filename is actually valid or not + let mut valid = false; + let s = p.file_name().unwrap_or_default().to_string_lossy(); + for e in [".mp4", ".webm", ".mkv"] { + if s.ends_with(e) { + valid = true; + break; + } + } + valid +} \ No newline at end of file