From eef5beb841c44f6d1f2af05f59a2c5f30a7bc5a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 27 Aug 2024 14:38:40 +0200 Subject: [PATCH] [pp:metadata] fix using '..' in directories on Windows (#5942, #6094) --- gallery_dl/path.py | 28 +++++++++++++++------------- gallery_dl/postprocessor/metadata.py | 5 ++++- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/gallery_dl/path.py b/gallery_dl/path.py index 78927764..d408a41a 100644 --- a/gallery_dl/path.py +++ b/gallery_dl/path.py @@ -184,29 +184,31 @@ class PathFormat(): def set_directory(self, kwdict): """Build directory path and create it if necessary""" self.kwdict = kwdict - sep = os.sep segments = self.build_directory(kwdict) if segments: self.directory = directory = self.basedirectory + self.clean_path( - sep.join(segments) + sep) + os.sep.join(segments) + os.sep) else: self.directory = directory = self.basedirectory if WINDOWS and self.extended: - # Enable longer-than-260-character paths - directory = os.path.abspath(directory) - if directory.startswith("\\\\"): - directory = "\\\\?\\UNC\\" + directory[2:] - else: - directory = "\\\\?\\" + directory - - # abspath() in Python 3.7+ removes trailing path separators (#402) - if directory[-1] != sep: - directory += sep - + directory = self._extended_path(directory) self.realdirectory = directory + def _extended_path(self, path): + # Enable longer-than-260-character paths + path = os.path.abspath(path) + if not path.startswith("\\\\"): + path = "\\\\?\\" + path + elif not path.startswith("\\\\?\\"): + path = "\\\\?\\UNC\\" + path[2:] + + # abspath() in Python 3.7+ removes trailing path separators (#402) + if path[-1] != os.sep: + return path + os.sep + return path + def set_filename(self, kwdict): """Set general filename data""" self.kwdict = kwdict diff --git a/gallery_dl/postprocessor/metadata.py b/gallery_dl/postprocessor/metadata.py index 08a08f11..3ef9fbce 100644 --- a/gallery_dl/postprocessor/metadata.py +++ b/gallery_dl/postprocessor/metadata.py @@ -114,7 +114,10 @@ class MetadataPP(PostProcessor): if archive and archive.check(pathfmt.kwdict): return - directory = self._directory(pathfmt) + if util.WINDOWS and pathfmt.extended: + directory = pathfmt._extended_path(self._directory(pathfmt)) + else: + directory = self._directory(pathfmt) path = directory + self._filename(pathfmt) if self.skip and os.path.exists(path):