diff --git a/src/main/java/lion/Service/DownloadCheckService.java b/src/main/java/lion/Service/DownloadCheckService.java index da4774f..36cf25a 100644 --- a/src/main/java/lion/Service/DownloadCheckService.java +++ b/src/main/java/lion/Service/DownloadCheckService.java @@ -125,23 +125,63 @@ public class DownloadCheckService { } public boolean addToQueue(GalleryTask galleryTask){ - if(galleryTask.getName() == null || galleryTask.getName().isEmpty()){ - queue.putIfAbsent(galleryTask.getGid(), galleryTask); - return false; - } - - if(new File(downloadPath + galleryTask.getGid()).isDirectory()){ - queue.putIfAbsent(galleryTask.getGid(), galleryTask); - return false; - } - - if(new File(storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip").exists()){ + // A reconnect can resend a task whose name is stale (for example, the + // downloader appended a resolution suffix). Resolve completed archives + // by gid first, because the gid is stable while the directory name is not. + File storedDirectory = findStoredDirectoryByGid(galleryTask.getGid()); + if(storedDirectory != null){ + galleryTask.setName(storedDirectory.getName()); galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE); CustomUtil.notifyMe(String.format("任务:%s在添加时已下载完成,更新任务状态", galleryTask.getName())); return true; } + // If the task is still downloading, retain the actual directory name so + // subsequent progress and compression use the same identity. + File downloadingDirectory = findDirectoryByGid(new File(downloadPath), galleryTask.getGid()); + if(downloadingDirectory != null){ + galleryTask.setName(downloadingDirectory.getName()); + queue.putIfAbsent(galleryTask.getGid(), galleryTask); + return false; + } + queue.putIfAbsent(galleryTask.getGid(), galleryTask); return false; } -} \ No newline at end of file + + private File findStoredDirectoryByGid(int gid){ + File storageDirectory = new File(storagePath); + File[] directories = storageDirectory.listFiles(File::isDirectory); + if(directories == null) + return null; + + for(File directory : directories){ + if(matchesGid(directory.getName(), gid) + && new File(directory, directory.getName() + ".zip").isFile()) + return directory; + } + return null; + } + + private File findDirectoryByGid(File parentDirectory, int gid){ + File[] directories = parentDirectory.listFiles(File::isDirectory); + if(directories == null) + return null; + + for(File directory : directories) + if(matchesGid(directory.getName(), gid)) + return directory; + return null; + } + + private boolean matchesGid(String name, int gid){ + String gidMarker = "[" + gid; + int markerIndex = name.lastIndexOf(gidMarker); + if(markerIndex < 0) + return false; + + int suffixIndex = markerIndex + gidMarker.length(); + return suffixIndex < name.length() + && (name.charAt(suffixIndex) == ']' || name.charAt(suffixIndex) == '-'); + } +}