From 8a56e9726f0a1d605651f30e0b20de316d9127dc Mon Sep 17 00:00:00 2001 From: root Date: Sat, 11 Jul 2026 13:57:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=20GID=20=E8=AF=86=E5=88=AB=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E4=B8=8B=E5=8F=91=E7=9A=84=E5=B7=B2=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lion/Service/DownloadCheckService.java | 64 +++++++++++++++---- 1 file changed, 52 insertions(+), 12 deletions(-) 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) == '-'); + } +}