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/APIControllers/PictureUpload.cs

51 lines
2.0 KiB
C#
Raw Normal View History

2022-11-02 01:45:42 +10:00
using Microsoft.AspNetCore.Mvc;
2022-11-25 22:40:17 +10:00
using NeDvachAPI.DBControllers;
2022-12-14 00:18:23 +10:00
2022-11-02 01:45:42 +10:00
namespace NeDvachAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class UploadPic : ControllerBase
2022-11-25 22:40:17 +10:00
{
2022-11-02 17:39:39 +10:00
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-12-14 00:18:23 +10:00
2022-11-02 17:39:39 +10:00
string receivedFileName = PostPicture.FileName;
2022-12-07 19:55:18 +10:00
string fName = Path.GetFileNameWithoutExtension(receivedFileName);
string fExtention = Path.GetExtension(receivedFileName);
string previevFileName = fName + 's' + fExtention;
Console.WriteLine("Расширение файла: " + fExtention);
2022-12-14 00:18:23 +10:00
if (ImageController.CheckExtention(fExtention)) //file type check
2022-11-02 17:39:39 +10:00
{
2022-11-25 22:40:17 +10:00
///Local Buffer File Part
string filePath;
2022-12-07 19:55:18 +10:00
string previevPath;
2022-11-02 17:39:39 +10:00
filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName;
2022-12-07 19:55:18 +10:00
previevPath = Directory.GetCurrentDirectory() + "\\Buffer\\" + previevFileName;
2022-12-14 00:18:23 +10:00
//creating original file buffer
2022-11-03 15:13:58 +10:00
Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
2022-11-02 17:39:39 +10:00
PostPicture.CopyTo(picBuffer);
picBuffer.Close();
2022-12-14 00:18:23 +10:00
//resizing
ImageController.ResizeImage(filePath, previevPath, 300, 400);
2022-11-03 15:13:58 +10:00
///MinIo part
2022-12-14 00:18:23 +10:00
await MinIOchat.PictureUpload(previevPath, receivedFileName, "thread-pics-small"); //upload thumbnail
JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName, "thread-pics")); //upload fullsize
2022-11-03 15:13:58 +10:00
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
}