1
This commit is contained in:
59
Yes.Application/Admins/Themes/DeleteThemeCommandHandler.cs
Normal file
59
Yes.Application/Admins/Themes/DeleteThemeCommandHandler.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
namespace Yes.Application.Admins.Themes
|
||||
{
|
||||
public record DeleteThemeCommand(string ThemeName) : IRequest<DeleteThemeCommandResponse>;
|
||||
|
||||
public record DeleteThemeCommandResponse();
|
||||
public class DeleteThemeCommandHandler(
|
||||
IWebHostEnvironment env,
|
||||
BlogDbContext db,
|
||||
IIdentityContext identity,
|
||||
IOptionsMonitor<BlogSettings> options) : IRequestHandler<DeleteThemeCommand, DeleteThemeCommandResponse>
|
||||
{
|
||||
private readonly IWebHostEnvironment _env = env;
|
||||
private readonly BlogSettings _settings = options.CurrentValue;
|
||||
private readonly BlogDbContext _db = db;
|
||||
private readonly IIdentityContext _identity = identity;
|
||||
|
||||
public async Task<DeleteThemeCommandResponse> Handle(DeleteThemeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var identityUser = await _db.Users.FindAsync(_identity.Id);
|
||||
if (identityUser == null)
|
||||
{
|
||||
throw new UserNotExistsException(_identity.Id);
|
||||
}
|
||||
|
||||
if (!identityUser.IsSystemUser())
|
||||
{
|
||||
throw new AccessDeniedException();
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var themeName = request.ThemeName;
|
||||
|
||||
if (_settings.Theme == themeName)
|
||||
{
|
||||
throw new ThemeInUseException();
|
||||
}
|
||||
|
||||
|
||||
var targetDirectory = Path.Combine(_env.ContentRootPath, "files", "themes", themeName);
|
||||
|
||||
|
||||
if (Directory.Exists(targetDirectory))
|
||||
Directory.Delete(targetDirectory, recursive: true);
|
||||
|
||||
return new DeleteThemeCommandResponse();
|
||||
}
|
||||
catch (Exception ex) when (ex is OverflowException)
|
||||
{
|
||||
throw new FileOverflowException();
|
||||
}
|
||||
catch (Exception ex) when (ex is InvalidDataException)
|
||||
{
|
||||
throw new FileNotSupportedException(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
54
Yes.Application/Admins/Themes/DownloadThemeCommandHandler.cs
Normal file
54
Yes.Application/Admins/Themes/DownloadThemeCommandHandler.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
namespace Yes.Application.Admins.Themes
|
||||
{
|
||||
public record DownloadThemeCommand(string ThemeName) : IRequest<DownloadThemeCommandResponse>;
|
||||
|
||||
public record DownloadThemeCommandResponse(string Filepath);
|
||||
public class DownloadThemeCommandHandler(
|
||||
IWebHostEnvironment env,
|
||||
BlogDbContext db,
|
||||
IIdentityContext identity,
|
||||
IOptionsMonitor<BlogSettings> options) : IRequestHandler<DownloadThemeCommand, DownloadThemeCommandResponse>
|
||||
{
|
||||
private readonly IWebHostEnvironment _env = env;
|
||||
private readonly BlogSettings _settings = options.CurrentValue;
|
||||
private readonly BlogDbContext _db = db;
|
||||
private readonly IIdentityContext _identity = identity;
|
||||
|
||||
public async Task<DownloadThemeCommandResponse> Handle(DownloadThemeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var identityUser = await _db.Users.FindAsync(_identity.Id);
|
||||
if (identityUser == null)
|
||||
{
|
||||
throw new UserNotExistsException(_identity.Id);
|
||||
}
|
||||
|
||||
if (!identityUser.IsSystemUser())
|
||||
{
|
||||
throw new AccessDeniedException();
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var themeName = request.ThemeName;
|
||||
|
||||
|
||||
var targetDirectory = Path.Combine(_env.ContentRootPath, "files", "themes", themeName);
|
||||
var zipPath = Path.Combine(_env.ContentRootPath, "files", "themes", $"{themeName}.zip");
|
||||
|
||||
ZipHelper.CompressFolderToZip(targetDirectory, zipPath);
|
||||
|
||||
|
||||
return new DownloadThemeCommandResponse(zipPath);
|
||||
}
|
||||
catch (Exception ex) when (ex is OverflowException)
|
||||
{
|
||||
throw new FileOverflowException();
|
||||
}
|
||||
catch (Exception ex) when (ex is InvalidDataException)
|
||||
{
|
||||
throw new FileNotSupportedException(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,8 @@
|
||||
string Description,
|
||||
string Homepage,
|
||||
string DirName,
|
||||
bool IsInUse
|
||||
bool IsInUse,
|
||||
DateTime LastWriteTime
|
||||
);
|
||||
|
||||
public class GetThemesQueryHandler(IOptionsMonitor<BlogSettings> options, IMapper mapper, IWebHostEnvironment env) : IRequestHandler<GetThemesQuery, List<GetThemesQueryResponse>>
|
||||
@@ -32,11 +33,14 @@
|
||||
var jsonPath = Path.Combine(directory, "config.json");
|
||||
if(!File.Exists(jsonPath)) continue;
|
||||
|
||||
var lastWriteTime = File.GetCreationTime(jsonPath);
|
||||
|
||||
var json = File.ReadAllText(jsonPath);
|
||||
|
||||
var theme = JsonConvert.DeserializeObject<ThemeModel>(json);
|
||||
|
||||
theme.DirName = dir.Name;
|
||||
theme.LastWriteTime = lastWriteTime;
|
||||
if (theme.DirName == _settings.Theme)
|
||||
{
|
||||
theme.IsInUse = true;
|
||||
@@ -45,7 +49,7 @@
|
||||
themes.Add(_mapper.Map<GetThemesQueryResponse>(theme));
|
||||
}
|
||||
|
||||
themes = themes.OrderByDescending(x=>x.IsInUse).ToList();
|
||||
themes = themes.OrderByDescending(x=>x.IsInUse).ThenBy(x=>x.LastWriteTime).ToList();
|
||||
|
||||
return themes;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Yes.Application.Admins.Themes
|
||||
{
|
||||
public record UploadThemeCommand(IFormFile File) : IRequest<UploadThemeCommandResponse>;
|
||||
public record UploadThemeCommand(IFormFile File, string Theme) : IRequest<UploadThemeCommandResponse>;
|
||||
|
||||
public record UploadThemeCommandResponse(string DirName);
|
||||
public class UploadThemeCommandHandler(
|
||||
@@ -31,6 +31,12 @@
|
||||
try
|
||||
{
|
||||
var file = request.File;
|
||||
var theme = request.Theme;
|
||||
|
||||
if(string.IsNullOrWhiteSpace(theme))
|
||||
{
|
||||
theme = Guid.NewGuid().ToString().ToLower();
|
||||
}
|
||||
|
||||
var ext = Path.GetExtension(file.FileName).ToLowerInvariant();
|
||||
var allowedExts = _settings.AllowedUploadExts;
|
||||
@@ -81,14 +87,7 @@
|
||||
|
||||
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文件");
|
||||
}
|
||||
ZipHelper.ExtractAndProcessZip(filePath, targetDirectory, theme);
|
||||
|
||||
return new UploadThemeCommandResponse(newDirName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user