From 5710703c5057048e52006bb26783aa636e6c96d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Sun, 2 Jun 2019 00:08:52 +0200 Subject: [PATCH] Make sure the backup option only appears when using sqlite --- src/api/admin.rs | 15 +++++++++++++-- src/static/templates/admin/page.hbs | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index 165387bf..a8b30f90 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -1,4 +1,5 @@ use serde_json::Value; +use std::process::Command; use rocket::http::{Cookie, Cookies, SameSite}; use rocket::request::{self, FlashMessage, Form, FromRequest, Request}; @@ -9,7 +10,7 @@ use rocket_contrib::json::Json; use crate::api::{ApiResult, EmptyResult, JsonResult}; use crate::auth::{decode_admin, encode_jwt, generate_admin_claims, ClientIp}; use crate::config::ConfigBuilder; -use crate::db::{models::*, DbConn, backup_database}; +use crate::db::{backup_database, models::*, DbConn}; use crate::error::Error; use crate::mail; use crate::CONFIG; @@ -34,6 +35,10 @@ pub fn routes() -> Vec { ] } +lazy_static! { + static ref CAN_BACKUP: bool = cfg!(feature = "sqlite") && Command::new("sqlite").arg("-version").status().is_ok(); +} + #[get("/")] fn admin_disabled() -> &'static str { "The admin panel is disabled, please configure the 'ADMIN_TOKEN' variable to enable it" @@ -102,6 +107,7 @@ struct AdminTemplateData { version: Option<&'static str>, users: Vec, config: Value, + can_backup: bool, } impl AdminTemplateData { @@ -111,6 +117,7 @@ impl AdminTemplateData { version: VERSION, users, config: CONFIG.prepare_json(), + can_backup: *CAN_BACKUP, } } @@ -207,7 +214,11 @@ fn delete_config(_token: AdminToken) -> EmptyResult { #[post("/config/backup_db")] fn backup_db(_token: AdminToken) -> EmptyResult { - backup_database() + if *CAN_BACKUP { + backup_database() + } else { + err!("Can't back up current DB (either it's not SQLite or the 'sqlite' binary is not present)"); + } } pub struct AdminToken {} diff --git a/src/static/templates/admin/page.hbs b/src/static/templates/admin/page.hbs index 1cab7634..24e16d1a 100644 --- a/src/static/templates/admin/page.hbs +++ b/src/static/templates/admin/page.hbs @@ -157,9 +157,11 @@ + + {{#if can_backup}}
+ data-target="#g_database">Backup Database
NOTE: A local installation of sqlite3 is required for this section to work. @@ -167,6 +169,7 @@
+ {{/if}}