From a940bb92f652c8e245405f84c1e17b3994961a45 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 12 Aug 2023 07:05:53 +0530 Subject: [PATCH] CBR Input: Fix comics with extremely long filenames not working on Windows. Fixes #2031047 [Private bug](https://bugs.launchpad.net/calibre/+bug/2031047) --- src/calibre/ebooks/comic/input.py | 3 ++- src/calibre/utils/filenames.py | 6 +++--- src/calibre/utils/unrar.py | 13 +++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/calibre/ebooks/comic/input.py b/src/calibre/ebooks/comic/input.py index 1bf7af23af..87c7832f05 100644 --- a/src/calibre/ebooks/comic/input.py +++ b/src/calibre/ebooks/comic/input.py @@ -115,7 +115,8 @@ class PageProcessor(list): # {{{ from qt.core import QImage from calibre.utils.img import crop_image, image_from_data, scale_image - with open(self.path_to_page, 'rb') as f: + from calibre.utils.filenames import make_long_path_useable + with open(make_long_path_useable(self.path_to_page), 'rb') as f: img = image_from_data(f.read()) width, height = img.width(), img.height() if self.num == 0: # First image so create a thumbnail from it diff --git a/src/calibre/utils/filenames.py b/src/calibre/utils/filenames.py index 631cf4cbc8..e0f8c24ca2 100644 --- a/src/calibre/utils/filenames.py +++ b/src/calibre/utils/filenames.py @@ -601,8 +601,8 @@ rmtree = shutil.rmtree if iswindows: long_path_prefix = '\\\\?\\' - def make_long_path_useable(path): - if len(path) > 200 and os.path.isabs(path) and not path.startswith(long_path_prefix): + def make_long_path_useable(path, threshold=200): + if len(path) > threshold and os.path.isabs(path) and not path.startswith(long_path_prefix): path = long_path_prefix + os.path.normpath(path) return path @@ -636,7 +636,7 @@ if iswindows: raise else: - def make_long_path_useable(path): + def make_long_path_useable(path, threshold=200): return path def get_long_path_name(path): diff --git a/src/calibre/utils/unrar.py b/src/calibre/utils/unrar.py index ed5bc8e033..ebadbc8071 100644 --- a/src/calibre/utils/unrar.py +++ b/src/calibre/utils/unrar.py @@ -12,6 +12,7 @@ from io import BytesIO from calibre.constants import filesystem_encoding, iswindows from calibre.ptempfile import PersistentTemporaryFile, TemporaryDirectory +from calibre.utils.filenames import make_long_path_useable from polyglot.builtins import string_or_bytes @@ -52,19 +53,19 @@ class StreamAsPath: def extract(path_or_stream, location): from unrardll import extract with StreamAsPath(path_or_stream) as path: - return extract(path, location) + return extract(make_long_path_useable(path), make_long_path_useable(location, threshold=0)) def names(path_or_stream): from unrardll import names with StreamAsPath(path_or_stream) as path: - yield from names(path, only_useful=True) + yield from names(make_long_path_useable(path), only_useful=True) def headers(path_or_stream): from unrardll import headers, is_useful with StreamAsPath(path_or_stream) as path: - for h in headers(path): + for h in headers(make_long_path_useable(path)): if is_useful(h): yield h @@ -72,7 +73,7 @@ def headers(path_or_stream): def comment(path_or_stream): from unrardll import comment with StreamAsPath(path_or_stream) as path: - return comment(path) + return comment(make_long_path_useable(path)) def extract_member( @@ -89,7 +90,7 @@ def extract_member( (match is not None and match.search(fname) is not None) with StreamAsPath(path_or_stream) as path: - name, data = extract_member(path, is_match) + name, data = extract_member(make_long_path_useable(path), is_match) if name is not None: return name, data @@ -97,7 +98,7 @@ def extract_member( def extract_members(path_or_stream, callback): from unrardll import extract_members with StreamAsPath(path_or_stream) as path: - extract_members(path, callback) + extract_members(make_long_path_useable(path), callback) def extract_first_alphabetically(stream):