Skip to content

Commit

Permalink
[Directories.getRecordingFilename] use sanitizeFilename to clean up f…
Browse files Browse the repository at this point in the history
…ilename

Avoids duplicating code and wasting cpu cycles.
  • Loading branch information
Huevos committed Sep 2, 2024
1 parent d493b6b commit 9d1337e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 22 deletions.
5 changes: 0 additions & 5 deletions lib/python/Components/Timeshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions lib/python/RecordTimer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 4 additions & 14 deletions lib/python/Tools/Directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9d1337e

Please sign in to comment.