share atomic-write bytes helper between write_atomic and image export

write_image_file hand-rolled the same temp+rename dance write_atomic
already did for text, just for bytes. Split write_atomic into a
write_atomic_bytes core and have both callers use it.
This commit is contained in:
Horizontal42 2026-08-12 19:54:37 +03:00
parent d7b88cc9c9
commit a8121dbc90

View file

@ -76,13 +76,7 @@ fn write_image_file(app: AppHandle, data_base64: String) -> Result<bool, String>
.blocking_save_file() .blocking_save_file()
{ {
let path = p.into_path().map_err(|e| e.to_string())?; let path = p.into_path().map_err(|e| e.to_string())?;
use std::io::Write; write_atomic_bytes(&path, &base64_decode(&data_base64)?)?;
let bytes = base64_decode(&data_base64)?;
let tmp = path.with_extension("tmp");
std::fs::File::create(&tmp)
.and_then(|mut f| f.write_all(&bytes))
.map_err(|e| e.to_string())?;
fs::rename(&tmp, path).map_err(|e| e.to_string())?;
Ok(true) Ok(true)
} else { } else {
Ok(false) Ok(false)
@ -123,12 +117,16 @@ fn base64_decode(s: &str) -> Result<Vec<u8>, String> {
} }
/// write via a temp file + rename so a crash mid-write cannot corrupt the target /// write via a temp file + rename so a crash mid-write cannot corrupt the target
fn write_atomic(path: &Path, contents: &str) -> Result<(), String> { fn write_atomic_bytes(path: &Path, contents: &[u8]) -> Result<(), String> {
let tmp = path.with_extension("tmp"); let tmp = path.with_extension("tmp");
fs::write(&tmp, contents).map_err(|e| e.to_string())?; fs::write(&tmp, contents).map_err(|e| e.to_string())?;
fs::rename(&tmp, path).map_err(|e| e.to_string()) fs::rename(&tmp, path).map_err(|e| e.to_string())
} }
fn write_atomic(path: &Path, contents: &str) -> Result<(), String> {
write_atomic_bytes(path, contents.as_bytes())
}
#[tauri::command] #[tauri::command]
fn save_file(app: AppHandle, name: String, contents: String) -> Result<(), String> { fn save_file(app: AppHandle, name: String, contents: String) -> Result<(), String> {
if !safe_name(&name) { if !safe_name(&name) {