use clap::ValueEnum;
use github_proxy::{Proxy, Resource};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use strum::{Display, EnumString, IntoStaticStr};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Hash,
Default,
Display,
EnumString,
IntoStaticStr,
Serialize,
Deserialize,
ValueEnum,
)]
pub enum UiType {
#[default]
MhoUI,
Metacubexd,
Yacd,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebConfig {
pub ui: UiType,
pub host: String,
pub secret: String,
}
impl Default for WebConfig {
fn default() -> Self {
Self {
ui: UiType::default(),
host: ":9090".to_string(),
secret: String::new(),
}
}
}
impl WebConfig {
pub fn ui_name(&self) -> &'static str {
self.ui.into()
}
pub fn ui_dir(&self, config_dir: &Path) -> PathBuf {
config_dir.join(self.ui_name())
}
pub fn ui_version(&self, config_dir: &Path) -> Option<String> {
let index_html = self.ui_dir(config_dir).join("index.html");
let content = std::fs::read_to_string(index_html).ok()?;
match self.ui {
UiType::Metacubexd => {
let start_pattern = "appVersion:\"";
if let Some(start) = content.find(start_pattern) {
let start = start + start_pattern.len();
if let Some(end) = content[start..].find('"') {
return Some(content[start..start + end].to_string());
}
}
}
UiType::MhoUI => {
let start_pattern = "<meta name=\"version\" content=\"";
if let Some(start) = content.find(start_pattern) {
let start = start + start_pattern.len();
if let Some(end) = content[start..].find('"') {
return Some(content[start..start + end].to_string());
}
}
}
UiType::Yacd => {}
}
None
}
fn ui_release_file_name(&self) -> String {
use UiType::*;
match self.ui {
Yacd => "yacd.tar.gz".to_string(),
Metacubexd => "metacubexd.tar.gz".to_string(),
MhoUI => "mho-ui.tar.gz".to_string(),
}
}
pub fn ui_url(&self) -> crate::error::Result<String> {
Resource::File {
owner: "ahaoboy".to_string(),
repo: "mho-assets".to_string(),
reference: "main".to_string(),
path: self.ui_release_file_name(),
}
.url(&Proxy::Github)
.ok_or_else(|| {
crate::error::MhoError::Download("Failed to get UI download URL".to_string())
})
}
}