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

244 lines
9.2 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;
namespace AsbCloudWebApi.Controllers
{
2023-05-19 16:51:41 +05:00
2022-06-16 17:37:10 +05:00
/// <summary>
/// Хранение файлов
/// </summary>
2021-08-16 17:34:00 +05:00
[Route("api/well/{idWell}/files")]
[ApiController]
[Authorize]
public class FileController : ControllerBase
{
2022-10-06 14:37:03 +05:00
private readonly FileService fileService;
private readonly IWellService wellService;
2022-10-06 14:37:03 +05:00
public FileController(FileService fileService, IWellService wellService)
{
this.fileService = fileService;
this.wellService = wellService;
}
/// <summary>
/// Сохраняет переданные файлы и информацию о них
/// </summary>
2021-07-27 14:43:30 +05:00
/// <param name="idWell">id скважины</param>
/// <param name="idCategory">id категории файла</param>
/// <param name="files">Коллекция файлов</param>
2022-11-17 17:54:25 +05:00
/// <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)
{
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
if (idCompany is null || idUser is null)
return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
if (!userRepository.HasPermission((int)idUser, $"File.edit{idCategory}"))
2022-02-03 08:23:52 +05:00
return Forbid();
foreach (var file in files)
{
var fileStream = file.OpenReadStream();
2022-04-11 18:00:34 +05:00
await fileService.SaveAsync(idWell, idUser ?? 0, idCategory, file.FileName,
2021-11-15 16:52:12 +05:00
fileStream, token).ConfigureAwait(false);
}
return Ok();
}
/// <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(
2022-10-17 14:42:47 +05:00
[FromQuery] FileRequest request,
CancellationToken token)
{
int? idCompany = User.GetCompanyId();
2022-10-17 14:42:47 +05:00
if (request.IdCategory is null || idCompany is null)
return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value,
2022-10-17 14:42:47 +05:00
request.IdWell, token).ConfigureAwait(false))
return Forbid();
2022-10-17 14:42:47 +05:00
var filesInfo = await fileService.GetInfosPaginatedAsync(request, token).ConfigureAwait(false);
return Ok(filesInfo);
}
/// <summary>
/// Возвращает файл с диска на сервере
/// </summary>
2022-10-17 14:42:47 +05:00
/// <param name="idFile">id запрашиваемого файла</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Запрашиваемый файл</returns>
[HttpGet("{idFile}")]
[Permission]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
2022-11-17 17:54:25 +05:00
public async Task<IActionResult> GetFileAsync(
int idFile, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
2022-10-17 14:42:47 +05:00
var fileInfo = await fileService.GetOrDefaultAsync(idFile, token);
if (fileInfo is null)
return NotFound(idFile);
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
2022-10-17 14:42:47 +05:00
fileInfo.IdWell, token).ConfigureAwait(false))
return Forbid();
2022-10-17 14:42:47 +05:00
var fileStream = fileService.GetFileStream(fileInfo);
2022-10-11 14:42:04 +05:00
2022-10-17 14:42:47 +05:00
return File(fileStream, "application/octet-stream", fileInfo.Name);
}
/// <summary>
2021-08-20 14:17:53 +05:00
/// Помечает файл как удаленный
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="idFile">id запрашиваемого файла</param>
2022-11-17 17:54:25 +05:00
/// <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)
{
2022-02-03 08:23:52 +05:00
int? idUser = User.GetUserId();
int? idCompany = User.GetCompanyId();
2022-10-17 14:42:47 +05:00
if (idUser is null || idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
2022-10-17 14:42:47 +05:00
var fileInfo = await fileService.GetOrDefaultAsync(idFile, token);
if (fileInfo is null)
return NotFound(idFile);
2022-02-03 08:23:52 +05:00
if (!userRepository.HasPermission((int)idUser, $"File.edit{fileInfo?.IdCategory}"))
2022-02-03 08:23:52 +05:00
return Forbid();
2022-09-30 10:49:40 +05:00
var result = await fileService.MarkAsDeletedAsync(idFile, token);
return Ok(result);
}
/// <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();
var idUser = User.GetUserId();
if (idCompany is null || idUser is null ||
!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
2022-09-30 10:49:40 +05:00
var result = await fileService.CreateFileMarkAsync(markDto, (int)idUser, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <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();
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
var result = await fileService.MarkFileMarkAsDeletedAsync(idMark, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <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();
if (idCompany is null)
return Forbid();
try
{
2022-09-30 10:49:40 +05:00
var fileInfo = await fileService.GetOrDefaultAsync(idFile, token).ConfigureAwait(false);
return Ok(fileInfo);
}
catch (FileNotFoundException ex)
{
return NotFound(ex.FileName);
}
}
}
2023-05-19 16:51:41 +05:00
}