Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 84215cc

Browse files
Preventing download of a file when it exists
The DownloadTask will download and append to a file that already exists and passes MD5. In order to fix this: - Check if the file exists - If there is no validation hash delete the file - If there is a validation hash, check if it matches, if so bypass the downloading an additional check
1 parent d428452 commit 84215cc

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,53 @@ protected virtual string RunDownload(bool success)
9999
{
100100
Logger.Trace($"Download of {Url} to {Destination} Attempt {attempts + 1} of {RetryCount + 1}");
101101

102-
using (var destinationStream = fileSystem.OpenWrite(Destination, FileMode.Append))
102+
var fileExistsAndValid = false;
103+
if (Destination.FileExists())
103104
{
104-
result = Utils.Download(Logger, Url, destinationStream,
105-
(value, total) =>
105+
if (ValidationHash == null)
106+
{
107+
Destination.Delete();
108+
}
109+
else
110+
{
111+
var md5 = fileSystem.CalculateFileMD5(Destination);
112+
result = md5.Equals(ValidationHash, StringComparison.CurrentCultureIgnoreCase);
113+
114+
if (result)
106115
{
107-
UpdateProgress(value, total);
108-
return !Token.IsCancellationRequested;
109-
});
116+
Logger.Trace($"Download previously exists & confirmed {md5}");
117+
fileExistsAndValid = true;
118+
}
119+
}
110120
}
111121

112-
if (result && ValidationHash != null)
122+
if (!fileExistsAndValid)
113123
{
114-
var md5 = fileSystem.CalculateFileMD5(TargetDirectory);
115-
result = md5.Equals(ValidationHash, StringComparison.CurrentCultureIgnoreCase);
116-
117-
if (!result)
124+
using (var destinationStream = fileSystem.OpenWrite(Destination, FileMode.Append))
118125
{
119-
Logger.Warning($"Downloaded MD5 {md5} does not match {ValidationHash}. Deleting {TargetDirectory}.");
120-
fileSystem.FileDelete(TargetDirectory);
126+
result = Utils.Download(Logger, Url, destinationStream,
127+
(value, total) =>
128+
{
129+
UpdateProgress(value, total);
130+
return !Token.IsCancellationRequested;
131+
});
121132
}
122-
else
133+
134+
if (result && ValidationHash != null)
123135
{
124-
Logger.Trace($"Download confirmed {md5}");
125-
break;
136+
var md5 = fileSystem.CalculateFileMD5(Destination);
137+
result = md5.Equals(ValidationHash, StringComparison.CurrentCultureIgnoreCase);
138+
139+
if (!result)
140+
{
141+
Logger.Warning($"Downloaded MD5 {md5} does not match {ValidationHash}. Deleting {Destination}.");
142+
fileSystem.FileDelete(TargetDirectory);
143+
}
144+
else
145+
{
146+
Logger.Trace($"Download confirmed {md5}");
147+
break;
148+
}
126149
}
127150
}
128151
}

0 commit comments

Comments
 (0)