Skip to content

Commit 7536a81

Browse files
Fix Kobo sync downloading incorrect books due to stale temp files
The Kobo sync process was occasionally serving the wrong book content because of unsafe file handling in the temporary directory used for metadata embedding. - Fixed `do_calibre_export` in `cps/embed_helper.py` to strictly match the requested filename (UUID) instead of returning the first file found with the correct extension. - Added cleanup logic to `_convert_kepubify` in `cps/tasks/convert.py` to ensure temporary source files are deleted after conversion, preventing the accumulationFix Kobo sync downloading incorrect books due to stale temp files The Kobo sync process was occasionally serving the wrong book content because of unsafe file handling in the temporary directory used for metadata embedding. - Fixed `do_calibre_export` in `cps/embed_helper.py` to strictly match the requested filename (UUID) instead of returning the first file found with the correct extension. - Added cleanup logic to `_convert_kepubify` in `cps/tasks/convert.py` to ensure temporary source files are deleted after conversion, preventing the accumulation of stale files. This resolves the issue where multiple different books would download as the same book content on Kobo devices.
1 parent 2f68a11 commit 7536a81

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

cps/embed_helper.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ def do_calibre_export(book_id, book_format):
5151
log.warning(f'No {book_format} file found in export directory: {export_dir}')
5252
else:
5353
# No subdirectory - look for files directly in tmp_dir
54+
# STRICT CHECK: Only look for the file we requested
55+
expected_filename = temp_file_name + '.' + book_format.lower()
5456
for filename in os.listdir(tmp_dir):
55-
if filename.lower().endswith('.' + book_format.lower()):
57+
if filename.lower() == expected_filename.lower():
5658
actual_filename = os.path.splitext(filename)[0]
5759
return tmp_dir, actual_filename
5860

59-
log.warning(f'No {book_format} file found in {tmp_dir}')
61+
log.warning(f'No file named {expected_filename} found in {tmp_dir}')
6062

6163
# Fallback to original behavior
6264
return tmp_dir, temp_file_name

cps/tasks/convert.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,19 @@ def _convert_kepubify(self, file_path, format_old_ext, format_new_ext):
236236
copyfile(converted_file[0], (file_path + format_new_ext))
237237
os.unlink(converted_file[0])
238238
else:
239+
if config.config_embed_metadata and config.config_binariesdir and os.path.isfile(filename):
240+
try:
241+
os.remove(filename)
242+
except OSError:
243+
pass
239244
return 1, N_("Converted file not found or more than one file in folder %(folder)s",
240245
folder=os.path.dirname(file_path))
246+
247+
if config.config_embed_metadata and config.config_binariesdir and os.path.isfile(filename):
248+
try:
249+
os.remove(filename)
250+
except OSError:
251+
pass
241252
return check, None
242253

243254
def _convert_calibre(self, file_path, format_old_ext, format_new_ext, has_cover):

0 commit comments

Comments
 (0)