From 4d89f3785b44b5574af7b65569f55428dd3b9ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Tue, 8 Feb 2022 10:25:05 +0500 Subject: [PATCH] =?UTF-8?q?CS2-147=20=D0=9F=D1=80=D0=B8=20=D0=BE=D1=82?= =?UTF-8?q?=D1=81=D1=83=D1=82=D1=81=D1=82=D0=B2=D0=B8=D0=B8=20=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=B0=20=D0=B2=D1=8B=D0=B4=D0=B0=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20404?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/FileService.cs | 13 +++++++++- .../Controllers/DrillingProgramController.cs | 24 +++++++++++-------- AsbCloudWebApi/Controllers/FileController.cs | 23 ++++++++---------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs index 06bf4708..2865259a 100644 --- a/AsbCloudInfrastructure/Services/FileService.cs +++ b/AsbCloudInfrastructure/Services/FileService.cs @@ -216,7 +216,18 @@ namespace AsbCloudInfrastructure.Services .ConfigureAwait(false); if (entity is null) - return null; + { + throw new FileNotFoundException($"fileId:{idFile} not found"); + } + + var ext = Path.GetExtension(entity.Name); + + var relativePath = GetUrl(entity.IdWell, entity.IdCategory, entity.Id, ext); + var fullPath = Path.GetFullPath(relativePath); + if (! File.Exists(fullPath)) + { + throw new FileNotFoundException("not found", relativePath); + } var dto = Convert(entity); return dto; diff --git a/AsbCloudWebApi/Controllers/DrillingProgramController.cs b/AsbCloudWebApi/Controllers/DrillingProgramController.cs index d79aa198..bede3820 100644 --- a/AsbCloudWebApi/Controllers/DrillingProgramController.cs +++ b/AsbCloudWebApi/Controllers/DrillingProgramController.cs @@ -44,17 +44,21 @@ namespace AsbCloudWebApi.Controllers !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false)) return Forbid(); - - var fileInfo = await drillingProgramService.GetOrCreateAsync(idWell, - (int)fileChangerId, token) - .ConfigureAwait(false); - - if (fileInfo is null) - return NoContent(); - - var relativePath = fileService.GetUrl(fileInfo); - return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name); + try + { + var fileInfo = await drillingProgramService.GetOrCreateAsync(idWell, + (int)fileChangerId, token) + .ConfigureAwait(false); + + var relativePath = fileService.GetUrl(fileInfo); + + return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name); + } + catch (FileNotFoundException ex) + { + return NotFound(ex.FileName); + } } /// diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs index f4ce6b09..21a11fce 100644 --- a/AsbCloudWebApi/Controllers/FileController.cs +++ b/AsbCloudWebApi/Controllers/FileController.cs @@ -123,28 +123,25 @@ namespace AsbCloudWebApi.Controllers public async Task GetFileAsync([FromRoute] int idWell, int fileId, CancellationToken token = default) { + int? idCompany = User.GetCompanyId(); + + if (idCompany is null) + return Forbid(); + + if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, + idWell, token).ConfigureAwait(false)) + return Forbid(); + try { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null) - return Forbid(); - - if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false)) - return Forbid(); - var fileInfo = await fileService.GetInfoAsync(fileId, token); - if (fileInfo is null) - throw new FileNotFoundException(); - var relativePath = fileService.GetUrl(fileInfo); return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name); } catch (FileNotFoundException ex) { - return NotFound($"Файл не найден. Текст ошибки: {ex.Message}"); + return NotFound(ex.FileName); } }