Skip to content

Commit

Permalink
Workaround for long paths in the direct-http downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
hstr0100 committed Nov 21, 2024
1 parent e55cea7 commit e182956
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/net/brlns/gdownloader/util/DirectoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ private static List<String> 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+", ""));
}
Expand Down

0 comments on commit e182956

Please sign in to comment.