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

45 lines
1.6 KiB
C#
Raw Normal View History

2022-11-02 01:45:42 +10:00
using Microsoft.AspNetCore.Mvc;
2022-11-03 15:13:58 +10:00
using Microsoft.VisualBasic;
using Microsoft.Win32.SafeHandles;
2022-11-02 17:39:39 +10:00
using System.IO;
2022-11-02 01:45:42 +10:00
namespace NeDvachAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class UploadPic : ControllerBase
{
2022-11-02 17:39:39 +10:00
///Local Buffer File Part
string filePath;
2022-11-02 01:45:42 +10:00
[HttpPost(Name = "UploadPicture")]
2022-11-02 17:39:39 +10:00
public async Task<JsonResult> ReceivePost([FromForm] IFormFile PostPicture)
2022-11-02 01:45:42 +10:00
{
2022-11-02 17:39:39 +10:00
var supportedTypes = new[] { "jpg", "png" };
string receivedFileName = PostPicture.FileName;
string fileExt = receivedFileName.Substring((receivedFileName.Length) - 3, 3);
2022-11-03 15:13:58 +10:00
//Console.WriteLine("Тип файла: " + fileExt);
2022-11-02 17:39:39 +10:00
if (supportedTypes.Contains(fileExt)) //file type check
{
filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName;
2022-11-03 15:13:58 +10:00
Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
//Console.WriteLine("Закидываю файл в " + filePath);
2022-11-02 17:39:39 +10:00
PostPicture.CopyTo(picBuffer);
picBuffer.Close();
2022-11-03 15:13:58 +10:00
///MinIo part
JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName));
Console.WriteLine("Загружен файл:" + "http://static.vdk2ch.ru:15555/thread-pics/" + receivedFileName);
return picAdress;
}
2022-11-02 17:39:39 +10:00
else return new JsonResult("Неверный тип файла");
2022-11-02 01:45:42 +10:00
}
2022-11-03 15:13:58 +10:00
2022-11-02 01:45:42 +10:00
}
2022-11-03 15:13:58 +10:00
2022-11-02 01:45:42 +10:00
}