Skip to content

Commit fee56a4

Browse files
committed
Don't restore existing files
1 parent e43d916 commit fee56a4

File tree

8 files changed

+70
-10
lines changed

8 files changed

+70
-10
lines changed

Add.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static Task DownloadPackage(string package, string basePath, IEnumerable
5656
{
5757
if (file.Type == "file")
5858
{
59-
tasks.Add(Download.DistFile(package, $"{basePath}{file.Path}"));
59+
tasks.Add(DownloadFile(package, basePath, file));
6060
}
6161
else if (file.Files?.Count > 0)
6262
{
@@ -66,5 +66,10 @@ private static Task DownloadPackage(string package, string basePath, IEnumerable
6666

6767
return Task.WhenAll(tasks);
6868
}
69+
70+
private static async Task DownloadFile(string package, string basePath, DistFile file)
71+
{
72+
file.LocalPath = await Download.DistFile(package, $"{basePath}{file.Path}");
73+
}
6974
}
7075
}

DistFile.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class DistFile
99
public DateTimeOffset LastModified { get; set; }
1010
public string ContentType { get; set; }
1111
public string Path { get; set; }
12+
public string LocalPath { get; set; }
1213
public int Size { get; set; }
1314
public string Type { get; set; }
1415
public string Integrity { get; set; }

Download.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static class Download
1414
};
1515
private static readonly string BaseDirectory = Path.Combine("wwwroot", "lib");
1616

17-
public static async Task DistFile(string package, string path)
17+
public static async Task<string> DistFile(string package, string path)
1818
{
1919
var target = TargetFile(path);
2020
using (var response = await Client.GetAsync(path))
@@ -29,16 +29,19 @@ public static async Task DistFile(string package, string path)
2929
Directory.CreateDirectory(directory);
3030
}
3131

32-
using (var fileStream = File.Create(Path.Combine(directory, file)))
32+
var localPath = Path.Combine(directory, file);
33+
using (var fileStream = File.Create(localPath))
3334
{
3435
await response.Content.CopyToAsync(fileStream);
3536
}
3637

37-
Console.WriteLine($"{Path.Combine(package, target)}... OK");
38+
Console.WriteLine($"{response.RequestMessage.RequestUri}... OK");
39+
return localPath;
3840
}
3941
else
4042
{
41-
Console.WriteLine($"{Path.Combine(package, target)}... failed ({(int)response.StatusCode}");
43+
Console.WriteLine($"{response.RequestMessage.RequestUri}... failed ({(int)response.StatusCode})");
44+
return null;
4245
}
4346
}
4447
}

Restore.cs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Net.Http;
6+
using System.Security.Cryptography;
67
using System.Threading.Tasks;
78
using Newtonsoft.Json.Linq;
89

@@ -53,8 +54,50 @@ private static Task DownloadFiles(string package, JObject entry)
5354
var files = (JArray) entry["files"];
5455

5556
return Task.WhenAll(files
56-
.Select(f => f["file"].Value<string>())
57-
.Select(f => Download.DistFile(package, $"{version}/{f}")));
57+
.Select(f => DownloadFile(package, version, (JObject)f)));
58+
}
59+
60+
private static Task DownloadFile(string package, string version, JObject file)
61+
{
62+
var local = file["local"].Value<string>();
63+
if (File.Exists(local))
64+
{
65+
var integrity = file["integrity"].Value<string>();
66+
var integrityBits = integrity.Split('-', 2);
67+
if (integrityBits.Length == 2)
68+
{
69+
var hashAlgorithm = GetAlgorithm(integrityBits[0]);
70+
if (hashAlgorithm != null)
71+
{
72+
using (var stream = File.OpenRead(local))
73+
{
74+
var hash = hashAlgorithm.ComputeHash(stream);
75+
if (integrityBits[1].Equals(Convert.ToBase64String(hash)))
76+
{
77+
Console.WriteLine($"{local} is up-to-date.");
78+
return Task.CompletedTask;
79+
}
80+
}
81+
}
82+
}
83+
}
84+
85+
return Download.DistFile(package, $"{version}/{file["file"].Value<string>()}");
86+
}
87+
88+
private static HashAlgorithm GetAlgorithm(string name)
89+
{
90+
switch (name.ToLowerInvariant())
91+
{
92+
case "sha256":
93+
return SHA256.Create();
94+
case "sha384":
95+
return SHA384.Create();
96+
case "sha512":
97+
return SHA512.Create();
98+
default:
99+
return null;
100+
}
58101
}
59102
}
60103
}

UnpkgJson.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public static async Task Save(IEnumerable<UnpkgJsonEntry> entries)
3131
var jEntry = new JObject
3232
{
3333
["version"] = CleanVersion(entry.Version),
34-
["files"] = JArray.FromObject(entry.Files.Select(f => new {file = f.Path, cdn = f.CdnUrl, integrity = f.Integrity}))
34+
["files"] = JArray.FromObject(entry.Files.Select(f =>
35+
new {file = f.Path, cdn = f.CdnUrl, local = f.LocalPath, integrity = f.Integrity}))
3536
};
3637
file[CleanPackageName(entry.PackageName)] = jEntry;
3738
}

UnpkgJsonEntry.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ private static void AddFiles(List<UnpkgJsonFile> files, string version, IEnumera
2626
{
2727
if (distFile.Type == "file")
2828
{
29-
files.Add(new UnpkgJsonFile { Path = distFile.Path, CdnUrl = $"https://unpkg.com/{version}{distFile.Path}", Integrity = distFile.Integrity});
29+
files.Add(new UnpkgJsonFile
30+
{
31+
Path = distFile.Path,
32+
LocalPath = distFile.LocalPath,
33+
CdnUrl = $"https://unpkg.com/{version}{distFile.Path}",
34+
Integrity = distFile.Integrity
35+
});
3036
}
3137
else if (distFile.Files?.Count > 0)
3238
{

UnpkgJsonFile.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ public class UnpkgJsonFile
55
public string Path { get; set; }
66
public string Integrity { get; set; }
77
public string CdnUrl { get; set; }
8+
public string LocalPath { get; set; }
89
}
910
}

dotnet-unpkg.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<Version>1.0.0-preview3</Version>
3+
<Version>1.0.0</Version>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp2.0</TargetFramework>
66
<LangVersion>latest</LangVersion>

0 commit comments

Comments
 (0)