From f5d61b107ce64e625abe2b00b9f68b1f18d69e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Reuh=20Fildadut?= Date: Thu, 9 Oct 2025 15:44:30 +0200 Subject: [PATCH] fix: error if deleting an html file during sync and it was read on the server --- README.md | 2 +- main.py | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 37edc9b..9ce9bde 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ - [x] Make HTML template configurable - [ ] Nested categories - [ ] Share the fun somewhere -- [ ] Edge cases: mark as read during sync (if marked as read on server or not) +- [x] Edge cases: mark as read during sync (if marked as read on server or not) - [ ] Proper filename escaping - [ ] Command to force regenerate all HTML files (incl. recompute datetimes & paths) - [ ] Handle item updates diff --git a/main.py b/main.py index d58307a..d28df90 100644 --- a/main.py +++ b/main.py @@ -164,11 +164,11 @@ def synchronize_with_server(config, client_session): } item_json["html_path"] = str(get_html_path(config, item_json).relative_to(config.html_root)) - p = config.json_root / f"{ sha256(item_json["id"].encode("utf-8")).hexdigest() }.json" - grabbed_item_paths.append(p) - if not p.exists(): + json_path = config.json_root / f"{ sha256(item_json["id"].encode("utf-8")).hexdigest() }.json" + grabbed_item_paths.append(json_path) + if not json_path.exists(): # write JSON - with p.open("w") as f: + with json_path.open("w") as f: json.dump(item_json, f) # write HTML generate_html_for_item(config, item_json) @@ -187,7 +187,7 @@ def synchronize_with_server(config, client_session): if not item_path in grabbed_item_paths: # remove HTML item_json = json.load(item_path.open("r")) - remove_html_for_item(config, item_json) + remove_html_for_item(config, item_json, ignore_deleted=True) # ignore if file was deleted by user during sync # remove JSON item_path.unlink() removed_items += 1 @@ -204,10 +204,11 @@ def generate_html_for_item(config, item_json): with html_path.open("w") as f: f.write(config.item_template.render(item_json)) -def remove_html_for_item(config, item_json): +def remove_html_for_item(config, item_json, ignore_deleted=False): # Delete a HTML file for a JSON object html_path = config.html_root / item_json["html_path"] - html_path.unlink() + if not ignore_deleted or html_path.exists(): + html_path.unlink() def remove_empty_html_directories(config): # Remove empty directories in the HTML directory