2021-08-09 15:41:42 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
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;
|
|
|
|
|
using System;
|
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;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
2021-08-16 17:34:00 +05:00
|
|
|
|
[Route("api/well/{idWell}/files")]
|
2021-07-23 17:40:31 +05:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class FileController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IFileService fileService;
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
|
|
|
|
|
public FileController(IFileService fileService, IWellService wellService)
|
|
|
|
|
{
|
|
|
|
|
this.fileService = fileService;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохраняет переданные файлы и информацию о них
|
|
|
|
|
/// </summary>
|
2021-07-27 14:43:30 +05:00
|
|
|
|
/// <param name="idWell">id скважины</param>
|
2021-07-23 17:40:31 +05:00
|
|
|
|
/// <param name="idCategory">id категории файла</param>
|
|
|
|
|
/// <param name="files">Коллекция файлов</param>
|
2022-02-03 08:23:52 +05:00
|
|
|
|
/// <param name="userService">dependency</param>
|
2021-08-11 16:54:42 +05:00
|
|
|
|
/// <param name="token"> Токен отмены задачи </param>
|
2021-07-23 17:40:31 +05:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-07-23 17:40:31 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-19 16:58:26 +05:00
|
|
|
|
public async Task<IActionResult> SaveFilesAsync(int idWell, int idCategory,
|
2022-02-03 08:23:52 +05:00
|
|
|
|
[FromForm] IFormFileCollection files, [FromServices] IUserService userService, CancellationToken token = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
2021-08-17 13:03:17 +05:00
|
|
|
|
int? idUser = User.GetUserId();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-17 13:03:17 +05:00
|
|
|
|
if (idCompany is null || idUser is null)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-08-19 16:58:26 +05:00
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
idWell, token).ConfigureAwait(false))
|
2021-08-19 16:58:26 +05:00
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
if (!userService.HasPermission((int)idUser, $"File.edit{idCategory}"))
|
2022-02-03 08:23:52 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-07-23 17:40:31 +05:00
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
2021-08-19 16:32:04 +05:00
|
|
|
|
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);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает информацию о файлах для скважины в выбраной категории
|
|
|
|
|
/// </summary>
|
2021-07-27 14:43:30 +05:00
|
|
|
|
/// <param name="idWell">id скважины</param>
|
2021-07-23 17:40:31 +05:00
|
|
|
|
/// <param name="idCategory">id категории файла</param>
|
2021-08-31 18:01:26 +05:00
|
|
|
|
/// <param name="companyName">id компаний для фильтрации возвращаемых файлов</param>
|
2021-08-31 10:02:04 +05:00
|
|
|
|
/// <param name="fileName">часть имени файла для поиска</param>
|
2021-07-26 11:54:50 +05:00
|
|
|
|
/// <param name="begin">дата начала</param>
|
|
|
|
|
/// <param name="end">дата окончания</param>
|
|
|
|
|
/// <param name="skip">для пагинации кол-во записей пропустить</param>
|
|
|
|
|
/// <param name="take">для пагинации кол-во записей взять </param>
|
2021-08-11 16:54:42 +05:00
|
|
|
|
/// <param name="token"> Токен отмены задачи </param>
|
2021-07-23 17:40:31 +05:00
|
|
|
|
/// <returns>Список информации о файлах в этой категории</returns>
|
|
|
|
|
[HttpGet]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-08-13 17:26:19 +05:00
|
|
|
|
[ProducesResponseType(typeof(PaginationContainer<FileInfoDto>), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-31 09:59:23 +05:00
|
|
|
|
public async Task<IActionResult> GetFilesInfoAsync(
|
|
|
|
|
[FromRoute] int idWell,
|
|
|
|
|
int idCategory = default,
|
2021-08-31 18:01:26 +05:00
|
|
|
|
string companyName = default,
|
2021-08-31 09:59:23 +05:00
|
|
|
|
string fileName = default,
|
2022-04-11 18:00:34 +05:00
|
|
|
|
DateTime begin = default,
|
2021-08-31 09:59:23 +05:00
|
|
|
|
DateTime end = default,
|
2022-04-11 18:00:34 +05:00
|
|
|
|
int skip = 0,
|
|
|
|
|
int take = 32,
|
2021-08-31 09:59:23 +05:00
|
|
|
|
CancellationToken token = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
|
2021-08-19 16:58:26 +05:00
|
|
|
|
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
idWell, token).ConfigureAwait(false))
|
2021-08-19 16:58:26 +05:00
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
var filesInfo = await fileService.GetInfosAsync(idWell, idCategory,
|
2021-08-31 18:01:26 +05:00
|
|
|
|
companyName, fileName, begin, end, skip, take, token).ConfigureAwait(false);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
|
|
|
|
return Ok(filesInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает файл с диска на сервере
|
|
|
|
|
/// </summary>
|
2021-07-27 14:43:30 +05:00
|
|
|
|
/// <param name="idWell">id скважины</param>
|
2021-07-23 17:40:31 +05:00
|
|
|
|
/// <param name="fileId">id запрашиваемого файла</param>
|
2021-08-11 16:54:42 +05:00
|
|
|
|
/// <param name="token"> Токен отмены задачи </param>
|
2021-07-23 17:40:31 +05:00
|
|
|
|
/// <returns>Запрашиваемый файл</returns>
|
|
|
|
|
[HttpGet]
|
2021-08-16 17:34:00 +05:00
|
|
|
|
[Route("{fileId}")]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-07-23 17:40:31 +05:00
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-19 16:58:26 +05:00
|
|
|
|
public async Task<IActionResult> GetFileAsync([FromRoute] int idWell,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
int fileId, CancellationToken token = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2022-02-08 10:25:05 +05:00
|
|
|
|
int? idCompany = User.GetCompanyId();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2022-02-08 10:25:05 +05:00
|
|
|
|
if (idCompany is null)
|
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2022-02-08 10:25:05 +05:00
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
|
|
|
|
idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2022-02-08 10:25:05 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-08-29 17:25:16 +05:00
|
|
|
|
var fileInfo = await fileService.GetInfoAsync(fileId, token);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-09-23 10:53:48 +05:00
|
|
|
|
var relativePath = fileService.GetUrl(fileInfo);
|
2021-08-13 17:26:19 +05:00
|
|
|
|
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
|
{
|
2022-02-08 10:25:05 +05:00
|
|
|
|
return NotFound(ex.FileName);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-08-20 14:17:53 +05:00
|
|
|
|
/// Помечает файл как удаленный
|
2021-08-19 16:58:26 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="idFile">id запрашиваемого файла</param>
|
2022-02-03 08:23:52 +05:00
|
|
|
|
/// <param name="userService">dependency</param>
|
2021-11-09 17:36:44 +05:00
|
|
|
|
/// <param name="token">Токен отмены задачи </param>
|
2021-08-19 16:58:26 +05:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete("{idFile}")]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-08-19 16:58:26 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> DeleteAsync(int idWell, int idFile,
|
2022-02-03 08:23:52 +05:00
|
|
|
|
[FromServices] IUserService userService,
|
2021-08-19 16:58:26 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2022-02-03 08:23:52 +05:00
|
|
|
|
int? idUser = User.GetUserId();
|
|
|
|
|
|
2021-08-19 16:58:26 +05:00
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
|
|
|
|
|
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
|
|
|
|
idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
var file = await fileService.GetInfoAsync((int)idFile, token);
|
2022-02-03 08:23:52 +05:00
|
|
|
|
|
|
|
|
|
if (!userService.HasPermission((int)idUser, $"File.edit{file.IdCategory}"))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
var result = await fileService.MarkAsDeletedAsync(idFile, token);
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создает метку для файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины </param>
|
|
|
|
|
/// <param name="markDto">метка файла</param>
|
|
|
|
|
/// <param name="token">Токен отмены задачи </param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost("fileMark")]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-11-09 17:36:44 +05:00
|
|
|
|
public async Task<IActionResult> CreateFileMarkAsync(int idWell, FileMarkDto markDto, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
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")]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-11-09 17:36:44 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> DeleteFileMarkAsync(int idWell, int idMark,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
}
|