134 lines
3.2 KiB
C#
134 lines
3.2 KiB
C#
using System.ComponentModel;
|
|
using System.IO.Compression;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace AutoUpdateServer.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),
|
|
"SSP",
|
|
"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);
|
|
}
|
|
}
|
|
}
|