1
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user