forked from ddrilling/AsbCloudServer
134 lines
5.1 KiB
C#
134 lines
5.1 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
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>
|
|||
|
/// <param name="wellId">id скважины</param>
|
|||
|
/// <param name="idCategory">id категории файла</param>
|
|||
|
/// <param name="idUser">id отправившего файл пользователя</param>
|
|||
|
/// <param name="files">Коллекция файлов</param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
[Route("{wellId}/files")]
|
|||
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
|||
|
public IActionResult SaveFiles(int wellId, int idCategory, int idUser,
|
|||
|
[FromForm] IFormFileCollection files)
|
|||
|
{
|
|||
|
int? idCompany = User.GetCompanyId();
|
|||
|
|
|||
|
if (idCompany is null)
|
|||
|
return Forbid();
|
|||
|
|
|||
|
if (!wellService.CheckWellOwnership((int)idCompany, wellId))
|
|||
|
return Forbid();
|
|||
|
|
|||
|
var fileInfoCollection = files.Select(f =>
|
|||
|
(f.FileName, idCategory, DateTime.Now, idUser));
|
|||
|
|
|||
|
var fileNamesAndIds = fileService.SaveFilesPropertiesToDb(wellId,
|
|||
|
idCategory, fileInfoCollection);
|
|||
|
|
|||
|
foreach (var file in files)
|
|||
|
{
|
|||
|
var fileExtension = Path.GetExtension(file.FileName);
|
|||
|
|
|||
|
var fileId = fileNamesAndIds[file.FileName];
|
|||
|
|
|||
|
var relativePath = Path.Combine(fileService.RootPath, $"{wellId}",
|
|||
|
$"{idCategory}", $"{fileId}" + $"{fileExtension}");
|
|||
|
|
|||
|
Directory.CreateDirectory(Path.GetDirectoryName(relativePath));
|
|||
|
using var fileStream = new FileStream(relativePath, FileMode.Create);
|
|||
|
file.CopyTo(fileStream);
|
|||
|
}
|
|||
|
|
|||
|
return Ok();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Возвращает информацию о файлах для скважины в выбраной категории
|
|||
|
/// </summary>
|
|||
|
/// <param name="wellId">id скважины</param>
|
|||
|
/// <param name="idCategory">id категории файла</param>
|
|||
|
/// <returns>Список информации о файлах в этой категории</returns>
|
|||
|
[HttpGet]
|
|||
|
[Route("{wellId}/filesInfo")]
|
|||
|
[ProducesResponseType(typeof(IEnumerable<FilePropertiesDto>), (int)System.Net.HttpStatusCode.OK)]
|
|||
|
public IActionResult GetFilesInfo([FromRoute] int wellId, int idCategory)
|
|||
|
{
|
|||
|
int? idCompany = User.GetCompanyId();
|
|||
|
|
|||
|
if (idCompany is null || !wellService.CheckWellOwnership((int)idCompany, wellId))
|
|||
|
return Forbid();
|
|||
|
|
|||
|
var filesInfo = fileService.GetFilesInfo(wellId, idCategory);
|
|||
|
|
|||
|
if (!filesInfo.Any())
|
|||
|
return NoContent();
|
|||
|
|
|||
|
return Ok(filesInfo);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Возвращает файл с диска на сервере
|
|||
|
/// </summary>
|
|||
|
/// <param name="wellId">id скважины</param>
|
|||
|
/// <param name="fileId">id запрашиваемого файла</param>
|
|||
|
/// <returns>Запрашиваемый файл</returns>
|
|||
|
[HttpGet]
|
|||
|
[Route("{wellId}/{fileName}")]
|
|||
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
|||
|
public IActionResult GetFile([FromRoute] int wellId, int fileId)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
int? idCompany = User.GetCompanyId();
|
|||
|
|
|||
|
if (idCompany is null)
|
|||
|
return Forbid();
|
|||
|
|
|||
|
if (!wellService.CheckWellOwnership((int)idCompany, wellId))
|
|||
|
return Forbid();
|
|||
|
|
|||
|
var fileInfo = fileService.GetFileInfo(fileId);
|
|||
|
|
|||
|
if (fileInfo is null)
|
|||
|
throw new FileNotFoundException();
|
|||
|
|
|||
|
// TODO: словарь content typoв
|
|||
|
var relativePath = Path.Combine(fileService.RootPath, $"{wellId}", $"{fileInfo.Value.IdCategory}",
|
|||
|
$"{fileInfo.Value.Id}", Path.GetExtension($"{fileInfo.Value.Name}"));
|
|||
|
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Value.Name);
|
|||
|
}
|
|||
|
catch (FileNotFoundException ex)
|
|||
|
{
|
|||
|
return NotFound($"Файл не найден. Текст ошибки: {ex.Message}");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|