Trimming video file name to avoid error when file name is too long.

This commit is contained in:
Roland 2024-07-14 09:54:02 -06:00
parent 37ff1ab2cd
commit cdc73d9d16
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
13 changed files with 140 additions and 27 deletions

View file

@ -223,6 +223,7 @@ __all__ = [
'slow_logger',
'split_lines_by_length',
'timer',
'trim_file_name',
'truncate_generator_bytes',
'truncate_object_bytes',
'tsvector',
@ -994,6 +995,25 @@ def escape_file_name(name: str) -> str:
return name.strip()
# Maximum length is probably 255, but we need more length for large suffixes like `.readability.json`, and temporary
# downloading suffixes.
MAXIMUM_FILE_LENGTH = 180
def trim_file_name(name: str) -> str:
"""Shorten the file name only if it is longer than the file system supports. Trim from the end of the name until
the name is short enough, excluding suffix."""
if len(name) < MAXIMUM_FILE_LENGTH:
return name
# Don't trim the filename to exactly 256 characters.
# This is because a FileGroup will have varying filename lengths.
from wrolpi.files.lib import split_path_stem_and_suffix
stem, suffix = split_path_stem_and_suffix(name)
excess = MAXIMUM_FILE_LENGTH - len(suffix)
return stem[:excess].strip() + suffix
def native_only(func: callable):
"""Wraps a function. Raises NativeOnly if run while Dockerized."""