Use slice to shorten name
This commit is contained in:
@@ -65,9 +65,10 @@ class FileNameFormatter:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _limit_file_name_length(filename: str, ending: str) -> str:
|
def _limit_file_name_length(filename: str, ending: str) -> str:
|
||||||
possible_id = re.search(r'((?:_\w{6})?$)', filename).group(1)
|
possible_id = re.search(r'((?:_\w{6})?$)', filename)
|
||||||
ending = possible_id + ending
|
if possible_id:
|
||||||
filename = re.sub(rf"^{possible_id}|{possible_id}$", "", filename)
|
ending = possible_id.group(1) + ending
|
||||||
|
filename = filename[:possible_id.start()]
|
||||||
max_length_chars = 255 - len(ending)
|
max_length_chars = 255 - len(ending)
|
||||||
max_length_bytes = 255 - len(ending.encode('utf-8'))
|
max_length_bytes = 255 - len(ending.encode('utf-8'))
|
||||||
while len(filename) > max_length_chars or len(filename.encode('utf-8')) > max_length_bytes:
|
while len(filename) > max_length_chars or len(filename.encode('utf-8')) > max_length_bytes:
|
||||||
|
|||||||
@@ -160,6 +160,7 @@ def test_limit_filename_length(test_filename: str, test_ending: str):
|
|||||||
@pytest.mark.parametrize(('test_filename', 'test_ending', 'expected_end'), (
|
@pytest.mark.parametrize(('test_filename', 'test_ending', 'expected_end'), (
|
||||||
('test_aaaaaa', '_1.png', 'test_aaaaaa_1.png'),
|
('test_aaaaaa', '_1.png', 'test_aaaaaa_1.png'),
|
||||||
('test_aataaa', '_1.png', 'test_aataaa_1.png'),
|
('test_aataaa', '_1.png', 'test_aataaa_1.png'),
|
||||||
|
('test_abcdef', '_1.png', 'test_abcdef_1.png'),
|
||||||
('test_aaaaaa', '.png', 'test_aaaaaa.png'),
|
('test_aaaaaa', '.png', 'test_aaaaaa.png'),
|
||||||
('test', '_1.png', 'test_1.png'),
|
('test', '_1.png', 'test_1.png'),
|
||||||
('test_m1hqw6', '_1.png', 'test_m1hqw6_1.png'),
|
('test_m1hqw6', '_1.png', 'test_m1hqw6_1.png'),
|
||||||
|
|||||||
Reference in New Issue
Block a user