config.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use serde::Deserialize;
  2. use std::{collections::HashSet, path::Path};
  3. use tokio::fs;
  4. #[derive(Debug, Deserialize)]
  5. pub struct Config {
  6. pub storage: StorageConfig,
  7. pub download_rules: DownloadRules,
  8. }
  9. #[derive(Debug, Deserialize, PartialEq)]
  10. pub enum StorageBackend {
  11. #[serde(rename = "local")]
  12. Local,
  13. #[serde(rename = "nginx")]
  14. Nginx,
  15. }
  16. #[derive(Debug, Deserialize)]
  17. pub struct StorageConfig {
  18. pub backend: StorageBackend,
  19. pub local: Option<LocalStorageConfig>,
  20. pub nginx: Option<NginxStorageConfig>,
  21. }
  22. #[derive(Debug, Deserialize)]
  23. pub struct NginxStorageConfig {
  24. pub base_url: String,
  25. pub public_url: String, // 用于对外返回的url
  26. }
  27. #[derive(Debug, Deserialize)]
  28. pub struct LocalStorageConfig {
  29. pub root_path: String,
  30. }
  31. #[derive(Debug, Deserialize)]
  32. pub struct DownloadRules {
  33. pub extensions: HashSet<String>,
  34. }
  35. pub async fn load_config(path: &str) -> anyhow::Result<Config> {
  36. let config_str = fs::read_to_string(path).await?;
  37. let config: Config = toml::from_str(&config_str)?;
  38. Ok(config)
  39. }
  40. pub fn has_matching_extension(path: &str, extensions: &HashSet<String>) -> bool {
  41. let path = Path::new(path);
  42. if let Some(ext) = path.extension() {
  43. let ext_str = ext.to_string_lossy().to_string();
  44. log::debug!(
  45. "Checking extension for {:?} against {:?}, extension: {:?}",
  46. path,
  47. extensions,
  48. ext_str
  49. );
  50. let matches = extensions.contains(&ext_str);
  51. if matches {
  52. log::debug!("Extension match found for {:?}", path);
  53. }
  54. matches
  55. } else {
  56. false
  57. }
  58. }