Refactoring
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
RakVhalate 2022-12-13 19:18:23 +05:00
parent 34a625361e
commit 35139d66a2
2 changed files with 45 additions and 25 deletions

View File

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NeDvachAPI.DBControllers; using NeDvachAPI.DBControllers;
using SkiaSharp;
namespace NeDvachAPI.Controllers namespace NeDvachAPI.Controllers
{ {
@ -13,46 +13,30 @@ namespace NeDvachAPI.Controllers
[HttpPost(Name = "UploadPicture")] [HttpPost(Name = "UploadPicture")]
public async Task<JsonResult> ReceivePost([FromForm] IFormFile PostPicture) public async Task<JsonResult> ReceivePost([FromForm] IFormFile PostPicture)
{ {
var supportedTypes = new[] { ".jpg", ".png", ".jpeg" };
string receivedFileName = PostPicture.FileName; string receivedFileName = PostPicture.FileName;
string fName = Path.GetFileNameWithoutExtension(receivedFileName); string fName = Path.GetFileNameWithoutExtension(receivedFileName);
string fExtention = Path.GetExtension(receivedFileName); string fExtention = Path.GetExtension(receivedFileName);
string previevFileName = fName + 's' + fExtention; string previevFileName = fName + 's' + fExtention;
Console.WriteLine("Расширение файла: " + fExtention); Console.WriteLine("Расширение файла: " + fExtention);
if (supportedTypes.Contains(fExtention.ToLower())) //file type check if (ImageController.CheckExtention(fExtention)) //file type check
{ {
///Local Buffer File Part ///Local Buffer File Part
string filePath; string filePath;
string previevPath; string previevPath;
filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName; filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName;
previevPath = Directory.GetCurrentDirectory() + "\\Buffer\\" + previevFileName; previevPath = Directory.GetCurrentDirectory() + "\\Buffer\\" + previevFileName;
//creating original file buffer
Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
PostPicture.CopyTo(picBuffer); PostPicture.CopyTo(picBuffer);
picBuffer.Close(); picBuffer.Close();
///resize part //resizing
ImageController.ResizeImage(filePath, previevPath, 300, 400);
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 ///MinIo part
await MinIOchat.PictureUpload(previevPath, receivedFileName, "thread-pics-small"); await MinIOchat.PictureUpload(previevPath, receivedFileName, "thread-pics-small"); //upload thumbnail
JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName, "thread-pics")); JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName, "thread-pics")); //upload fullsize
Console.WriteLine("Загружен файл:" + "http://static.vdk2ch.ru:15555/thread-pics/" + receivedFileName); Console.WriteLine("Загружен файл:" + "http://static.vdk2ch.ru:15555/thread-pics/" + receivedFileName);
return picAdress; return picAdress;
} }

View File

@ -0,0 +1,36 @@
using SkiaSharp;
namespace NeDvachAPI.DBControllers
{
public class ImageController
{
public static bool CheckExtention(string extentionToCheck)
{
var supportedTypes = new[] { ".jpg", ".png", ".jpeg" };
if (supportedTypes.Contains(extentionToCheck.ToLower()))
{
return true;
}
else return false;
}
public static 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;
}
}
}