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

Commit 5298102

Browse files
Using DownloadResult and calculating MD5
1 parent 8e07c41 commit 5298102

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

src/GitHub.Api/IO/FileSystem.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public bool FileExists(string filename)
3434
return File.Exists(filename);
3535
}
3636

37+
public long FileLength(string path)
38+
{
39+
var fileInfo = new FileInfo(path);
40+
return fileInfo.Length;
41+
}
42+
3743
public IEnumerable<string> GetDirectories(string path)
3844
{
3945
return Directory.GetDirectories(path);
@@ -206,5 +212,10 @@ public Stream OpenRead(string path)
206212
{
207213
return File.OpenRead(path);
208214
}
215+
216+
public Stream OpenWrite(string path, FileMode mode)
217+
{
218+
return new FileStream(path, mode);
219+
}
209220
}
210221
}

src/GitHub.Api/IO/IFileSystem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace GitHub.Unity
77
public interface IFileSystem
88
{
99
bool FileExists(string path);
10+
long FileLength(string path);
1011
string Combine(string path1, string path2);
1112
string Combine(string path1, string path2, string path3);
1213
string GetFullPath(string path);
@@ -37,6 +38,7 @@ public interface IFileSystem
3738
string ReadAllText(string path);
3839
string ReadAllText(string path, Encoding encoding);
3940
Stream OpenRead(string path);
41+
Stream OpenWrite(string path, FileMode mode);
4042
string[] ReadAllLines(string path);
4143
char DirectorySeparatorChar { get; }
4244
bool ExistingPathIsDirectory(string path);

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ public static bool Copy(Stream source, Stream destination, int chunkSize, long t
7676

7777
public class DownloadResult
7878
{
79+
public bool Success { get; set; }
7980

81+
public string Url { get; set; }
82+
83+
public string Destination { get; set; }
84+
85+
public string MD5Sum { get; set; }
8086
}
8187

8288
public static class WebRequestExtensions
@@ -96,32 +102,37 @@ public static WebResponse GetResponseWithoutException(this WebRequest request)
96102

97103
class DownloadTask: TaskBase<DownloadResult>
98104
{
105+
private IFileSystem fileSystem;
99106
private long bytes;
100107
private WebRequest webRequest;
101108
private bool restarted;
102109

103110
public float Progress { get; set; }
104111

105-
public DownloadTask(CancellationToken token, string url, string destination)
112+
public DownloadTask(CancellationToken token, IFileSystem fileSystem, string url, string destination)
106113
: base(token)
107114
{
115+
this.fileSystem = fileSystem;
108116
Url = url;
109117
Destination = destination;
110118
Name = "DownloadTask";
111119
}
112120

113121
protected override DownloadResult RunWithReturn(bool success)
114122
{
115-
DownloadResult result = base.RunWithReturn(success);
116-
123+
base.RunWithReturn(success);
124+
117125
RaiseOnStart();
118126

127+
var downloadResult = new DownloadResult {
128+
Url = Url,
129+
Destination = Destination
130+
};
131+
119132
try
120133
{
121-
Logger.Trace("Downloading");
122-
RunDownload();
123-
124-
Logger.Trace("Downloaded");
134+
downloadResult.Success = Download();
135+
downloadResult.MD5Sum = fileSystem.CalculateMD5(Destination);
125136
}
126137
catch (Exception ex)
127138
{
@@ -131,30 +142,31 @@ protected override DownloadResult RunWithReturn(bool success)
131142
}
132143
finally
133144
{
134-
RaiseOnEnd(result);
145+
RaiseOnEnd(downloadResult);
135146
}
136147

137-
return result;
148+
return downloadResult;
138149
}
139150

140151
protected virtual void UpdateProgress(float progress)
141152
{
142153
Progress = progress;
143154
}
144155

145-
public bool RunDownload()
156+
public bool Download()
146157
{
147-
var fileInfo = new FileInfo(Destination);
148-
if (fileInfo.Exists)
158+
FileInfo fileInfo = new FileInfo(Destination);
159+
if (fileSystem.FileExists(Destination))
149160
{
150-
if (fileInfo.Length > 0)
161+
var fileLength = fileSystem.FileLength(Destination);
162+
if (fileLength > 0)
151163
{
152164
bytes = fileInfo.Length;
153165
restarted = true;
154166
}
155-
else if (fileInfo.Length == 0)
167+
else if (fileLength == 0)
156168
{
157-
fileInfo.Delete();
169+
fileSystem.FileDelete(Destination);
158170
}
159171
}
160172

@@ -209,7 +221,7 @@ public bool RunDownload()
209221

210222
using (var responseStream = webResponse.GetResponseStream())
211223
{
212-
using (Stream destinationStream = new FileStream(Destination, FileMode.Append))
224+
using (var destinationStream = fileSystem.OpenWrite(Destination, FileMode.Append))
213225
{
214226
if (Token.IsCancellationRequested)
215227
return false;

src/tests/IntegrationTests/Download/DownloadTaskTests.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ public async Task TestDownloadTask()
2525
var downloadPath = TestBasePath.Combine("5MB.zip");
2626
var downloadHalfPath = TestBasePath.Combine("5MB-split.zip");
2727

28-
var downloadTask = new DownloadTask(CancellationToken.None, TestDownload, downloadPath);
29-
await downloadTask.StartAwait();
28+
var downloadTask = new DownloadTask(CancellationToken.None, fileSystem, TestDownload, downloadPath);
29+
var downloadResult = await downloadTask.StartAwait();
3030

3131
var downloadPathBytes = fileSystem.ReadAllBytes(downloadPath);
3232
Logger.Trace("File size {0} bytes", downloadPathBytes.Length);
3333

34-
var computedHash = fileSystem.CalculateMD5(downloadPath);
35-
computedHash.Should().Be(TestDownloadMD5.ToUpperInvariant());
34+
downloadResult.MD5Sum.Should().Be(TestDownloadMD5.ToUpperInvariant());
3635

3736
var random = new Random();
3837
var takeCount = random.Next(downloadPathBytes.Length);
@@ -42,14 +41,13 @@ public async Task TestDownloadTask()
4241
var cutDownloadPathBytes = downloadPathBytes.Take(takeCount).ToArray();
4342
fileSystem.WriteAllBytes(downloadHalfPath, cutDownloadPathBytes);
4443

45-
downloadTask = new DownloadTask(CancellationToken.None, TestDownload, downloadHalfPath);
46-
await downloadTask.StartAwait();
44+
downloadTask = new DownloadTask(CancellationToken.None, fileSystem, TestDownload, downloadHalfPath);
45+
downloadResult = await downloadTask.StartAwait();
4746

4847
var downloadHalfPathBytes = fileSystem.ReadAllBytes(downloadHalfPath);
4948
Logger.Trace("File size {0} Bytes", downloadHalfPathBytes.Length);
5049

51-
computedHash = fileSystem.CalculateMD5(downloadHalfPath);
52-
computedHash.Should().Be(TestDownloadMD5.ToUpperInvariant());
50+
downloadResult.MD5Sum.Should().Be(TestDownloadMD5.ToUpperInvariant());
5351
}
5452
}
5553
}

0 commit comments

Comments
 (0)