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

228 lines
9.1 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
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
{
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>
/// <param name="idCategory">id категории файла</param>
/// <param name="files">Коллекция файлов</param>
2022-02-03 08:23:52 +05:00
/// <param name="userService">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,
2022-02-03 08:23:52 +05:00
[FromForm] IFormFileCollection files, [FromServices] IUserService userService, CancellationToken token = default)
{
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();
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();
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>
2021-07-27 14:43:30 +05:00
/// <param name="idWell">id скважины</param>
/// <param name="idCategory">id категории файла</param>
/// <param name="companyName">id компаний для фильтрации возвращаемых файлов</param>
2021-08-31 10:02:04 +05:00
/// <param name="fileName">часть имени файла для поиска</param>
/// <param name="begin">дата начала</param>
/// <param name="end">дата окончания</param>
/// <param name="skip">для пагинации кол-во записей пропустить</param>
/// <param name="take">для пагинации кол-во записей взять </param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Список информации о файлах в этой категории</returns>
[HttpGet]
[Permission]
[ProducesResponseType(typeof(PaginationContainer<FileInfoDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFilesInfoAsync(
[FromRoute] int idWell,
int idCategory = default,
string companyName = default,
string fileName = default,
2022-04-11 18:00:34 +05:00
DateTime begin = default,
DateTime end = default,
2022-04-11 18:00:34 +05:00
int skip = 0,
int take = 32,
CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
2021-08-29 17:25:16 +05:00
var filesInfo = await fileService.GetInfosAsync(idWell, idCategory,
companyName, fileName, begin, end, skip, take, token).ConfigureAwait(false);
return Ok(filesInfo);
}
/// <summary>
/// Возвращает файл с диска на сервере
/// </summary>
2021-07-27 14:43:30 +05:00
/// <param name="idWell">id скважины</param>
/// <param name="fileId">id запрашиваемого файла</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Запрашиваемый файл</returns>
[HttpGet]
2021-08-16 17:34:00 +05:00
[Route("{fileId}")]
[Permission]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFileAsync([FromRoute] int idWell,
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
{
2021-08-29 17:25:16 +05:00
var fileInfo = await fileService.GetInfoAsync(fileId, token);
var relativePath = fileService.GetUrl(fileInfo);
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name);
}
catch (FileNotFoundException ex)
{
return NotFound(ex.FileName);
}
}
/// <summary>
2021-08-20 14:17:53 +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>
/// <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,
2022-02-03 08:23:52 +05:00
[FromServices] IUserService userService,
CancellationToken token = default)
{
2022-02-03 08:23:52 +05:00
int? idUser = User.GetUserId();
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);
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 = 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")]
[Permission]
[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);
}
}
}