This repository has been archived on 2023-06-30. You can view files and clone it, but cannot push or open issues or pull requests.
2chBackAPI/Controllers/PictureUpload.cs
RakVhalate e7d4292d82
Some checks failed
continuous-integration/drone/push Build is failing
Image upload function
2022-11-03 15:13:58 +10:00

45 lines
1.6 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using Microsoft.Win32.SafeHandles;
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.ReadWrite, FileShare.ReadWrite);
//Console.WriteLine("Закидываю файл в " + filePath);
PostPicture.CopyTo(picBuffer);
picBuffer.Close();
///MinIo part
JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName));
Console.WriteLine("Загружен файл:" + "http://static.vdk2ch.ru:15555/thread-pics/" + receivedFileName);
return picAdress;
}
else return new JsonResult("Неверный тип файла");
}
}
}