[116] Exporte atomar (Tauri save_export), Electron speichert Projekte atomar im Hauptprozess, Wiederherstellung aus .obp~ (Öffnen-Angebot + Datei-Menü)

This commit is contained in:
Claude committed 2026-11-14 11:34:19 +00:00
1 parent 7cc642241d
commit 47dfedd829
22 files changed
+755 -29

No files matched your search

+93 -1
View File
@@ -15,6 +15,44 @@ fn with_suffix(path: &Path, suffix: &str) -> PathBuf {
/// Schreibt `content` atomar nach `path`; die Vorversion landet in `<path>~`.
pub fn save_atomic_impl(path: &Path, content: &[u8]) -> Result<(), String> {
write_atomic(path, content, true)
}
/// Export ([116]): atomar wie `save_atomic_impl`, aber ohne `<path>~` (ein Export ersetzt eine Datei bewusst).
pub fn export_atomic_impl(path: &Path, content: &[u8]) -> Result<(), String> {
write_atomic(path, content, false)
}
/// Gibt es eine Sicherung der Vorversion (`<path>~`)?
pub fn backup_exists_impl(path: &Path) -> bool {
with_suffix(path, "~").is_file()
}
/// Liest die Sicherung der Vorversion (`<path>~`); `None`, wenn es keine gibt oder sie nicht als UTF-8 lesbar ist.
pub fn read_backup_impl(path: &Path) -> Option<String> {
fs::read_to_string(with_suffix(path, "~")).ok()
}
/// Dekodiert `%XX` (UTF-8) aus einem Header-Wert; ungültige Folgen bleiben wie sie sind.
pub fn percent_decode(s: &str) -> String {
let b = s.as_bytes();
let mut out = Vec::with_capacity(b.len());
let mut i = 0;
while i < b.len() {
if b[i] == b'%' && i + 2 < b.len() + 0 && i + 2 <= b.len() - 1 {
if let Ok(v) = u8::from_str_radix(&s[i + 1..i + 3], 16) {
out.push(v);
i += 3;
continue;
}
}
out.push(b[i]);
i += 1;
}
String::from_utf8_lossy(&out).into_owned()
}
fn write_atomic(path: &Path, content: &[u8], keep_backup: bool) -> Result<(), String> {
let tmp = with_suffix(path, ".tmp");
let backup = with_suffix(path, "~");
let write_tmp = || -> std::io::Result<()> {
@@ -27,7 +65,7 @@ pub fn save_atomic_impl(path: &Path, content: &[u8]) -> Result<(), String> {
return Err(format!("Temp-Datei schreiben: {e}"));
}
// Vorversion sichern (Kopie statt Umbenennen: der Zielpfad existiert so zu jedem Zeitpunkt).
if path.exists() {
if keep_backup && path.exists() {
if let Err(e) = fs::copy(path, &backup) {
let _ = fs::remove_file(&tmp);
return Err(format!("Sicherung anlegen: {e}"));
@@ -57,6 +95,60 @@ mod tests {
d
}
#[test]
fn export_ueberschreibt_atomar_ohne_sicherung() {
let d = tmpdir("export");
let p = d.join("plan.pdf");
export_atomic_impl(&p, b"%PDF-1").unwrap();
export_atomic_impl(&p, &[0u8, 255, 1, 2]).unwrap();
assert_eq!(fs::read(&p).unwrap(), vec![0u8, 255, 1, 2]);
assert!(!with_suffix(&p, "~").exists());
assert!(!with_suffix(&p, ".tmp").exists());
fs::remove_dir_all(&d).unwrap();
}
#[test]
fn export_fehler_laesst_ziel_heil() {
let d = tmpdir("export-fehler");
let p = d.join("plan.dxf");
export_atomic_impl(&p, b"alt").unwrap();
fs::create_dir(with_suffix(&p, ".tmp")).unwrap();
assert!(export_atomic_impl(&p, b"neu").is_err());
assert_eq!(fs::read(&p).unwrap(), b"alt");
assert!(export_atomic_impl(&d.join("nope").join("x.dxf"), b"x").is_err());
fs::remove_dir_all(&d).unwrap();
}
#[test]
fn sicherung_wird_erkannt() {
let d = tmpdir("exists");
let p = d.join("a.obp");
save_atomic_impl(&p, b"eins").unwrap();
assert!(!backup_exists_impl(&p));
save_atomic_impl(&p, b"zwei").unwrap();
assert!(backup_exists_impl(&p));
fs::remove_dir_all(&d).unwrap();
}
#[test]
fn sicherung_wird_gelesen() {
let d = tmpdir("read-backup");
let p = d.join("a.obp");
assert_eq!(read_backup_impl(&p), None);
save_atomic_impl(&p, "eins äö".as_bytes()).unwrap();
save_atomic_impl(&p, b"zwei").unwrap();
assert_eq!(read_backup_impl(&p).as_deref(), Some("eins äö"));
fs::remove_dir_all(&d).unwrap();
}
#[test]
fn prozent_dekodierung_von_pfaden() {
assert_eq!(percent_decode("/tmp/Pl%C3%A4ne/a%20b.pdf"), "/tmp/Pläne/a b.pdf");
assert_eq!(percent_decode("/x/100%"), "/x/100%");
assert_eq!(percent_decode("/x/%zz"), "/x/%zz");
assert_eq!(percent_decode("C:%5Cx"), "C:\\x");
}
#[test]
fn neue_datei_ohne_sicherung() {
let d = tmpdir("neu");
+35
View File
@@ -43,6 +43,35 @@ fn save_atomic(path: String, content: String) -> Result<(), String> {
atomic_save::save_atomic_impl(&PathBuf::from(path), content.as_bytes())
}
/// Export schreiben ([116], PDF/DXF/IFC/PNG …): Bytes als Rohkörper, Zielpfad prozent-kodiert im Header `x-path`;
/// atomar (Temp + fsync + Umbenennen), das Ziel bleibt bei Fehler heil. Keine `~`-Sicherung.
#[tauri::command]
fn save_export(request: tauri::ipc::Request<'_>) -> Result<(), String> {
let path = request
.headers()
.get("x-path")
.and_then(|v| v.to_str().ok())
.map(atomic_save::percent_decode)
.filter(|p| !p.is_empty())
.ok_or_else(|| "Zielpfad fehlt".to_string())?;
let tauri::ipc::InvokeBody::Raw(bytes) = request.body() else {
return Err("Inhalt fehlt".to_string());
};
atomic_save::export_atomic_impl(&PathBuf::from(path), bytes)
}
/// Gibt es zur Projektdatei eine Sicherung der Vorversion (`<pfad>~`)? ([116])
#[tauri::command]
fn backup_exists(path: String) -> bool {
atomic_save::backup_exists_impl(&PathBuf::from(path))
}
/// Inhalt der Sicherung der Vorversion (`<pfad>~`), falls vorhanden ([116]).
#[tauri::command]
fn read_backup(path: String) -> Option<String> {
atomic_save::read_backup_impl(&PathBuf::from(path))
}
/// Blender-Programmdatei ([211]): eigene Angabe aus den Einstellungen, sonst PATH und die üblichen Orte. `None` = nicht gefunden.
#[tauri::command]
fn blender_find(custom: Option<String>) -> Option<String> {
@@ -414,6 +443,9 @@ pub fn run() {
acquire_project_lock,
release_project_lock,
save_atomic,
save_export,
backup_exists,
read_backup,
blender_find,
blender_job_dir,
blender_start,
@@ -438,6 +470,9 @@ pub fn run() {
acquire_project_lock,
release_project_lock,
save_atomic,
save_export,
backup_exists,
read_backup,
blender_find,
blender_job_dir,
blender_start,