using Microsoft.AspNetCore.Mvc;
using System.IO;


namespace NeDvachAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UploadPic : ControllerBase
    {
        ///Local Buffer File Part
                
        string filePath;

        [HttpPost(Name = "UploadPicture")]
        public async Task<JsonResult> ReceivePost([FromForm] IFormFile PostPicture)
        {
            var supportedTypes = new[] { "jpg", "png" };
            string receivedFileName = PostPicture.FileName;
            string fileExt = receivedFileName.Substring((receivedFileName.Length) - 3, 3);
            Console.WriteLine("Тип файла: " + fileExt);
            if (supportedTypes.Contains(fileExt))  //file type check
            {

                filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName;
                Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                Console.WriteLine("Закидываю файл в " + filePath);
                PostPicture.CopyTo(picBuffer);
                picBuffer.Close();
                return new JsonResult(await MinIOchat.PictureUpload(filePath, receivedFileName));
             }
            else return new JsonResult("Неверный тип файла");
        }
    }
}
