2024-07-04 11:02:45 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-10-11 08:28:37 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using System.IO;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
2021-08-11 12:11:21 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2022-10-27 11:22:39 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
namespace AsbCloudWebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Хранение файлов
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("api/well/{idWell}/files")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class FileController : ControllerBase
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
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)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
int? idUser = User.GetUserId();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (idCompany is null || idUser is null)
|
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
|
|
|
|
idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (!userRepository.HasPermission((int)idUser, $"File.edit{idCategory}"))
|
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
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();
|
|
|
|
|
}
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
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();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value,
|
|
|
|
|
request.IdWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var filesInfo = await fileService.GetInfosPaginatedAsync(request, token).ConfigureAwait(false);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return Ok(filesInfo);
|
|
|
|
|
}
|
2022-10-11 08:28:37 +05:00
|
|
|
|
|
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();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (idCompany is null)
|
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var fileInfo = await fileService.GetOrDefaultAsync(idFile, token);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return NotFound(idFile);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
|
|
|
|
fileInfo.IdWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
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();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
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();
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
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);
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (!userRepository.HasPermission((int)idUser, $"File.edit{fileInfo?.IdCategory}"))
|
|
|
|
|
return Forbid();
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
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();
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
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();
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var result = await fileService.CreateFileMarkAsync(markDto, (int)idUser, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
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();
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
|
|
|
|
idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var result = await fileService.MarkFileMarkAsDeletedAsync(idMark, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
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();
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (idCompany is null)
|
|
|
|
|
return Forbid();
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var fileInfo = await fileService.GetOrDefaultAsync(idFile, token).ConfigureAwait(false);
|
|
|
|
|
return Ok(fileInfo);
|
2021-11-09 17:36:44 +05:00
|
|
|
|
}
|
2024-08-19 10:01:07 +05:00
|
|
|
|
catch (FileNotFoundException ex)
|
2022-09-28 10:46:12 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return NotFound(ex.FileName);
|
2022-09-28 10:46:12 +05:00
|
|
|
|
}
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
}
|