按 GID 识别重复下发的已完成任务

This commit is contained in:
root 2026-07-11 13:57:00 +08:00
parent 3ab82509a0
commit 8a56e9726f

View File

@ -125,23 +125,63 @@ public class DownloadCheckService {
} }
public boolean addToQueue(GalleryTask galleryTask){ public boolean addToQueue(GalleryTask galleryTask){
if(galleryTask.getName() == null || galleryTask.getName().isEmpty()){ // A reconnect can resend a task whose name is stale (for example, the
queue.putIfAbsent(galleryTask.getGid(), galleryTask); // downloader appended a resolution suffix). Resolve completed archives
return false; // by gid first, because the gid is stable while the directory name is not.
} File storedDirectory = findStoredDirectoryByGid(galleryTask.getGid());
if(storedDirectory != null){
if(new File(downloadPath + galleryTask.getGid()).isDirectory()){ galleryTask.setName(storedDirectory.getName());
queue.putIfAbsent(galleryTask.getGid(), galleryTask);
return false;
}
if(new File(storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip").exists()){
galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE); galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE);
CustomUtil.notifyMe(String.format("任务:%s在添加时已下载完成更新任务状态", galleryTask.getName())); CustomUtil.notifyMe(String.format("任务:%s在添加时已下载完成更新任务状态", galleryTask.getName()));
return true; 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); queue.putIfAbsent(galleryTask.getGid(), galleryTask);
return false; return false;
} }
queue.putIfAbsent(galleryTask.getGid(), galleryTask);
return false;
}
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) == '-');
}
} }