forked from ddrilling/AsbCloudServer
169 lines
5.7 KiB
C#
169 lines
5.7 KiB
C#
|
using System;
|
|||
|
using AsbCloudApp.Data;
|
|||
|
using AsbCloudApp.Repositories;
|
|||
|
using AsbCloudApp.Services;
|
|||
|
using System.IO;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using AsbCloudApp.Exceptions;
|
|||
|
|
|||
|
namespace AsbCloudInfrastructure.Services;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Реализация сервиса справок страниц
|
|||
|
/// </summary>
|
|||
|
public class HelpPageService : IHelpPageService
|
|||
|
{
|
|||
|
private readonly string directoryNameHelpPageFiles;
|
|||
|
private readonly IHelpPageRepository helpPageRepository;
|
|||
|
private readonly IFileStorageRepository fileStorageRepository;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Конструктор класса
|
|||
|
/// </summary>
|
|||
|
/// <param name="helpPageRepository"></param>
|
|||
|
/// <param name="fileStorageRepository"></param>
|
|||
|
/// <param name="directoryNameHelpPageFiles"></param>
|
|||
|
public HelpPageService(IHelpPageRepository helpPageRepository,
|
|||
|
IFileStorageRepository fileStorageRepository,
|
|||
|
string directoryNameHelpPageFiles)
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(directoryNameHelpPageFiles))
|
|||
|
throw new ArgumentException("Value cannot be null or whitespace", nameof(this.directoryNameHelpPageFiles));
|
|||
|
|
|||
|
this.helpPageRepository = helpPageRepository;
|
|||
|
this.fileStorageRepository = fileStorageRepository;
|
|||
|
this.directoryNameHelpPageFiles = directoryNameHelpPageFiles;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Создание справки страницы
|
|||
|
/// </summary>
|
|||
|
/// <param name="urlPage"></param>
|
|||
|
/// <param name="idCategory"></param>
|
|||
|
/// <param name="fileName"></param>
|
|||
|
/// <param name="fileStream"></param>
|
|||
|
/// <param name="cancellationToken"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task<int> CreateAsync(string urlPage,
|
|||
|
int idCategory,
|
|||
|
string fileName,
|
|||
|
Stream fileStream,
|
|||
|
CancellationToken cancellationToken)
|
|||
|
{
|
|||
|
if (await helpPageRepository.IsCheckHelpPageWithUrlPageAndIdCategoryAsync(urlPage,
|
|||
|
idCategory,
|
|||
|
cancellationToken))
|
|||
|
{
|
|||
|
throw new ArgumentsInvalidException("Справка с такой категории файла для данной страницы уже существует",
|
|||
|
new[] { nameof(urlPage), nameof(idCategory) });
|
|||
|
}
|
|||
|
|
|||
|
HelpPageDto helpPage = new()
|
|||
|
{
|
|||
|
UrlPage = urlPage,
|
|||
|
IdCategory = idCategory,
|
|||
|
Name = Path.GetFileName(fileName),
|
|||
|
Size = fileStream.Length,
|
|||
|
};
|
|||
|
|
|||
|
int idFile = await helpPageRepository.InsertAsync(helpPage,
|
|||
|
cancellationToken);
|
|||
|
|
|||
|
await SaveFileAsync(idCategory,
|
|||
|
fileName,
|
|||
|
fileStream,
|
|||
|
idFile,
|
|||
|
cancellationToken);
|
|||
|
|
|||
|
return idFile;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Обновление справки страницы
|
|||
|
/// </summary>
|
|||
|
/// <param name="helpPage"></param>
|
|||
|
/// <param name="idCategory"></param>
|
|||
|
/// <param name="fileName"></param>
|
|||
|
/// <param name="fileStream"></param>
|
|||
|
/// <param name="cancellationToken"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task UpdateAsync(HelpPageDto helpPage,
|
|||
|
int idCategory,
|
|||
|
string fileName,
|
|||
|
Stream fileStream,
|
|||
|
CancellationToken cancellationToken)
|
|||
|
{
|
|||
|
helpPage.Name = Path.GetFileName(fileName);
|
|||
|
helpPage.IdCategory = idCategory;
|
|||
|
helpPage.Size = fileStream.Length;
|
|||
|
|
|||
|
string fileFullName = fileStorageRepository.GetFilePath(directoryNameHelpPageFiles,
|
|||
|
idCategory.ToString(),
|
|||
|
helpPage.Id,
|
|||
|
Path.GetExtension(helpPage.Name));
|
|||
|
|
|||
|
await helpPageRepository.UpdateAsync(helpPage,
|
|||
|
cancellationToken);
|
|||
|
|
|||
|
fileStorageRepository.DeleteFile(fileFullName);
|
|||
|
|
|||
|
await SaveFileAsync(helpPage.IdCategory,
|
|||
|
fileName,
|
|||
|
fileStream,
|
|||
|
helpPage.Id,
|
|||
|
cancellationToken);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение справки по url страницы и id категории
|
|||
|
/// </summary>
|
|||
|
/// <param name="urlPage"></param>
|
|||
|
/// <param name="idCategory"></param>
|
|||
|
/// <param name="cancellationToken"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public Task<HelpPageDto?> GetOrDefaultByUrlPageAndIdCategoryAsync(string urlPage,
|
|||
|
int idCategory,
|
|||
|
CancellationToken cancellationToken) =>
|
|||
|
helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(urlPage,
|
|||
|
idCategory,
|
|||
|
cancellationToken);
|
|||
|
|
|||
|
public Task<HelpPageDto?> GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken) =>
|
|||
|
helpPageRepository.GetOrDefaultAsync(id,
|
|||
|
cancellationToken);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение файлового потока для файла справки
|
|||
|
/// </summary>
|
|||
|
/// <param name="helpPage"></param>
|
|||
|
/// <param name="cancellationToken"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public Stream GetFileStream(HelpPageDto helpPage)
|
|||
|
{
|
|||
|
string filePath = fileStorageRepository.GetFilePath(directoryNameHelpPageFiles,
|
|||
|
helpPage.IdCategory.ToString(),
|
|||
|
helpPage.Id,
|
|||
|
Path.GetExtension(helpPage.Name));;
|
|||
|
|
|||
|
var fileStream = new FileStream(Path.GetFullPath(filePath), FileMode.Open);
|
|||
|
|
|||
|
return fileStream;
|
|||
|
}
|
|||
|
|
|||
|
private async Task SaveFileAsync(int idCategory,
|
|||
|
string fileName,
|
|||
|
Stream fileStream,
|
|||
|
int fileId,
|
|||
|
CancellationToken cancellationToken = default)
|
|||
|
{
|
|||
|
string filePath = fileStorageRepository.MakeFilePath(directoryNameHelpPageFiles,
|
|||
|
idCategory.ToString(),
|
|||
|
$"{fileId}" + $"{Path.GetExtension(fileName)}");
|
|||
|
|
|||
|
await fileStorageRepository.SaveFileAsync(filePath,
|
|||
|
fileStream,
|
|||
|
cancellationToken);
|
|||
|
}
|
|||
|
}
|