From 6b2ccbf975193e35760fe5e5456f428170289928 Mon Sep 17 00:00:00 2001 From: asonix Date: Fri, 24 Nov 2023 11:48:45 -0600 Subject: [PATCH] Allow configuring pict-rs' temporary directory --- defaults.toml | 1 + pict-rs.toml | 5 +++++ src/config.rs | 2 +- src/config/commandline.rs | 8 ++++++++ src/config/defaults.rs | 2 ++ src/config/file.rs | 2 ++ src/lib.rs | 2 +- src/tmp_file.rs | 4 ++-- 8 files changed, 22 insertions(+), 4 deletions(-) diff --git a/defaults.toml b/defaults.toml index 76e5c41..4e756ce 100644 --- a/defaults.toml +++ b/defaults.toml @@ -3,6 +3,7 @@ address = "0.0.0.0:8080" read_only = false danger_dummy_mode = false max_file_count = 1 +temporary_directory = "/tmp" [client] timeout = 30 diff --git a/pict-rs.toml b/pict-rs.toml index 997a278..e5a72cd 100644 --- a/pict-rs.toml +++ b/pict-rs.toml @@ -32,6 +32,11 @@ api_key = 'API_KEY' # default: 1 max_file_count = 1 +## Optional: directory pict-rs will use to create temporary files +# environment variable: PICTRS__SERVER__TEMPORARY_DIRECTORY +# default: The system's advertised temporary directory ("/tmp" on most linuxes) +temporary_directory = "/tmp" + ## Client configuration [client] ## Optional: time (in seconds) the client will wait for a response before giving up diff --git a/src/config.rs b/src/config.rs index 13e9104..2745685 100644 --- a/src/config.rs +++ b/src/config.rs @@ -54,7 +54,7 @@ impl ConfigSource { } } -// deserialized Configuration for the pict-rs application +// Instantiated configuration for the pict-rs application #[derive(Debug)] pub struct PictRsConfiguration { pub(crate) config: Configuration, diff --git a/src/config/commandline.rs b/src/config/commandline.rs index 7c1e264..852d909 100644 --- a/src/config/commandline.rs +++ b/src/config/commandline.rs @@ -52,6 +52,7 @@ impl Args { Command::Run(Run { address, api_key, + temporary_directory, client_timeout, upgrade_concurrency, metrics_prometheus_address, @@ -110,6 +111,7 @@ impl Args { read_only, danger_dummy_mode, max_file_count, + temporary_directory, }; let client = Client { @@ -515,6 +517,8 @@ struct Server { danger_dummy_mode: bool, #[serde(skip_serializing_if = "Option::is_none")] max_file_count: Option, + #[serde(skip_serializing_if = "Option::is_none")] + temporary_directory: Option, } #[derive(Debug, Default, serde::Serialize)] @@ -898,6 +902,10 @@ struct Run { #[arg(long)] api_key: Option, + /// The temporary directory pict-rs should use when processing media + #[arg(long)] + temporary_directory: Option, + /// How long (in seconds) the internel HTTP client should wait for responses /// /// This number defaults to 30 diff --git a/src/config/defaults.rs b/src/config/defaults.rs index 160dc1d..74f4a34 100644 --- a/src/config/defaults.rs +++ b/src/config/defaults.rs @@ -23,6 +23,7 @@ struct ServerDefaults { read_only: bool, danger_dummy_mode: bool, max_file_count: u32, + temporary_directory: PathBuf, } #[derive(Clone, Debug, serde::Serialize)] @@ -184,6 +185,7 @@ impl Default for ServerDefaults { read_only: false, danger_dummy_mode: false, max_file_count: 1, + temporary_directory: std::env::temp_dir(), } } } diff --git a/src/config/file.rs b/src/config/file.rs index a04e1a0..541baf4 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -116,6 +116,8 @@ pub(crate) struct Server { pub(crate) danger_dummy_mode: bool, pub(crate) max_file_count: u32, + + pub(crate) temporary_directory: PathBuf, } #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] diff --git a/src/lib.rs b/src/lib.rs index 1f9f854..8f8d6a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2091,7 +2091,7 @@ impl PictRsConfiguration { pub async fn run(self) -> color_eyre::Result<()> { let PictRsConfiguration { config, operation } = self; - let tmp_dir = TmpDir::init().await?; + let tmp_dir = TmpDir::init(&config.server.temporary_directory).await?; let client = build_client()?; diff --git a/src/tmp_file.rs b/src/tmp_file.rs index c1d208f..bd7ee7b 100644 --- a/src/tmp_file.rs +++ b/src/tmp_file.rs @@ -14,8 +14,8 @@ pub(crate) struct TmpDir { } impl TmpDir { - pub(crate) async fn init() -> std::io::Result> { - let path = std::env::temp_dir().join(Uuid::new_v4().to_string()); + pub(crate) async fn init>(path: P) -> std::io::Result> { + let path = path.as_ref().join(Uuid::new_v4().to_string()); tokio::fs::create_dir(&path).await?; Ok(Arc::new(TmpDir { path: Some(path) })) }