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; /// /// Реализация сервиса справок страниц /// public class HelpPageService : IHelpPageService { private readonly string directoryNameHelpPageFiles; private readonly IHelpPageRepository helpPageRepository; private readonly IFileStorageRepository fileStorageRepository; /// /// Конструктор класса /// /// /// /// 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; } /// /// Создание справки страницы /// /// /// /// /// /// /// public async Task 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; } /// /// Обновление справки страницы /// /// /// /// /// /// /// 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); } /// /// Получение справки по url страницы и id категории /// /// /// /// /// public Task GetOrDefaultByUrlPageAndIdCategoryAsync(string urlPage, int idCategory, CancellationToken cancellationToken) => helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(urlPage, idCategory, cancellationToken); public Task GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken) => helpPageRepository.GetOrDefaultAsync(id, cancellationToken); /// /// Получение файлового потока для файла справки /// /// /// /// 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); } }