Skip to main content

shared/settings/
server.rs

1use super::{
2    ExtensionSettings, SettingsDeserializeExt, SettingsDeserializer, SettingsSerializeExt,
3    SettingsSerializer,
4};
5use crate::models::server::TrashSettings;
6use compact_str::ToCompactString;
7use serde::{Deserialize, Serialize};
8use utoipa::ToSchema;
9
10#[derive(Clone, ToSchema, Serialize, Deserialize)]
11pub struct AppSettingsServer {
12    pub max_file_manager_view_size: u64,
13    pub max_file_manager_content_search_size: u64,
14    pub max_file_manager_search_results: u64,
15    pub max_subuser_count: u64,
16    pub max_schedule_step_count: u64,
17    pub max_backup_group_count: u64,
18    pub max_firewall_rule_count: u64,
19    pub max_firewall_rule_source_count: u64,
20    pub max_tunnel_connection_count: u64,
21    pub max_tunnel_port_count: u64,
22    pub max_database_instance_database_count: u64,
23    pub max_database_instance_user_count: u64,
24
25    pub allow_overwriting_custom_docker_image: bool,
26    pub allow_viewing_installation_logs: bool,
27    pub allow_acknowledging_installation_failure: bool,
28    pub allow_viewing_transfer_progress: bool,
29
30    pub trash: TrashSettings,
31    pub allow_changing_trash_settings: bool,
32
33    pub container_prelude: compact_str::CompactString,
34}
35
36#[async_trait::async_trait]
37impl SettingsSerializeExt for AppSettingsServer {
38    async fn serialize(
39        &self,
40        serializer: SettingsSerializer,
41    ) -> Result<SettingsSerializer, anyhow::Error> {
42        Ok(serializer
43            .write_raw_setting(
44                "max_file_manager_view_size",
45                self.max_file_manager_view_size.to_compact_string(),
46            )
47            .write_raw_setting(
48                "max_file_manager_content_search_size",
49                self.max_file_manager_content_search_size
50                    .to_compact_string(),
51            )
52            .write_raw_setting(
53                "max_file_manager_search_results",
54                self.max_file_manager_search_results.to_compact_string(),
55            )
56            .write_raw_setting(
57                "max_subuser_count",
58                self.max_subuser_count.to_compact_string(),
59            )
60            .write_raw_setting(
61                "max_schedule_step_count",
62                self.max_schedule_step_count.to_compact_string(),
63            )
64            .write_raw_setting(
65                "max_backup_group_count",
66                self.max_backup_group_count.to_compact_string(),
67            )
68            .write_raw_setting(
69                "max_firewall_rule_count",
70                self.max_firewall_rule_count.to_compact_string(),
71            )
72            .write_raw_setting(
73                "max_firewall_rule_source_count",
74                self.max_firewall_rule_source_count.to_compact_string(),
75            )
76            .write_raw_setting(
77                "max_tunnel_connection_count",
78                self.max_tunnel_connection_count.to_compact_string(),
79            )
80            .write_raw_setting(
81                "max_tunnel_port_count",
82                self.max_tunnel_port_count.to_compact_string(),
83            )
84            .write_raw_setting(
85                "max_database_instance_database_count",
86                self.max_database_instance_database_count
87                    .to_compact_string(),
88            )
89            .write_raw_setting(
90                "max_database_instance_user_count",
91                self.max_database_instance_user_count.to_compact_string(),
92            )
93            .write_raw_setting(
94                "allow_overwriting_custom_docker_image",
95                self.allow_overwriting_custom_docker_image
96                    .to_compact_string(),
97            )
98            .write_raw_setting(
99                "allow_viewing_installation_logs",
100                self.allow_viewing_installation_logs.to_compact_string(),
101            )
102            .write_raw_setting(
103                "allow_acknowledging_installation_failure",
104                self.allow_acknowledging_installation_failure
105                    .to_compact_string(),
106            )
107            .write_raw_setting(
108                "allow_viewing_transfer_progress",
109                self.allow_viewing_transfer_progress.to_compact_string(),
110            )
111            .write_raw_setting("trash_retention", self.trash.retention.to_compact_string())
112            .write_raw_setting(
113                "trash_usage_threshold",
114                self.trash.usage_threshold.to_compact_string(),
115            )
116            .write_raw_setting(
117                "allow_changing_trash_settings",
118                self.allow_changing_trash_settings.to_compact_string(),
119            )
120            .write_raw_setting("container_prelude", &*self.container_prelude))
121    }
122}
123
124pub struct AppSettingsServerDeserializer;
125
126#[async_trait::async_trait]
127impl SettingsDeserializeExt for AppSettingsServerDeserializer {
128    async fn deserialize_boxed(
129        &self,
130        mut deserializer: SettingsDeserializer<'_>,
131    ) -> Result<ExtensionSettings, anyhow::Error> {
132        Ok(Box::new(AppSettingsServer {
133            max_file_manager_view_size: deserializer
134                .take_raw_setting("max_file_manager_view_size")
135                .and_then(|s| s.parse().ok())
136                .unwrap_or(10 * 1024 * 1024),
137            max_file_manager_content_search_size: deserializer
138                .take_raw_setting("max_file_manager_content_search_size")
139                .and_then(|s| s.parse().ok())
140                .unwrap_or(5 * 1024 * 1024),
141            max_file_manager_search_results: deserializer
142                .take_raw_setting("max_file_manager_search_results")
143                .and_then(|s| s.parse().ok())
144                .unwrap_or(100),
145            max_subuser_count: deserializer
146                .take_raw_setting("max_subuser_count")
147                .and_then(|s| s.parse().ok())
148                .unwrap_or(25),
149            max_schedule_step_count: deserializer
150                .take_raw_setting("max_schedule_step_count")
151                .and_then(|s| s.parse().ok())
152                .unwrap_or(50),
153            max_backup_group_count: deserializer
154                .take_raw_setting("max_backup_group_count")
155                .and_then(|s| s.parse().ok())
156                .unwrap_or(10),
157            max_firewall_rule_count: deserializer
158                .take_raw_setting("max_firewall_rule_count")
159                .and_then(|s| s.parse().ok())
160                .unwrap_or(100),
161            max_firewall_rule_source_count: deserializer
162                .take_raw_setting("max_firewall_rule_source_count")
163                .and_then(|s| s.parse().ok())
164                .unwrap_or(50),
165            max_tunnel_connection_count: deserializer
166                .take_raw_setting("max_tunnel_connection_count")
167                .and_then(|s| s.parse().ok())
168                .unwrap_or(25),
169            max_tunnel_port_count: deserializer
170                .take_raw_setting("max_tunnel_port_count")
171                .and_then(|s| s.parse().ok())
172                .unwrap_or(10),
173            max_database_instance_database_count: deserializer
174                .take_raw_setting("max_database_instance_database_count")
175                .and_then(|s| s.parse().ok())
176                .unwrap_or(10),
177            max_database_instance_user_count: deserializer
178                .take_raw_setting("max_database_instance_user_count")
179                .and_then(|s| s.parse().ok())
180                .unwrap_or(10),
181            allow_overwriting_custom_docker_image: deserializer
182                .take_raw_setting("allow_overwriting_custom_docker_image")
183                .map(|s| s == "true")
184                .unwrap_or(true),
185            allow_viewing_installation_logs: deserializer
186                .take_raw_setting("allow_viewing_installation_logs")
187                .map(|s| s == "true")
188                .unwrap_or(true),
189            allow_acknowledging_installation_failure: deserializer
190                .take_raw_setting("allow_acknowledging_installation_failure")
191                .map(|s| s == "true")
192                .unwrap_or(true),
193            allow_viewing_transfer_progress: deserializer
194                .take_raw_setting("allow_viewing_transfer_progress")
195                .map(|s| s == "true")
196                .unwrap_or(true),
197            trash: TrashSettings {
198                retention: deserializer
199                    .take_raw_setting("trash_retention")
200                    .and_then(|s| s.parse().ok())
201                    .unwrap_or(7 * 24 * 60 * 60),
202                usage_threshold: deserializer
203                    .take_raw_setting("trash_usage_threshold")
204                    .and_then(|s| s.parse().ok())
205                    .unwrap_or(90),
206            },
207            allow_changing_trash_settings: deserializer
208                .take_raw_setting("allow_changing_trash_settings")
209                .map(|s| s == "true")
210                .unwrap_or(true),
211            container_prelude: deserializer
212                .take_raw_setting("container_prelude")
213                .unwrap_or_else(|| "container@calagopus~".into()),
214        }))
215    }
216}