This commit is contained in:
2025-05-09 17:36:55 +08:00
parent 7416a2dd12
commit 25b958046d
12 changed files with 403 additions and 91 deletions

View File

@@ -30,6 +30,8 @@
DirectoryInfo dir = new DirectoryInfo(directory);
var jsonPath = Path.Combine(directory, "config.json");
if(!File.Exists(jsonPath)) continue;
var json = File.ReadAllText(jsonPath);
var theme = JsonConvert.DeserializeObject<ThemeModel>(json);

View File

@@ -0,0 +1,91 @@

namespace Yes.Application.Admins.Themes
{
public record UploadThemeCommand(IFormFile File) : IRequest<UploadThemeCommandResponse>;
public record UploadThemeCommandResponse(string DirName);
public class UploadThemeCommandHandler(IWebHostEnvironment env,
IOptionsMonitor<BlogSettings> options) : IRequestHandler<UploadThemeCommand, UploadThemeCommandResponse>
{
private readonly IWebHostEnvironment _env = env;
private readonly BlogSettings _settings = options.CurrentValue;
public async Task<UploadThemeCommandResponse> Handle(UploadThemeCommand request, CancellationToken cancellationToken)
{
try
{
var file = request.File;
var ext = Path.GetExtension(file.FileName).ToLowerInvariant();
var allowedExts = _settings.AllowedUploadExts;
if (!ext.Equals(".zip"))
{
throw new FileTypeException(".zip");
}
if (file.Length > _settings.UploadSizeLimit)
{
throw new FileOverflowException($"{_settings.UploadSizeLimit / 1024 / 1024}M");
}
var baseUploadPath = "uploads";
var datePath = DateTime.Now.ToString("yyyyMM");
var uploads = Path.Combine(_env.ContentRootPath, "files", baseUploadPath, datePath);
if (!Directory.Exists(uploads))
{
Directory.CreateDirectory(uploads);
}
var hashKey = "";
using (var memoryStream = new MemoryStream())
{
await file.CopyToAsync(memoryStream);
memoryStream.Position = 0;
using (var cryptoProvider = SHA256.Create())
{
byte[] hashBytes = cryptoProvider.ComputeHash(memoryStream);
hashKey = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
}
}
var newFileName = $"{hashKey}{ext}";
var filePath = Path.Combine(uploads, newFileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
var newDirName = Path.GetFileName(file.FileName).Replace(".zip", "");
var targetDirectory = Path.Combine(_env.ContentRootPath, "files", "themes");
ZipHelper.ExtractToDirectory(filePath, targetDirectory, newDirName);
string targetFilePath = Path.Combine(targetDirectory, newDirName, "config.json");
if (!File.Exists(targetFilePath))
{
throw new InvalidDataException("缺少config.json文件");
}
return new UploadThemeCommandResponse(newDirName);
}
catch (Exception ex) when (ex is OverflowException)
{
throw new FileOverflowException();
}
catch (Exception ex) when (ex is InvalidDataException)
{
throw new FileNotSupportedException(ex.Message);
}
}
}
}

View File

@@ -31,4 +31,5 @@ global using Yes.Infrastructure.Data.DbContexts;
global using Yes.Infrastructure.Authorizations.Identity.Context;
global using Yes.Infrastructure.Authorizations.Identity;
global using Microsoft.Data.SqlClient;
global using Yes.Infrastructure.Migrator.Providers;
global using Yes.Infrastructure.Migrator.Providers;
global using Yes.Infrastructure.Helpers;