1
This commit is contained in:
14
AutoUpdate/AutoUpdateServer/AutoUpdateServer.csproj
Normal file
14
AutoUpdate/AutoUpdateServer/AutoUpdateServer.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0-beta1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
6
AutoUpdate/AutoUpdateServer/AutoUpdateServer.http
Normal file
6
AutoUpdate/AutoUpdateServer/AutoUpdateServer.http
Normal file
@@ -0,0 +1,6 @@
|
||||
@AutoUpdateServer_HostAddress = http://localhost:5062
|
||||
|
||||
GET {{AutoUpdateServer_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
133
AutoUpdate/AutoUpdateServer/Controllers/自动更新Controller.cs
Normal file
133
AutoUpdate/AutoUpdateServer/Controllers/自动更新Controller.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using System.ComponentModel;
|
||||
using System.IO.Compression;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Lmes服务端.Controllers
|
||||
{
|
||||
// 版本信息模型
|
||||
public class VersionInfo
|
||||
{
|
||||
public string Version { get; set; }
|
||||
public string CRC { get; set; }
|
||||
public string FileName { get; set; }
|
||||
}
|
||||
[EnableCors("AllowAll")]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class 自动更新Controller : ControllerBase
|
||||
{
|
||||
private string 存储路径 { get; set; }
|
||||
|
||||
public 自动更新Controller()
|
||||
{
|
||||
// 设置版本文件存储路径
|
||||
存储路径 = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||||
"Lmes_1.0",
|
||||
"VersionInfo"
|
||||
);
|
||||
|
||||
// 确保目录存在
|
||||
if (!Directory.Exists(存储路径))
|
||||
{
|
||||
Directory.CreateDirectory(存储路径);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取最新版本信息
|
||||
[HttpGet("获取最新版本信息")]
|
||||
public ActionResult<VersionInfo> GetLatestVersion()
|
||||
{
|
||||
try
|
||||
{
|
||||
var files = Directory.GetFiles(存储路径, "*.zip");
|
||||
if (!files.Any())
|
||||
{
|
||||
return NotFound("未找到任何版本文件");
|
||||
}
|
||||
|
||||
// 获取最新的版本文件
|
||||
var latestFile = files.OrderByDescending(f => f).First();
|
||||
var fileName = Path.GetFileNameWithoutExtension(latestFile);
|
||||
|
||||
// 计算文件的CRC值
|
||||
string crc;
|
||||
using (var fs = System.IO.File.OpenRead(latestFile))
|
||||
{
|
||||
using var sha = System.Security.Cryptography.SHA256.Create();
|
||||
var hash = sha.ComputeHash(fs);
|
||||
crc = BitConverter.ToString(hash).Replace("-", "");
|
||||
}
|
||||
|
||||
return new VersionInfo
|
||||
{
|
||||
Version = fileName.Split("--")[0],
|
||||
CRC = crc,
|
||||
FileName = Path.GetFileName(latestFile)
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, $"获取版本信息失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 上传新版本
|
||||
[HttpPost("上传新版本")]
|
||||
public async Task<IActionResult> UploadVersion(IFormFile file)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileName = file.FileName;
|
||||
var filePath = Path.Combine(存储路径, fileName);
|
||||
|
||||
using (var stream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
|
||||
// 验证上传的文件是否为有效的ZIP文件
|
||||
try
|
||||
{
|
||||
using (var archive = ZipFile.OpenRead(filePath))
|
||||
{
|
||||
// 检查是否包含必要的文件
|
||||
if (!archive.Entries.Any(e => e.Name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
System.IO.File.Delete(filePath);
|
||||
return BadRequest("ZIP文件中未找到可执行文件");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
System.IO.File.Delete(filePath);
|
||||
return BadRequest("上传的文件不是有效的ZIP文件");
|
||||
}
|
||||
|
||||
return Ok("文件上传成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, $"文件上传失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 下载指定版本
|
||||
[HttpGet("下载指定版本/{fileName}")]
|
||||
public IActionResult DownloadVersion(string fileName)
|
||||
{
|
||||
var filePath = Path.Combine(存储路径, fileName);
|
||||
|
||||
if (!System.IO.File.Exists(filePath))
|
||||
{
|
||||
return NotFound("指定的版本文件不存在");
|
||||
}
|
||||
|
||||
var fileStream = System.IO.File.OpenRead(filePath);
|
||||
return File(fileStream, "application/zip", fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
53
AutoUpdate/AutoUpdateServer/Program.cs
Normal file
53
AutoUpdate/AutoUpdateServer/Program.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
|
||||
namespace AutoUpdateServer
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowAll", builder =>
|
||||
{
|
||||
builder.AllowAnyOrigin()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
});
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddLogging();
|
||||
builder.Services.Configure<KestrelServerOptions>(options =>
|
||||
{
|
||||
options.Limits.MaxRequestBodySize = 1024 * 1024 * 1024; // 100 MB
|
||||
});
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseCors("AllowAll");
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
25
AutoUpdate/AutoUpdateServer/Properties/launchSettings.json
Normal file
25
AutoUpdate/AutoUpdateServer/Properties/launchSettings.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5062",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7116;http://localhost:5062",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
AutoUpdate/AutoUpdateServer/appsettings.Development.json
Normal file
15
AutoUpdate/AutoUpdateServer/appsettings.Development.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
"Url": "http://*:5000"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
AutoUpdate/AutoUpdateServer/appsettings.json
Normal file
9
AutoUpdate/AutoUpdateServer/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user