forked from ddrilling/AsbCloudServer
CS2-147 При отсутствии файла выдавать 404
This commit is contained in:
parent
696dbee375
commit
4d89f3785b
@ -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;
|
||||||
|
@ -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>
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user