From 9d1337eb91759355b2ed290e3204607262cd5317 Mon Sep 17 00:00:00 2001 From: Huevos Date: Mon, 2 Sep 2024 02:43:53 +0200 Subject: [PATCH] [Directories.getRecordingFilename] use sanitizeFilename to clean up filename Avoids duplicating code and wasting cpu cycles. --- lib/python/Components/Timeshift.py | 5 ----- lib/python/RecordTimer.py | 4 +--- lib/python/Tools/Directories.py | 18 ++++-------------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/lib/python/Components/Timeshift.py b/lib/python/Components/Timeshift.py index c2bad169571..7ee0dfbcc88 100644 --- a/lib/python/Components/Timeshift.py +++ b/lib/python/Components/Timeshift.py @@ -679,9 +679,6 @@ def SaveTimeshift(self, timeshiftfile=None, mergelater=False): except: print("[Timeshift][TimeShift] Using default filename") - ptsfilename = sanitizeFilename(ptsfilename) - - # print("[Timeshift]ptsfilename", ptsfilename) fullname = getRecordingFilename(ptsfilename, config.usage.default_path.value) # print("[Timeshift]fullname", fullname) oslink("%s%s" % (config.usage.timeshift_path.value, savefilename), "%s.ts" % fullname) @@ -710,8 +707,6 @@ def SaveTimeshift(self, timeshiftfile=None, mergelater=False): except: print("[Timeshift][TimeShift] Using default filename") - ptsfilename = sanitizeFilename(ptsfilename) - fullname = getRecordingFilename(ptsfilename, config.usage.default_path.value) oslink("%s%s" % (config.usage.timeshift_path.value, timeshiftfile), "%s.ts" % fullname) oslink("%s%s.meta" % (config.usage.timeshift_path.value, timeshiftfile), "%s.ts.meta" % fullname) diff --git a/lib/python/RecordTimer.py b/lib/python/RecordTimer.py index 1debc65e8bd..08a477f6999 100644 --- a/lib/python/RecordTimer.py +++ b/lib/python/RecordTimer.py @@ -15,7 +15,7 @@ from Screens.PictureInPicture import PictureInPicture import Screens.Standby from Tools import Notifications, Trashcan -from Tools.Directories import fileReadXML, getRecordingFilename, isPluginInstalled, resolveFilename, sanitizeFilename, SCOPE_CONFIG +from Tools.Directories import fileReadXML, getRecordingFilename, isPluginInstalled, resolveFilename, SCOPE_CONFIG from Tools.XMLTools import stringToXML import NavigationInstance @@ -347,8 +347,6 @@ def calculateFilename(self, name=None): else: filename += " - " + name # standard - filename = sanitizeFilename(filename) - self.Filename = getRecordingFilename(filename, self.MountPath) self.log(0, "Filename calculated as: '%s'" % self.Filename) return self.Filename diff --git a/lib/python/Tools/Directories.py b/lib/python/Tools/Directories.py index e5f9db00c3e..2d110b84728 100644 --- a/lib/python/Tools/Directories.py +++ b/lib/python/Tools/Directories.py @@ -437,20 +437,10 @@ def fileReadXML(filename, default=None, *args, **kwargs): def getRecordingFilename(basename, dirname=None): - # Filter out non-allowed characters. - non_allowed_characters = "/.\\:*?<>|\"" - basename = basename.replace("\xc2\x86", "").replace("\xc2\x87", "") - filename = "" - for c in basename: - if c in non_allowed_characters or ord(c) < 32: - c = "_" - filename += c - # Max filename length for ext4 is 255 (minus 8 characters for .ts.meta) - # but we cannot leave the byte truncate in the middle of a - # multi-byte utf8 character! - # So convert to bytes, truncate then get back to unicode, ignoring - # errors along the way, the result will be valid unicode. - filename = filename.encode(encoding='utf-8', errors='ignore')[:247].decode(encoding='utf-8', errors='ignore') + # The "replaces" remove dvb emphasis chars. + # Also, "." is replaced with "_" which respects the original code. (Why was this required?) + # Max filename length for ext4 is 255 bytes (minus 8 bytes for ".ts.meta") + filename = sanitizeFilename(basename.replace("\xc2\x86", "").replace("\xc2\x87", "").replace(".", "_"), maxlen=247) if dirname is not None: if not dirname.startswith("/"): dirname = pathJoin(defaultRecordingLocation(), dirname)