From 1b2204f1a687e1e34a312bfca852fe377b3b49cb Mon Sep 17 00:00:00 2001 From: Luis Garcia Date: Mon, 16 Dec 2024 12:26:49 -0700 Subject: [PATCH] Use XDG directories --- Cargo.lock | 10 ++++++++++ crates/tanoshi/Cargo.toml | 1 + crates/tanoshi/src/infrastructure/config.rs | 20 +++++++++++++++++--- org.luigi311.tanoshi.yml | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7696af80..bc76e50f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1618,6 +1618,15 @@ dependencies = [ "subtle", ] +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys", +] + [[package]] name = "dirs" version = "5.0.1" @@ -6292,6 +6301,7 @@ dependencies = [ "chrono", "clap", "compress-tools", + "directories", "dirs", "env_logger", "fancy-regex", diff --git a/crates/tanoshi/Cargo.toml b/crates/tanoshi/Cargo.toml index ceee8176..f4d37ff1 100644 --- a/crates/tanoshi/Cargo.toml +++ b/crates/tanoshi/Cargo.toml @@ -59,6 +59,7 @@ mime_guess = { version = "2", default-features = false } clap = { version = "4", features = ["derive"] } bytes = { version = "1", default-features = false } dirs = "5" +directories = "5" base64 = { version = "0.22", default-features = false } sqlx = { version = "0.8", features = [ "runtime-tokio", diff --git a/crates/tanoshi/src/infrastructure/config.rs b/crates/tanoshi/src/infrastructure/config.rs index ecaa4a52..6f1f3c8b 100644 --- a/crates/tanoshi/src/infrastructure/config.rs +++ b/crates/tanoshi/src/infrastructure/config.rs @@ -3,6 +3,7 @@ use rand::{thread_rng, Rng}; use serde::{Deserialize, Serialize}; use std::path::Path; use std::{iter, path::PathBuf}; +use directories::ProjectDirs; #[derive(Deserialize, Serialize, Debug, Clone)] pub struct TelegramConfig { @@ -109,10 +110,23 @@ impl Default for Config { } fn tanoshi_home() -> PathBuf { - match std::env::var("TANOSHI_HOME") { - Ok(path) => PathBuf::from(path), - Err(_) => dirs::home_dir().expect("should have home").join(".tanoshi"), + // If TANOSHI_HOME is explicitly set, prefer that + if let Ok(path) = std::env::var("TANOSHI_HOME") { + return PathBuf::from(path); } + + // Check for legacy directory ~/.tanoshi + let legacy_dir = dirs::home_dir() + .expect("Home directory should exist") + .join(".tanoshi"); + if legacy_dir.exists() { + return legacy_dir; + } + + // Use XDG directories + let proj_dirs = ProjectDirs::from("org", "luigi311", "tanoshi") + .expect("Could not determine project directory"); + proj_dirs.data_dir().to_path_buf() } fn default_port() -> u16 { diff --git a/org.luigi311.tanoshi.yml b/org.luigi311.tanoshi.yml index 2709018f..6b5d253b 100644 --- a/org.luigi311.tanoshi.yml +++ b/org.luigi311.tanoshi.yml @@ -12,7 +12,7 @@ finish-args: - --device=dri # OpenGL, not necessary for all projects - --share=ipc - --share=network - - --filesystem=home/.tanoshi + - --filesystem=xdg-data/tanoshi:rw cleanup: - "/include"