This repository was archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemote.cs
More file actions
executable file
·82 lines (69 loc) · 2.73 KB
/
Remote.cs
File metadata and controls
executable file
·82 lines (69 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
namespace AutoArc
{
public class Remote
{
public const string LocalPath = "bin64/";
public const string LocalBakPath = "bin64/bak/";
public string Name { get; set; }
public bool BackupOld { get; set; } = true;
public string RemotePath { get; set; }
public string ChecksumPath { get; set; }
public string LocalName
{
get => this.localName ?? this.RemotePath.Split('/').Last();
set => this.localName = value;
}
private string localName = null;
public void Update()
{
Console.WriteLine("Checking for {0} updates...", this.Name);
string fullLocalPath = LocalPath + this.LocalName;
using (var resp = (HttpWebResponse)WebRequest.Create(this.RemotePath).GetResponse())
{
if (File.Exists(fullLocalPath) && File.GetLastWriteTimeUtc(fullLocalPath) >= resp.LastModified)
{
Console.WriteLine("Up to date ({0}).\n", resp.LastModified);
return;
}
Console.WriteLine("Downloading update ({0})...", resp.LastModified);
}
if (File.Exists(fullLocalPath))
{
Directory.CreateDirectory(LocalBakPath);
File.Copy(fullLocalPath, LocalBakPath + this.LocalName, true);
}
byte[] file;
using (var wc = new WebClient())
{
file = wc.DownloadData(this.RemotePath);
if (this.ChecksumPath != null)
{
using (var md5 = MD5.Create())
{
string checksum = wc.DownloadString(this.ChecksumPath).Split(' ')[0].Trim();
string downloadChecksum = BitConverter.ToString(md5.ComputeHash(file)).Replace("-", "").ToLower();
bool equal = checksum == downloadChecksum;
Console.WriteLine("{0} == {1} ({2})", checksum, downloadChecksum, equal);
if (!equal)
{
Console.WriteLine();
Console.ReadKey();
return;
}
}
}
else
{
Console.WriteLine("No checksum verification for {0}.", this.Name);
}
}
Console.WriteLine();
File.WriteAllBytes(fullLocalPath, file);
}
}
}