From 834edadc280128c9bda86c4273056e1fd9daa19d Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Tue, 29 Mar 2022 18:28:53 +0200 Subject: [PATCH] Possible fix for #2350 and #2351 (databse locked) Fix for #2309 (long unicode filenames could get to long) --- cps.py | 10 ++--- cps/helper.py | 18 ++++++++- test/Calibre-Web TestSummary_Linux.html | 50 +++++++++++++++++-------- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/cps.py b/cps.py index 8959679a..bffdfc7f 100755 --- a/cps.py +++ b/cps.py @@ -16,11 +16,11 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -try: - from gevent import monkey - monkey.patch_all() -except ImportError: - pass +#try: +#from gevent import monkey +#monkey.patch_all() +#except ImportError: +# pass import sys import os diff --git a/cps/helper.py b/cps/helper.py index 4928d388..742188d0 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -19,6 +19,7 @@ import os import io +import sys import mimetypes import re import shutil @@ -224,11 +225,23 @@ def send_mail(book_id, book_format, convert, kindle_mail, calibrepath, user_id): return _(u"The requested file could not be read. Maybe wrong permissions?") +def shorten_component(s, by_what): + l = len(s) + if l < by_what: + return s + l = (l - by_what)//2 + if l <= 0: + return s + return s[:l] + s[-l:] + + def get_valid_filename(value, replace_whitespace=True, chars=128): """ Returns the given string converted to a string that can be used for a clean filename. Limits num characters to 128 max. """ + + if value[-1:] == u'.': value = value[:-1]+u'_' value = value.replace("/", "_").replace(":", "_").strip('\0') @@ -239,7 +252,10 @@ def get_valid_filename(value, replace_whitespace=True, chars=128): value = re.sub(r'[*+:\\\"/<>?]+', u'_', value, flags=re.U) # pipe has to be replaced with comma value = re.sub(r'[|]+', u',', value, flags=re.U) - value = value[:chars].strip() + + filename_encoding_for_length = 'utf-16' if sys.platform == "win32" or sys.platform == "darwin" else 'utf-8' + value = value.encode(filename_encoding_for_length)[:chars].decode('utf-8', errors='ignore').strip() + if not value: raise ValueError("Filename cannot be empty") return value diff --git a/test/Calibre-Web TestSummary_Linux.html b/test/Calibre-Web TestSummary_Linux.html index 54fda2b1..524db518 100644 --- a/test/Calibre-Web TestSummary_Linux.html +++ b/test/Calibre-Web TestSummary_Linux.html @@ -37,14 +37,14 @@
-

Start Time: 2022-03-28 06:40:49

+

Start Time: 2022-03-28 21:45:14

-

Stop Time: 2022-03-28 12:18:13

+

Stop Time: 2022-03-29 03:21:52

@@ -1562,11 +1562,11 @@ - + TestLoadMetadata 1 - 1 0 + 1 0 0 @@ -1576,11 +1576,31 @@ - +
TestLoadMetadata - test_load_metadata
- PASS + +
+ FAIL +
+ + + + @@ -4548,8 +4568,8 @@ Total 406 - 400 - 0 + 399 + 1 0 6   @@ -4643,6 +4663,12 @@ Basic + + gevent + 21.12.0 + Basic + + greenlet 1.1.2 @@ -4697,12 +4723,6 @@ Basic - - tornado - 6.1 - Basic - - Unidecode 1.3.4 @@ -4993,7 +5013,7 @@