Add repository commands
Signed-off-by: trivernis <trivernis@protonmail.com>pull/4/head
parent
6912ad6f05
commit
d7a9a1450f
@ -0,0 +1,82 @@
|
|||||||
|
use crate::client_api::ApiClient;
|
||||||
|
use crate::tauri_plugin::commands::{ApiAccess, AppAccess};
|
||||||
|
use crate::tauri_plugin::error::{PluginError, PluginResult};
|
||||||
|
use crate::tauri_plugin::settings::{save_settings, Repository};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use tokio::fs;
|
||||||
|
|
||||||
|
static REPO_CONFIG_FILE: &str = "repo.toml";
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
|
pub struct RepoConfig {
|
||||||
|
pub listen_address: String,
|
||||||
|
pub database_path: String,
|
||||||
|
pub default_file_store: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn get_repositories(app_state: AppAccess<'_>) -> PluginResult<Vec<Repository>> {
|
||||||
|
let settings = app_state.settings.read().await;
|
||||||
|
|
||||||
|
Ok(settings.repositories.values().cloned().collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn get_active_repository(app_state: AppAccess<'_>) -> PluginResult<Option<Repository>> {
|
||||||
|
let repo = app_state.active_repo.read().await;
|
||||||
|
Ok(repo.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn add_repository(
|
||||||
|
name: String,
|
||||||
|
path: String,
|
||||||
|
app_state: AppAccess<'_>,
|
||||||
|
) -> PluginResult<Vec<Repository>> {
|
||||||
|
let repo_path = path.clone();
|
||||||
|
let path = PathBuf::from(path);
|
||||||
|
let RepoConfig { listen_address, .. } = read_repo_config(path.join(REPO_CONFIG_FILE)).await?;
|
||||||
|
|
||||||
|
let repo = Repository {
|
||||||
|
name,
|
||||||
|
path: Some(repo_path),
|
||||||
|
address: listen_address,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut repositories = Vec::new();
|
||||||
|
{
|
||||||
|
let mut settings = app_state.settings.write().await;
|
||||||
|
settings.repositories.insert(repo.name.clone(), repo);
|
||||||
|
save_settings(&settings)?;
|
||||||
|
repositories.append(&mut settings.repositories.values().cloned().collect());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(repositories)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn select_repository(
|
||||||
|
name: String,
|
||||||
|
app_state: AppAccess<'_>,
|
||||||
|
api_state: ApiAccess<'_>,
|
||||||
|
) -> PluginResult<()> {
|
||||||
|
let settings = app_state.settings.read().await;
|
||||||
|
let repo = settings.repositories.get(&name).ok_or(PluginError::from(
|
||||||
|
format!("Repository '{}' not found", name).as_str(),
|
||||||
|
))?;
|
||||||
|
let client = ApiClient::connect(&repo.address).await?;
|
||||||
|
api_state.set_api(client).await;
|
||||||
|
|
||||||
|
let mut active_repo = app_state.active_repo.write().await;
|
||||||
|
*active_repo = Some(repo.clone());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_repo_config(path: PathBuf) -> PluginResult<RepoConfig> {
|
||||||
|
let toml_str = fs::read_to_string(path).await?;
|
||||||
|
let config = toml::from_str(&toml_str)?;
|
||||||
|
|
||||||
|
Ok(config)
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
use crate::tauri_plugin::commands::ApiAccess;
|
||||||
|
use crate::tauri_plugin::error::PluginResult;
|
||||||
|
use crate::types::tags::TagResponse;
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn get_tags_for_file(
|
||||||
|
hash: String,
|
||||||
|
api_state: ApiAccess<'_>,
|
||||||
|
) -> PluginResult<Vec<TagResponse>> {
|
||||||
|
let api = api_state.api().await?;
|
||||||
|
let tags = api.tag.get_tags_for_file(hash).await?;
|
||||||
|
|
||||||
|
Ok(tags)
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
use crate::tauri_plugin::error::PluginResult;
|
||||||
|
use directories::ProjectDirs;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
static SETTINGS_FILE: &str = "settings.toml";
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
pub struct Repository {
|
||||||
|
pub(crate) name: String,
|
||||||
|
pub(crate) path: Option<String>,
|
||||||
|
pub(crate) address: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Serialize, Deserialize)]
|
||||||
|
pub struct Settings {
|
||||||
|
pub repositories: HashMap<String, Repository>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_settings_path() -> PathBuf {
|
||||||
|
let dirs = ProjectDirs::from("com", "trivernis", "mediarepo").unwrap();
|
||||||
|
let config_path = dirs.config_dir().to_path_buf();
|
||||||
|
|
||||||
|
config_path.join(SETTINGS_FILE)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Writes the settings to the file
|
||||||
|
pub fn save_settings(settings: &Settings) -> PluginResult<()> {
|
||||||
|
let settings_path = get_settings_path();
|
||||||
|
let settings_string = toml::to_string(&settings)?;
|
||||||
|
fs::write(&settings_path, &settings_string.into_bytes())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Loads the settings from the file
|
||||||
|
pub fn load_settings() -> PluginResult<Settings> {
|
||||||
|
let dirs = ProjectDirs::from("com", "trivernis", "mediarepo").unwrap();
|
||||||
|
let config_path = dirs.config_dir().to_path_buf();
|
||||||
|
if !config_path.exists() {
|
||||||
|
fs::create_dir_all(&config_path)?;
|
||||||
|
}
|
||||||
|
let settings_path = config_path.join(SETTINGS_FILE);
|
||||||
|
if !settings_path.exists() {
|
||||||
|
let settings = Settings::default();
|
||||||
|
save_settings(&settings)?;
|
||||||
|
}
|
||||||
|
let config_str = fs::read_to_string(settings_path)?;
|
||||||
|
let settings = toml::from_str(&config_str)?;
|
||||||
|
|
||||||
|
Ok(settings)
|
||||||
|
}
|
Loading…
Reference in New Issue