Skip to content

Commit

Permalink
enable long pathnames on windows (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Sep 25, 2016
1 parent 0a3fb19 commit f32cf28
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions gallery_dl/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import json
import hashlib
import platform
from . import config, extractor, downloader, text, output, exception
from .extractor.message import Message

Expand All @@ -24,7 +25,6 @@ def run(self):
"""Execute or run the job"""
pass


class DownloadJob(Job):
"""Download images into appropriate directory/filename locations"""

Expand Down Expand Up @@ -84,12 +84,13 @@ def download(self, msg):
_, url, metadata = msg
filename = text.clean_path(self.filename_fmt.format(**metadata))
path = os.path.join(self.directory, filename)
if os.path.exists(path):
realpath = self.adjust_path(path)
if os.path.exists(realpath):
self.printer.skip(path)
return
dlinstance = self.get_downloader(url)
self.printer.start(path)
with open(path, "wb") as file:
with open(realpath, "wb") as file:
tries = dlinstance.download(url, file)
self.printer.success(path, tries)

Expand All @@ -103,7 +104,7 @@ def set_directory(self, msg):
self.get_base_directory(),
*segments
)
os.makedirs(self.directory, exist_ok=True)
os.makedirs(self.adjust_path(self.directory), exist_ok=True)

def get_downloader(self, url):
"""Return, and possibly construct, a downloader suitable for 'url'"""
Expand Down Expand Up @@ -133,6 +134,11 @@ def get_base_directory():
bdir = os.path.join(*bdir)
return os.path.expanduser(os.path.expandvars(bdir))

@staticmethod
def adjust_path(path, longpaths=platform.system() == "Windows"):
"""Enable longer-than-260-character paths on windows"""
return "\\\\?\\" + os.path.abspath(path) if longpaths else path


class KeywordJob(Job):
"""Print available keywords"""
Expand Down

0 comments on commit f32cf28

Please sign in to comment.