From e182956a746173034ba7103b4a1c7ba69d3ab744 Mon Sep 17 00:00:00 2001 From: hstr0100 Date: Thu, 21 Nov 2024 11:38:35 -0300 Subject: [PATCH] Workaround for long paths in the direct-http downloader --- .../downloader/DirectHttpDownloader.java | 19 +++++++++++++------ .../gdownloader/util/DirectoryUtils.java | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/net/brlns/gdownloader/downloader/DirectHttpDownloader.java b/core/src/main/java/net/brlns/gdownloader/downloader/DirectHttpDownloader.java index 0f28e17..01633ad 100644 --- a/core/src/main/java/net/brlns/gdownloader/downloader/DirectHttpDownloader.java +++ b/core/src/main/java/net/brlns/gdownloader/downloader/DirectHttpDownloader.java @@ -310,16 +310,23 @@ private boolean downloadFile(QueueEntry queueEntry, ProgressUpdater progressCall throw new IOException("Cannot determine filename: " + fileUrl); } - String urlPath = URLUtils.getDirectoryPath(fileUrl.toString()); + String suggestedUrlPath = URLUtils.getDirectoryPath(fileUrl.toString()); - Path targetPath = Paths.get( - queueEntry.getTmpDirectory().getPath(), - urlPath != null ? urlPath : "", - detectedFileName); + Path basePath = queueEntry.getTmpDirectory().toPath(); + Path urlPath = Paths.get(suggestedUrlPath != null ? suggestedUrlPath : ""); + Path targetPath = basePath.resolve(urlPath); + + int pathLength = targetPath.resolve(detectedFileName).toString().length(); + + if (pathLength > 260 && GDownloader.isWindows()) { // Microsoft shenanigans + log.info("Long path detected, trimming {}", targetPath); + targetPath = DirectoryUtils.trimPathToFit(basePath, urlPath, detectedFileName, 260); + log.info("Trimmed to {}", targetPath); + } Files.createDirectories(targetPath); - File targetFile = targetPath.toFile(); + File targetFile = new File(targetPath.toFile(), detectedFileName); long downloadedBytesSoFar = targetFile.exists() ? targetFile.length() : 0; diff --git a/core/src/main/java/net/brlns/gdownloader/util/DirectoryUtils.java b/core/src/main/java/net/brlns/gdownloader/util/DirectoryUtils.java index 6e8f9d0..7f2ab65 100644 --- a/core/src/main/java/net/brlns/gdownloader/util/DirectoryUtils.java +++ b/core/src/main/java/net/brlns/gdownloader/util/DirectoryUtils.java @@ -131,6 +131,25 @@ private static List getDirectoryNames(String prefix, Path workDir) { return directoryNames; } + public static Path trimPathToFit(Path basePath, Path toTrim, String fileName, int maxLength) { + Path currentPath = toTrim; + + while (currentPath != null) { + String fullPath = basePath.resolve(currentPath).resolve(fileName).toString(); + if (fullPath.length() <= maxLength) { + break; + } + + currentPath = currentPath.getParent(); + } + + if (currentPath == null) { + throw new IllegalArgumentException("Cannot trim path to fit within the maximum length limit."); + } + + return basePath.resolve(currentPath); + } + private static int extractNumber(String directoryName) { return Integer.parseInt(directoryName.replaceAll("\\D+", "")); }