feat(cache): add atomic file write (temp + rename)

Swadhin Biswas committed 7 months ago
acb6611
1 parent f450829
commit acb6611
1 file changed
+50 -0
Showing 1 changed file+50-0
▼src/image_cache.rs
+50-0
148
148
149
149
/// Get GNOME wallpaper from dconf
150
150
fn get_gnome_wallpaper() -> Option<PathBuf> {
151
+ let output = Command::new("dconf")
152
+ .args(&["read", "/org/gnome/desktop/background/picture-uri-dark"])
153
+ .output()
154
+ .ok()?;
155
+
156
+ let path_str = String::from_utf8(output.stdout).ok()?;
157
+ let path_str = path_str
158
+ .trim()
159
+ .trim_matches('\'')
160
+ .trim_start_matches("file://");
161
+
162
+ let path = PathBuf::from(path_str);
163
+ if path.exists() && is_image_file(&path) {
164
+ return Some(path);
165
+ }
166
+
167
+ None
168
+}
169
+
170
+/// Get KDE wallpaper from config
171
+fn get_kde_wallpaper() -> Option<PathBuf> {
172
+ let kde_config = dirs::config_dir()
173
+ .map(|d| d.join("plasmarc"))?;
174
+
175
+ if let Ok(content) = fs::read_to_string(&kde_config) {
176
+ for line in content.lines() {
177
+ if line.contains("Image=") || line.contains("File=") {
178
+ if let Some(value) = line.split('=').nth(1) {
179
+ let path = expand_path(value.trim());
180
+ if path.exists() && is_image_file(&path) {
181
+ return Some(path);
182
+ }
183
+ }
184
+ }
185
+ }
186
+ }
187
+
188
+ None
189
+}
190
+
191
+/// Copy wallpaper image to cache directory
192
+fn copy_image_to_cache(source: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
193
+ let cache = cache_dir();
194
+ fs::create_dir_all(&cache)?;
195
+
196
+ let cached = cached_image_path();
197
+ fs::copy(source, &cached)?;
198
+
199
+ Ok(())
200
+}
151
201