2021-07-23 17:40:31 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/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>
|
2021-07-23 17:40:31 +05:00
|
|
|
|
/// <param name="idCategory">id категории файла</param>
|
|
|
|
|
/// <param name="idUser">id отправившего файл пользователя</param>
|
|
|
|
|
/// <param name="files">Коллекция файлов</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
[Route("{idWell}/files")]
|
2021-07-23 17:40:31 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
public IActionResult SaveFiles(int idWell, int idCategory, int idUser,
|
2021-07-23 17:40:31 +05:00
|
|
|
|
[FromForm] IFormFileCollection files)
|
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
|
|
|
|
|
if (idCompany is null)
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-07-27 14:43:30 +05:00
|
|
|
|
if (!wellService.IsCompanyOwnsWell((int)idCompany, idWell))
|
2021-07-23 17:40:31 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var fileInfoCollection = files.Select(f =>
|
2021-07-27 14:43:30 +05:00
|
|
|
|
(f.FileName, idWell, idCategory, DateTime.Now, idUser));
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-07-27 14:43:30 +05:00
|
|
|
|
var fileNamesAndIds = fileService.SaveFilesPropertiesToDb(idWell,
|
2021-07-23 17:40:31 +05:00
|
|
|
|
idCategory, fileInfoCollection);
|
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
var fileExtension = Path.GetExtension(file.FileName);
|
|
|
|
|
|
|
|
|
|
var fileId = fileNamesAndIds[file.FileName];
|
|
|
|
|
|
2021-07-27 14:43:30 +05:00
|
|
|
|
var relativePath = Path.Combine(fileService.RootPath, $"{idWell}",
|
2021-07-23 17:40:31 +05:00
|
|
|
|
$"{idCategory}", $"{fileId}" + $"{fileExtension}");
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(relativePath));
|
|
|
|
|
using var fileStream = new FileStream(relativePath, FileMode.Create);
|
|
|
|
|
file.CopyTo(fileStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-07-26 11:54:50 +05:00
|
|
|
|
/// <param name="begin">дата начала</param>
|
|
|
|
|
/// <param name="end">дата окончания</param>
|
|
|
|
|
/// <param name="skip">для пагинации кол-во записей пропустить</param>
|
|
|
|
|
/// <param name="take">для пагинации кол-во записей взять </param>
|
2021-07-23 17:40:31 +05:00
|
|
|
|
/// <returns>Список информации о файлах в этой категории</returns>
|
|
|
|
|
[HttpGet]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
[Route("{idWell}")]
|
2021-07-26 11:54:50 +05:00
|
|
|
|
[ProducesResponseType(typeof(PaginationContainer<FilePropertiesDto>), (int)System.Net.HttpStatusCode.OK)]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
public IActionResult GetFilesInfo([FromRoute] int idWell,
|
2021-07-26 11:54:50 +05:00
|
|
|
|
int skip = 0, int take = 32, int idCategory = default,
|
|
|
|
|
DateTime begin = default, DateTime end = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
|
2021-07-27 14:43:30 +05:00
|
|
|
|
if (idCompany is null || !wellService.IsCompanyOwnsWell((int)idCompany, idWell))
|
2021-07-23 17:40:31 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-07-27 14:43:30 +05:00
|
|
|
|
var filesInfo = fileService.GetFilesInfo(idWell, idCategory,
|
2021-07-26 11:54:50 +05:00
|
|
|
|
begin, end, skip, take);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-07-26 11:54:50 +05:00
|
|
|
|
if (filesInfo is null || !filesInfo.Items.Any())
|
2021-07-23 17:40:31 +05:00
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
/// <returns>Запрашиваемый файл</returns>
|
|
|
|
|
[HttpGet]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
[Route("{idWell}/{fileId}")]
|
2021-07-23 17:40:31 +05:00
|
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
2021-07-27 14:43:30 +05:00
|
|
|
|
public IActionResult GetFile([FromRoute] int idWell, int fileId)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
|
|
|
|
|
if (idCompany is null)
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-07-27 14:43:30 +05:00
|
|
|
|
if (!wellService.IsCompanyOwnsWell((int)idCompany, idWell))
|
2021-07-23 17:40:31 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var fileInfo = fileService.GetFileInfo(fileId);
|
|
|
|
|
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
|
|
|
|
|
|
// TODO: словарь content typoв
|
2021-07-27 14:43:30 +05:00
|
|
|
|
var relativePath = Path.Combine(fileService.RootPath, $"{idWell}", $"{fileInfo.Value.IdCategory}",
|
2021-07-26 16:54:53 +05:00
|
|
|
|
$"{fileInfo.Value.Id}" + Path.GetExtension($"{fileInfo.Value.Name}"));
|
2021-07-23 17:40:31 +05:00
|
|
|
|
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Value.Name);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
return NotFound($"Файл не найден. Текст ошибки: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|