Conform path length to filename scheme restriction
This commit is contained in:
@@ -27,6 +27,8 @@ class FileNameFormatter:
|
||||
"title",
|
||||
"upvotes",
|
||||
)
|
||||
WINDOWS_MAX_PATH_LENGTH = 260
|
||||
LINUX_MAX_PATH_LENGTH = 4096
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -41,6 +43,10 @@ class FileNameFormatter:
|
||||
self.directory_format_string: list[str] = directory_format_string.split("/")
|
||||
self.time_format_string = time_format_string
|
||||
self.restiction_scheme = restriction_scheme.lower().strip() if restriction_scheme else None
|
||||
if self.restiction_scheme == "windows":
|
||||
self.max_path = self.WINDOWS_MAX_PATH_LENGTH
|
||||
else:
|
||||
self.max_path = self.find_max_path_length()
|
||||
|
||||
def _format_name(self, submission: Union[Comment, Submission], format_string: str) -> str:
|
||||
if isinstance(submission, Submission):
|
||||
@@ -63,6 +69,7 @@ class FileNameFormatter:
|
||||
if platform.system() == "Windows":
|
||||
result = FileNameFormatter._format_for_windows(result)
|
||||
elif self.restiction_scheme == "windows":
|
||||
logger.debug("Forcing Windows-compatible filenames")
|
||||
result = FileNameFormatter._format_for_windows(result)
|
||||
return result
|
||||
|
||||
@@ -135,14 +142,13 @@ class FileNameFormatter:
|
||||
raise BulkDownloaderException(f"Could not determine path name: {subfolder}, {index}, {resource.extension}")
|
||||
return file_path
|
||||
|
||||
@staticmethod
|
||||
def limit_file_name_length(filename: str, ending: str, root: Path) -> Path:
|
||||
def limit_file_name_length(self, filename: str, ending: str, root: Path) -> Path:
|
||||
root = root.resolve().expanduser()
|
||||
possible_id = re.search(r"((?:_\w{6})?$)", filename)
|
||||
if possible_id:
|
||||
ending = possible_id.group(1) + ending
|
||||
filename = filename[: possible_id.start()]
|
||||
max_path = FileNameFormatter.find_max_path_length()
|
||||
max_path = self.max_path
|
||||
max_file_part_length_chars = 255 - len(ending)
|
||||
max_file_part_length_bytes = 255 - len(ending.encode("utf-8"))
|
||||
max_path_length = max_path - len(ending) - len(str(root)) - 1
|
||||
@@ -166,9 +172,9 @@ class FileNameFormatter:
|
||||
return int(subprocess.check_output(["getconf", "PATH_MAX", "/"]))
|
||||
except (ValueError, subprocess.CalledProcessError, OSError):
|
||||
if platform.system() == "Windows":
|
||||
return 260
|
||||
return FileNameFormatter.WINDOWS_MAX_PATH_LENGTH
|
||||
else:
|
||||
return 4096
|
||||
return FileNameFormatter.LINUX_MAX_PATH_LENGTH
|
||||
|
||||
def format_resource_paths(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user