|
1 | | -using System; |
2 | | -using System.IO; |
3 | | -using System.Linq; |
4 | | -using System.Security.Cryptography; |
5 | | -using Renci.SshNet; |
6 | | - |
7 | | -using Serilog; |
8 | | - |
9 | | -namespace Uploader |
10 | | -{ |
11 | | - static class Program |
12 | | - { |
13 | | - static int Main(string host, string name, string password) |
14 | | - { |
15 | | - Log.Logger = new LoggerConfiguration() |
16 | | - .Enrich.FromLogContext() |
17 | | - .WriteTo.Console() |
18 | | - .CreateLogger(); |
19 | | - |
20 | | - using var scpClient = new ScpClient(host, port: 20002, name, password); |
21 | | - scpClient.Connect(); // 与下载服务器建立连接 |
22 | | - |
23 | | - // 确认连接状态 |
24 | | - if (scpClient.IsConnected) |
25 | | - { |
26 | | - Log.Information("SCP服务器连接成功"); |
27 | | - } |
28 | | - else |
29 | | - { |
30 | | - Log.Error("SCP服务器连接失败"); |
31 | | - return -1; |
32 | | - } |
33 | | - |
34 | | - |
35 | | - // 获取可用的资源包,准备上传 |
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Security.Cryptography; |
| 5 | +using Renci.SshNet; |
| 6 | + |
| 7 | +using Serilog; |
| 8 | + |
| 9 | +namespace Uploader |
| 10 | +{ |
| 11 | + static class Program |
| 12 | + { |
| 13 | + static int Main(string host, string name, string password) |
| 14 | + { |
| 15 | + Log.Logger = new LoggerConfiguration() |
| 16 | + .Enrich.FromLogContext() |
| 17 | + .WriteTo.Console() |
| 18 | + .CreateLogger(); |
| 19 | + |
| 20 | + using var scpClient = new ScpClient(host, port: 22, name, password); |
| 21 | + scpClient.Connect(); // 与下载服务器建立连接 |
| 22 | + |
| 23 | + // 确认连接状态 |
| 24 | + if (scpClient.IsConnected) |
| 25 | + { |
| 26 | + Log.Information("SCP服务器连接成功"); |
| 27 | + } |
| 28 | + else |
| 29 | + { |
| 30 | + Log.Error("SCP服务器连接失败"); |
| 31 | + return -1; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + // 获取可用的资源包,准备上传 |
36 | 36 | var artifactDirectory = new DirectoryInfo(Path.Join(Directory.GetCurrentDirectory(), "artifacts")); |
37 | 37 | var packList = artifactDirectory |
38 | 38 | .EnumerateFiles("Minecraft-Mod-Language-Package-*.zip", SearchOption.AllDirectories); |
39 | | - |
40 | | - Log.Information("检测到的资源包数目:{0}", packList.Count()); |
41 | | - |
42 | | - packList.ToList() |
43 | | - .ForEach(_ => |
44 | | - { |
45 | | - using var stream = _.OpenRead(); |
46 | | - var md5 = stream.ComputeMD5(); |
47 | | - |
48 | | - // 文件名格式:Minecraft-Mod-Language-Modpack-[dashed-version]-[md5-hash].zip |
49 | | - // 如:Minecraft-Mod-Language-Modpack-1-16-Fabric-0000000000000000.zip |
50 | | - // hash的对象是文件内容,不包括文件名(当然) |
51 | | - // hash应该是全大写 |
52 | | - |
53 | | - var fileExtensionName = _.Extension; // 带点名称,应当为 ".zip" |
54 | | - var fileName = _.Name[0..^fileExtensionName.Length] |
55 | | - .RegulateFileName(); // 无后缀的文件名,应当已修正 |
56 | | - |
57 | | - // 选择性地加上该文件的md5值,以便生成patch |
58 | | - var tweakedName = fileName + "-" + md5; |
59 | | - |
60 | | - var destinationName = $"/var/www/html/files/{fileName + fileExtensionName}"; |
61 | | - var tweakedDestinationName = $"/var/www/html/files/{tweakedName + fileExtensionName}"; |
62 | | - |
63 | | - // 传递不带md5值的最新版本;会覆写已有文件 |
64 | | - scpClient.Upload(_.OpenRead(), destinationName); |
65 | | - Log.Information("向远程服务器写入文件:{0}", destinationName); |
66 | | - |
67 | | - //// 传递带md5值的历史版本,一般不会覆写已有文件 |
68 | | - //scpClient.Upload(_.OpenRead(), tweakedDestinationName); |
69 | | - //Log.Information("向远程服务器写入文件:{0}", tweakedDestinationName); |
70 | | - }); |
71 | | - |
72 | | - // 临时操作 在使用旧md5校验的程序弃用以后需要删除 |
| 39 | + |
| 40 | + Log.Information("检测到的资源包数目:{0}", packList.Count()); |
| 41 | + |
| 42 | + packList.ToList() |
| 43 | + .ForEach(_ => |
| 44 | + { |
| 45 | + using var stream = _.OpenRead(); |
| 46 | + var md5 = stream.ComputeMD5(); |
| 47 | + |
| 48 | + // 文件名格式:Minecraft-Mod-Language-Modpack-[dashed-version]-[md5-hash].zip |
| 49 | + // 如:Minecraft-Mod-Language-Modpack-1-16-Fabric-0000000000000000.zip |
| 50 | + // hash的对象是文件内容,不包括文件名(当然) |
| 51 | + // hash应该是全大写 |
| 52 | + |
| 53 | + var fileExtensionName = _.Extension; // 带点名称,应当为 ".zip" |
| 54 | + var fileName = _.Name[0..^fileExtensionName.Length] |
| 55 | + .RegulateFileName(); // 无后缀的文件名,应当已修正 |
| 56 | + |
| 57 | + // 选择性地加上该文件的md5值,以便生成patch |
| 58 | + var tweakedName = fileName + "-" + md5; |
| 59 | + |
| 60 | + var destinationName = $"/var/www/html/files/{fileName + fileExtensionName}"; |
| 61 | + var tweakedDestinationName = $"/var/www/html/files/{tweakedName + fileExtensionName}"; |
| 62 | + |
| 63 | + // 传递不带md5值的最新版本;会覆写已有文件 |
| 64 | + scpClient.Upload(_.OpenRead(), destinationName); |
| 65 | + Log.Information("向远程服务器写入文件:{0}", destinationName); |
| 66 | + |
| 67 | + //// 传递带md5值的历史版本,一般不会覆写已有文件 |
| 68 | + //scpClient.Upload(_.OpenRead(), tweakedDestinationName); |
| 69 | + //Log.Information("向远程服务器写入文件:{0}", tweakedDestinationName); |
| 70 | + }); |
| 71 | + |
| 72 | + // 临时操作 在使用旧md5校验的程序弃用以后需要删除 |
73 | 73 | var md5List = artifactDirectory |
74 | 74 | .EnumerateFiles("*.md5", SearchOption.AllDirectories); |
75 | | - md5List.ToList() |
76 | | - .ForEach(_ => |
77 | | - { |
78 | | - scpClient.Upload(_.OpenRead(), $"/var/www/html/files/{_.Name}"); |
79 | | - Log.Information("向远程服务器写入文件:{0}", $"/var/www/html/files/{_.Name}"); |
80 | | - }); |
81 | | - |
82 | | - Log.Information("资源包传递完毕"); |
83 | | - return 0; |
84 | | - } |
85 | | - |
86 | | - public static string RegulateFileName(this string fileName) |
87 | | - { |
88 | | - // 历史遗留问题:全部单词都要大小写 |
89 | | - return CapitalizeGroup(fileName.Replace("Package", "Modpack") // 历史遗留问题:文件名 |
90 | | - .Replace('.', '-') // 历史遗留问题:版本号需要输杠 |
91 | | - .Replace("-1-12-2", "") // 历史遗留问题:1.12.2文件没有版本号 |
92 | | - .Split('-')); |
93 | | - |
94 | | - // 将一组字符串的各项大小写,用'-'连接 |
95 | | - string CapitalizeGroup(string[] texts) => string.Join('-', texts.Select(_ => Capitalize(_))); |
96 | | - |
97 | | - // 将一段文本的首字母大写,其余不动 |
98 | | - string Capitalize(string text) => string.Join("", |
99 | | - text[0..1].ToUpper(), |
100 | | - text[1..]); |
101 | | - } |
102 | | - |
103 | | - /// <summary> |
104 | | - /// 计算给定流中全体内容的MD5值。 |
105 | | - /// </summary> |
106 | | - /// <param name="stream">被计算的流</param> |
107 | | - /// <returns></returns> |
108 | | - public static string ComputeMD5(this Stream stream) |
109 | | - { |
110 | | - stream.Seek(0, SeekOrigin.Begin); // 确保文件流的位置被重置 |
111 | | - return Convert.ToHexString(MD5.Create().ComputeHash(stream)); |
112 | | - } |
113 | | - } |
114 | | -} |
| 75 | + md5List.ToList() |
| 76 | + .ForEach(_ => |
| 77 | + { |
| 78 | + scpClient.Upload(_.OpenRead(), $"/var/www/html/files/{_.Name}"); |
| 79 | + Log.Information("向远程服务器写入文件:{0}", $"/var/www/html/files/{_.Name}"); |
| 80 | + }); |
| 81 | + |
| 82 | + Log.Information("资源包传递完毕"); |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + public static string RegulateFileName(this string fileName) |
| 87 | + { |
| 88 | + // 历史遗留问题:全部单词都要大小写 |
| 89 | + return CapitalizeGroup(fileName.Replace("Package", "Modpack") // 历史遗留问题:文件名 |
| 90 | + .Replace('.', '-') // 历史遗留问题:版本号需要输杠 |
| 91 | + .Replace("-1-12-2", "") // 历史遗留问题:1.12.2文件没有版本号 |
| 92 | + .Split('-')); |
| 93 | + |
| 94 | + // 将一组字符串的各项大小写,用'-'连接 |
| 95 | + string CapitalizeGroup(string[] texts) => string.Join('-', texts.Select(_ => Capitalize(_))); |
| 96 | + |
| 97 | + // 将一段文本的首字母大写,其余不动 |
| 98 | + string Capitalize(string text) => string.Join("", |
| 99 | + text[0..1].ToUpper(), |
| 100 | + text[1..]); |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// 计算给定流中全体内容的MD5值。 |
| 105 | + /// </summary> |
| 106 | + /// <param name="stream">被计算的流</param> |
| 107 | + /// <returns></returns> |
| 108 | + public static string ComputeMD5(this Stream stream) |
| 109 | + { |
| 110 | + stream.Seek(0, SeekOrigin.Begin); // 确保文件流的位置被重置 |
| 111 | + return Convert.ToHexString(MD5.Create().ComputeHash(stream)); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments