Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

".." in directory does not work (at least not on Windows) #5942

Closed
fireattack opened this issue Aug 5, 2024 · 1 comment
Closed

".." in directory does not work (at least not on Windows) #5942

fireattack opened this issue Aug 5, 2024 · 1 comment

Comments

@fireattack
Copy link
Contributor

fireattack commented Aug 5, 2024

Test with config.json:

{
    "extractor": {
        "postprocessors": [
            {
                "name": "metadata",
                "event": "after",
                "directory": [".."],
                "filename": "!metadata.jsonl",
                "mode": "jsonl",
                "open": "a"
            }
        ]
    }
}

Prior to 1bdb0f7, use .. will unexpectedly change the dir to root drive. After that fix, it straightly up throws errors on Windows:

>python -m gallery_dl -c config.json --config-ignore "https://en.wikipedia.org/wiki/File:Malabar_pied_hornbill_(Anthracoceros_coronatus)_male.jpg"
.\gallery-dl\wikipedia\File_Malabar_pied_hornbill_(Anthracoceros_coronatus)_male.jpg\Malabar pied hornbill (Anthracoceros coronatus) male (4a77f077).jpg
[wikimedia][error] Unable to download data:  OSError: [Errno 22] Invalid argument: '\\\\?\\C:\\files\\repo\\gallery-dl_og\\gallery-dl\\wikipedia\\File_Malabar_pied_hornbill_(Anthracoceros_coronatus)_male.jpg\\..\\!metadata.jsonl'

Related: #5262 (comment)

@fireattack
Copy link
Contributor Author

fireattack commented Aug 5, 2024

It looks like path.py has all the batteries to solve this (mainly, os.path.abspath() which would resolve relative path(s)):

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

but it isn't used in _directory_format. I see lots of other places are still manually joining paths, too. Maybe we can create a static method for that?

def _directory_format(self, pathfmt):
formatters = pathfmt.directory_formatters
conditions = pathfmt.directory_conditions
try:
pathfmt.directory_formatters = self._directory_formatters
pathfmt.directory_conditions = ()
segments = pathfmt.build_directory(pathfmt.kwdict)
directory = pathfmt.clean_path(os.sep.join(segments) + os.sep)
return os.path.join(pathfmt.realdirectory, directory)
finally:
pathfmt.directory_conditions = conditions
pathfmt.directory_formatters = formatters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants