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

67 lines
2.7 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-07 19:55:18 +10:00
using SkiaSharp;
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-07 19:55:18 +10:00
var supportedTypes = new[] { ".jpg", ".png", ".jpeg" };
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);
if (supportedTypes.Contains(fExtention.ToLower())) //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-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-07 19:55:18 +10:00
///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);
2022-11-03 15:13:58 +10:00
///MinIo part
2022-12-07 19:55:18 +10:00
await MinIOchat.PictureUpload(previevPath, receivedFileName, "thread-pics-small");
JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName, "thread-pics"));
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
}