Files
parr_api/PARR.API/Controllers/V1/EsppDataController.cs

55 lines
2.5 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.BLL.Services.Interfaces;
using PARR.BLL.Settings;
namespace PARR.API.Controllers.V1
{
public class EsppDataController : BaseApiController
{
private readonly IFileService fileService;
private readonly StorageSettings storageSettings;
public EsppDataController(
ILogger<EsppDataController> logger,
IFileService fileService,
StorageSettings storageSettings
)
{
Logger = logger;
this.fileService = fileService;
this.storageSettings = storageSettings;
}
public ILogger<EsppDataController> Logger { get; }
/// <summary>
/// Загрузить файл списка шаблонов полученных из ЕСПП в формате csv
/// </summary>
/// <returns></returns>
[HttpPost(ApiRoutes.EsppData.UploadTemplates)]
public async Task<IActionResult> UploadTemplates([BindRequired] IFormFile file)
{
if (!fileService.IsExtensionAllowed(file, storageSettings.EsppTemplates!.AllowedExtensions))
return BadRequest(
new Response(false,
new List<ErrorModel> {
new ErrorModel {
Message = $"Недопустимое расширение файла. Разрешенные расширения: {string.Join(", ", storageSettings.EsppTemplates.AllowedExtensions)}"
} }));
if (!fileService.IsNotExceededLimit(file, storageSettings.EsppTemplates.MaxFileSizeMb))
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { FieldName = nameof(file), Message = $"Размер файла превышает {storageSettings.EsppTemplates.MaxFileSizeMb} МБайт" } }));
var uploadResult = await fileService.UploadAsync(file, storageSettings.EsppTemplates.TemplatePath);
if (uploadResult == null)
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при сохранении файла" } }));
return Ok(new Response<string>("", true, new List<ErrorModel>(), $"Файл загружен."));
}
}
}