utils: In add_filename() don't put a final dot if there is no extension in the filename --> to head
patch 6493c04f9d8bdb7b1d2c037fdd66c337a7a91af5
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun May 31 00:26:34 CEST 2026
* utils: Modify sanitize_filename(): convert invalid chars to hex representation,
this makes the filenames deterministic for the same session,
and helps to not download a file more than once
patch dd18dbebc3877faa7fe1c7f1257fb3a4048aab32
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sat May 30 22:25:32 CEST 2026
* utils: In add_filename() don't put a final dot if there is no extension in the filename
diff -rN -u old-irgramd/utils.py new-irgramd/utils.py
--- old-irgramd/utils.py 2026-06-09 14:31:37.959945759 +0200
+++ new-irgramd/utils.py 2026-06-09 14:31:37.959945759 +0200
@@ -18,7 +18,7 @@
# Constants
-FILENAME_INVALID_CHARS = re.compile('[/{}<>()"\'\\|&#%?]')
+FILENAME_INVALID_CHARS = re.compile('[\0-\x1F/{}<>"\'\\|*&#%?\x7F]')
SIMPLE_URL = re.compile('http(|s)://[^ ]+')
from include import MAX_LINE
@@ -87,22 +87,23 @@
return messages_limited
def sanitize_filename(fn):
- cn = str(sanitize_filename.cn)
- new_fn, ns = FILENAME_INVALID_CHARS.subn(cn, fn)
- if ns:
- sanitize_filename.cn += 1
- return new_fn.strip('-').replace(' ','_')
-sanitize_filename.cn = 0
+ def hexize(m):
+ return '-{:x}-'.format(ord(m.group(0)))
+
+ new_fn = FILENAME_INVALID_CHARS.sub(hexize, fn)
+ return new_fn.lstrip('-').replace(' ','_')
def add_filename(filename, add):
if add:
aux = filename.rsplit('.', 1)
name = aux[0]
+ last_dot = '.'
try:
ext = aux[1]
except:
ext = ''
- return '{}-{}.{}'.format(name, add, ext)
+ last_dot = ''
+ return '{}-{}{}{}'.format(name, add, last_dot, ext)
else:
return filename