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); } }