HelpPageService.GetFileStreamAsync удалил ArgumentInvalidException. Заменил возвращаемый тип на Nullable.

This commit is contained in:
ngfrolov 2023-07-26 17:53:25 +05:00
parent 963da559e2
commit 84d84fb504
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
3 changed files with 14 additions and 15 deletions

View File

@ -27,11 +27,11 @@ public interface IHelpPageService
/// <summary> /// <summary>
/// Метод получения файла справки /// Метод получения файла справки
/// </summary> /// </summary>
/// <param name="urlPage"></param> /// <param name="pageKey"></param>
/// <param name="idCategory"></param> /// <param name="idCategory"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
Task<(Stream stream, string fileName)> GetFileStreamAsync(string urlPage, Task<(Stream stream, string fileName)?> GetFileStreamAsync(string pageKey,
int idCategory, int idCategory,
CancellationToken cancellationToken); CancellationToken cancellationToken);
} }

View File

@ -78,20 +78,23 @@ public class HelpPageService : IHelpPageService
/// <summary> /// <summary>
/// Метод получения файла справки /// Метод получения файла справки
/// </summary> /// </summary>
/// <param name="urlPage"></param> /// <param name="pageKey"></param>
/// <param name="idCategory"></param> /// <param name="idCategory"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="ArgumentInvalidException"></exception> /// <exception cref="NotFoundException"></exception>
public async Task<(Stream stream, string fileName)> GetFileStreamAsync(string urlPage, public async Task<(Stream stream, string fileName)?> GetFileStreamAsync(string pageKey,
int idCategory, int idCategory,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
urlPage = WebUtility.UrlDecode(urlPage); pageKey = WebUtility.UrlDecode(pageKey);
var helpPage = await helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(urlPage, var helpPage = await helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(pageKey,
idCategory, idCategory,
cancellationToken) ?? throw new ArgumentInvalidException("Справки не существует", nameof(idCategory)); cancellationToken);
if(helpPage is null)
return null;
string filePath = fileStorageRepository.GetFilePath(directoryNameHelpPageFiles, string filePath = fileStorageRepository.GetFilePath(directoryNameHelpPageFiles,
helpPage.IdCategory.ToString(), helpPage.IdCategory.ToString(),

View File

@ -89,14 +89,10 @@ public class HelpPageController : ControllerBase
idCategory, idCategory,
cancellationToken); cancellationToken);
using var fileStream = file.stream; if (!file.HasValue)
return NotFound();
var memoryStream = new MemoryStream(); return File(file.Value.stream, "application/pdf", file.Value.fileName);
await fileStream.CopyToAsync(memoryStream,
cancellationToken);
memoryStream.Position = 0;
return File(memoryStream, "application/pdf", file.fileName);
} }
/// <summary> /// <summary>