From 01bfe4de97d2d09b216aec19d0c819d70a13fb3c Mon Sep 17 00:00:00 2001 From: Roland Date: Wed, 2 Feb 2022 20:48:43 -0700 Subject: [PATCH] Limiting maximum title length in Archive files. --- modules/archive/lib.py | 3 +++ modules/archive/test/test_lib.py | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/archive/lib.py b/modules/archive/lib.py index ee17d29d..85a4a3d3 100644 --- a/modules/archive/lib.py +++ b/modules/archive/lib.py @@ -78,6 +78,7 @@ def get_new_archive_files(url: str, title: Optional[str]) -> ArchiveFiles: dt = archive_strftime(now()) title = escape_file_name(title or 'NA') + title = title[:50] prefix = f'{dt}_{title}' singlefile_path = directory / f'{prefix}.html' readability_path = directory / f'{prefix}.readability.html' @@ -407,6 +408,8 @@ def migrate_archive_files(): with archive_files.readability_json.open() as fh: title = escape_file_name(json.load(fh).get('title') or 'NA') + title = title[:50] + dt = archive_strftime(dt) prefix = f'{dt}_{title}' singlefile_path = domain_directory / f'{prefix}.html' diff --git a/modules/archive/test/test_lib.py b/modules/archive/test/test_lib.py index b226abf0..a6402d81 100644 --- a/modules/archive/test/test_lib.py +++ b/modules/archive/test/test_lib.py @@ -490,7 +490,7 @@ def test_migrate_archive_files(test_session, archive_directory, archive_factory) # Archive6 (example_org_directory / '2000-01-01 00:00:00.000000.html').touch() path = example_org_directory / '2000-01-01 00:00:00.000000.readability.json' - path.write_text(json.dumps({'title': 'invalid#*: title'})) + path.write_text(json.dumps({'title': 'A' * 500})) # Really long file name. # Migration forms a plan, then performs it. plan = lib.migrate_archive_files() @@ -510,9 +510,10 @@ def test_migrate_archive_files(test_session, archive_directory, archive_factory) (pathlib.Path('example.com/2021-10-05 16:20:10.346823.readability.json'), pathlib.Path('example.com/2021-10-05-16-20-10_my title.readability.json')), (pathlib.Path('example.org/2000-01-01 00:00:00.000000.html'), - pathlib.Path('example.org/2000-01-01-00-00-00_invalid# title.html')), + pathlib.Path('example.org/2000-01-01-00-00-00_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.html')), (pathlib.Path('example.org/2000-01-01 00:00:00.000000.readability.json'), - pathlib.Path('example.org/2000-01-01-00-00-00_invalid# title.readability.json')), + pathlib.Path( + 'example.org/2000-01-01-00-00-00_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.readability.json')), ] # New Archive files still exist. assert all(i.is_file() for i in archive1.my_paths())