67 lines
2.7 KiB
C#
67 lines
2.7 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using NeDvachAPI.DBControllers;
|
|
using SkiaSharp;
|
|
|
|
namespace NeDvachAPI.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class UploadPic : ControllerBase
|
|
{
|
|
|
|
|
|
[HttpPost(Name = "UploadPicture")]
|
|
public async Task<JsonResult> ReceivePost([FromForm] IFormFile PostPicture)
|
|
{
|
|
var supportedTypes = new[] { ".jpg", ".png", ".jpeg" };
|
|
string receivedFileName = PostPicture.FileName;
|
|
string fName = Path.GetFileNameWithoutExtension(receivedFileName);
|
|
string fExtention = Path.GetExtension(receivedFileName);
|
|
string previevFileName = fName + 's' + fExtention;
|
|
|
|
Console.WriteLine("Расширение файла: " + fExtention);
|
|
|
|
if (supportedTypes.Contains(fExtention.ToLower())) //file type check
|
|
{
|
|
///Local Buffer File Part
|
|
string filePath;
|
|
string previevPath;
|
|
filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName;
|
|
previevPath = Directory.GetCurrentDirectory() + "\\Buffer\\" + previevFileName;
|
|
Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
|
|
PostPicture.CopyTo(picBuffer);
|
|
picBuffer.Close();
|
|
///resize part
|
|
|
|
bool resizeImage(string picPath, string smallPath, int width, int height)
|
|
{
|
|
var dstInfo = new SKImageInfo(height, width);
|
|
var bitmap = SKBitmap.Decode(picPath);
|
|
|
|
var image = SKImage.FromBitmap(bitmap.Resize(dstInfo, SKFilterQuality.Medium));
|
|
var data = image.Encode(SKEncodedImageFormat.Jpeg, 90);
|
|
|
|
using (var stream = new FileStream(smallPath, FileMode.Create, FileAccess.Write))
|
|
data.SaveTo(stream);
|
|
|
|
data.Dispose();
|
|
image.Dispose();
|
|
bitmap.Dispose();
|
|
return true;
|
|
}
|
|
resizeImage(filePath, previevPath, 300, 400);
|
|
///MinIo part
|
|
await MinIOchat.PictureUpload(previevPath, receivedFileName, "thread-pics-small");
|
|
JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName, "thread-pics"));
|
|
Console.WriteLine("Загружен файл:" + "http://static.vdk2ch.ru:15555/thread-pics/" + receivedFileName);
|
|
return picAdress;
|
|
}
|
|
|
|
else return new JsonResult("Неверный тип файла");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|