Skip to content

Commit

Permalink
Fix list of files to download showing already downloaded files and no…
Browse files Browse the repository at this point in the history
…t always marking new files with green color when using p2p

Fixes #112
  • Loading branch information
ManlyMarco committed Feb 14, 2023
1 parent 2e9fb8b commit e49dacf
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/KKManager.Updater/Sources/TorrentUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,21 @@ public static async Task<UpdateTask> GetUpdateTask(Torrent torrent, UpdateInfo u
var remoteFiles = new List<UpdateItem>();
foreach (var file in torrentManager.Files)
{
var targetPath = new FileInfo(file.FullPath);

// Seed finished files, but do not download unfinished yet
var isFinished = file.BitField.PercentComplete >= 100d;
// The file is most likely downloaded entirely, but a different file that shares a chunk with this file is missing
// which causes the chunk to be lost after the torrent starts downloading
var isProbablyFinished = isFinished || (targetPath.Exists && targetPath.Length == file.Length);

var info = new TorrentFileInfo(file, torrentManager, updateInfo.ClientPathInfo.FullName, newSource);
var targetPath = new FileInfo(file.FullPath);

await torrentManager.SetFilePriorityAsync(file, isFinished ? Priority.Low : Priority.DoNotDownload);
await torrentManager.SetFilePriorityAsync(file, isFinished ? Priority.Low : isProbablyFinished ? Priority.Highest : Priority.DoNotDownload);
// If files don't exist, a 0 byte placeholder is created by HashCheckAsync, which messes things up later
// Partially downloaded files aren't changed until the torrent is started
// If a file is missing, other fully downloaded files can show as partially downloaded if they share chunks, so don't move those until we start
if (!isFinished && targetPath.Exists && targetPath.Length == 0)
if (!isProbablyFinished && targetPath.Exists && targetPath.Length == 0)
{
Debug.WriteLine($"IncompletePath={file.DownloadIncompleteFullPath} -> CompletePath={file.DownloadCompleteFullPath}");
await torrentManager.MoveFileAsync(file, (await UpdateItem.GetTempDownloadFilename()).FullName);
Expand All @@ -218,8 +222,11 @@ public static async Task<UpdateTask> GetUpdateTask(Torrent torrent, UpdateInfo u
if (torrentManager.State == TorrentState.Error)
throw new IOException("Failed to move file " + file.FullPath, torrentManager.Error?.Exception);
}

// File was moved so it no longer exists at the path, need to refresh or UI will think it still exists
targetPath.Refresh();
}
var updateItem = new UpdateItem(targetPath, info, isFinished, CustomMoveResult);
var updateItem = new UpdateItem(targetPath, info, isProbablyFinished, CustomMoveResult);
remoteFiles.Add(updateItem);
}

Expand Down

0 comments on commit e49dacf

Please sign in to comment.