CS2-147 При отсутствии файла выдавать 404

This commit is contained in:
Фролов 2022-02-08 10:25:05 +05:00
parent 696dbee375
commit 4d89f3785b
3 changed files with 36 additions and 24 deletions

View File

@ -216,7 +216,18 @@ namespace AsbCloudInfrastructure.Services
.ConfigureAwait(false); .ConfigureAwait(false);
if (entity is null) 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); var dto = Convert(entity);
return dto; return dto;

View File

@ -44,17 +44,21 @@ namespace AsbCloudWebApi.Controllers
!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false)) idWell, token).ConfigureAwait(false))
return Forbid(); 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);
}
} }
/// <summary> /// <summary>

View File

@ -123,28 +123,25 @@ namespace AsbCloudWebApi.Controllers
public async Task<IActionResult> GetFileAsync([FromRoute] int idWell, public async Task<IActionResult> GetFileAsync([FromRoute] int idWell,
int fileId, CancellationToken token = default) 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 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); var fileInfo = await fileService.GetInfoAsync(fileId, token);
if (fileInfo is null)
throw new FileNotFoundException();
var relativePath = fileService.GetUrl(fileInfo); var relativePath = fileService.GetUrl(fileInfo);
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name); return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name);
} }
catch (FileNotFoundException ex) catch (FileNotFoundException ex)
{ {
return NotFound($"Файл не найден. Текст ошибки: {ex.Message}"); return NotFound(ex.FileName);
} }
} }