DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/FileController.cs

242 lines
8.5 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Repositories;
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi.Controllers;
/// <summary>
/// Хранение файлов
/// </summary>
[Route("api/well/{idWell}/files")]
[ApiController]
[Authorize]
public class FileController : ControllerBase
{
2024-08-19 10:01:07 +05:00
private readonly FileService fileService;
private readonly IWellService wellService;
public FileController(FileService fileService, IWellService wellService)
{
this.fileService = fileService;
this.wellService = wellService;
}
2023-05-19 16:51:41 +05:00
2022-06-16 17:37:10 +05:00
/// <summary>
2024-08-19 10:01:07 +05:00
/// Сохраняет переданные файлы и информацию о них
2022-06-16 17:37:10 +05:00
/// </summary>
2024-08-19 10:01:07 +05:00
/// <param name="idWell">id скважины</param>
/// <param name="idCategory">id категории файла</param>
/// <param name="files">Коллекция файлов</param>
/// <param name="userRepository">dependency</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns></returns>
[HttpPost]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> SaveFilesAsync(int idWell, int idCategory,
[FromForm] IFormFileCollection files, [FromServices] IUserRepository userRepository, CancellationToken token)
{
2024-08-19 10:01:07 +05:00
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
2024-08-19 10:01:07 +05:00
if (idCompany is null || idUser is null)
return Forbid();
2024-08-19 10:01:07 +05:00
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
if (!userRepository.HasPermission((int)idUser, $"File.edit{idCategory}"))
return Forbid();
2024-08-19 10:01:07 +05:00
foreach (var file in files)
{
var fileStream = file.OpenReadStream();
await fileService.SaveAsync(idWell, idUser ?? 0, idCategory, file.FileName,
fileStream, token).ConfigureAwait(false);
}
return Ok();
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Возвращает информацию о файлах для скважины в выбраной категории
/// </summary>
/// <param name="request"> </param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Список информации о файлах в этой категории</returns>
[HttpGet("/api/files")]
[Permission]
[ProducesResponseType(typeof(PaginationContainer<FileInfoDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFilesInfoAsync(
[FromQuery] FileRequest request,
CancellationToken token)
{
int? idCompany = User.GetCompanyId();
2022-02-03 08:23:52 +05:00
2024-08-19 10:01:07 +05:00
if (request.IdCategory is null || idCompany is null)
return Forbid();
2024-08-19 10:01:07 +05:00
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value,
request.IdWell, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
var filesInfo = await fileService.GetInfosPaginatedAsync(request, token).ConfigureAwait(false);
2024-08-19 10:01:07 +05:00
return Ok(filesInfo);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Возвращает файл с диска на сервере
/// </summary>
/// <param name="idFile">id запрашиваемого файла</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Запрашиваемый файл</returns>
[HttpGet("{idFile}")]
[Permission]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFileAsync(
int idFile, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
2024-08-19 10:01:07 +05:00
if (idCompany is null)
return Forbid();
2024-08-19 10:01:07 +05:00
var fileInfo = await fileService.GetOrDefaultAsync(idFile, token);
2024-08-19 10:01:07 +05:00
if (fileInfo is null)
return NotFound(idFile);
2024-08-19 10:01:07 +05:00
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
fileInfo.IdWell, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
var fileStream = fileService.GetFileStream(fileInfo);
2022-10-17 14:42:47 +05:00
2024-08-19 10:01:07 +05:00
return File(fileStream, "application/octet-stream", fileInfo.Name);
}
2022-10-17 14:42:47 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// Помечает файл как удаленный
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="idFile">id запрашиваемого файла</param>
/// <param name="userRepository">dependency</param>
/// <param name="token">Токен отмены задачи </param>
/// <returns></returns>
[HttpDelete("{idFile}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> DeleteAsync(int idWell, int idFile,
[FromServices] IUserRepository userRepository,
CancellationToken token)
{
int? idUser = User.GetUserId();
2024-08-19 10:01:07 +05:00
int? idCompany = User.GetCompanyId();
2022-10-11 14:42:04 +05:00
2024-08-19 10:01:07 +05:00
if (idUser is null || idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
var fileInfo = await fileService.GetOrDefaultAsync(idFile, token);
2022-02-03 08:23:52 +05:00
2024-08-19 10:01:07 +05:00
if (fileInfo is null)
return NotFound(idFile);
2024-08-19 10:01:07 +05:00
if (!userRepository.HasPermission((int)idUser, $"File.edit{fileInfo?.IdCategory}"))
return Forbid();
2024-08-19 10:01:07 +05:00
var result = await fileService.MarkAsDeletedAsync(idFile, token);
2022-10-17 14:42:47 +05:00
2024-08-19 10:01:07 +05:00
return Ok(result);
}
2022-02-03 08:23:52 +05:00
2024-08-19 10:01:07 +05:00
/// <summary>
/// Создает метку для файла
/// </summary>
/// <param name="idWell">id скважины </param>
/// <param name="markDto">метка файла</param>
/// <param name="token">Токен отмены задачи </param>
/// <returns></returns>
[HttpPost("fileMark")]
[Permission]
public async Task<IActionResult> CreateFileMarkAsync(int idWell, FileMarkDto markDto, CancellationToken token)
{
var idCompany = User.GetCompanyId();
2022-02-03 08:23:52 +05:00
2024-08-19 10:01:07 +05:00
var idUser = User.GetUserId();
2024-08-19 10:01:07 +05:00
if (idCompany is null || idUser is null ||
!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
var result = await fileService.CreateFileMarkAsync(markDto, (int)idUser, token)
.ConfigureAwait(false);
2024-08-19 10:01:07 +05:00
return Ok(result);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Помечает метку у файла как удаленную
/// </summary>
/// <param name="idWell">id скважины </param>
/// <param name="idMark">id метки </param>
/// <param name="token">Токен отмены задачи </param>
/// <returns></returns>
[HttpDelete("fileMark")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> DeleteFileMarkAsync(int idWell, int idMark,
CancellationToken token)
{
var idCompany = User.GetCompanyId();
2024-08-19 10:01:07 +05:00
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
var result = await fileService.MarkFileMarkAsDeletedAsync(idMark, token)
.ConfigureAwait(false);
2024-08-19 10:01:07 +05:00
return Ok(result);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Возвращает информацию о файле
/// </summary>
/// <param name="idFile">id запрашиваемого файла</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Запрашиваемый файл</returns>
[HttpGet("/api/files/{idFile}")]
[Permission]
[ProducesResponseType(typeof(FileInfoDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFileInfoAsync([FromRoute] int idFile, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
2024-08-19 10:01:07 +05:00
if (idCompany is null)
return Forbid();
2024-08-19 10:01:07 +05:00
try
{
var fileInfo = await fileService.GetOrDefaultAsync(idFile, token).ConfigureAwait(false);
return Ok(fileInfo);
}
2024-08-19 10:01:07 +05:00
catch (FileNotFoundException ex)
{
2024-08-19 10:01:07 +05:00
return NotFound(ex.FileName);
}
}
}