This commit is contained in:
parent
10db158595
commit
32bb6d34e0
@ -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,19 +13,46 @@ 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" };
|
var supportedTypes = new[] { ".jpg", ".png", ".jpeg" };
|
||||||
string receivedFileName = PostPicture.FileName;
|
string receivedFileName = PostPicture.FileName;
|
||||||
string fileExt = receivedFileName.Substring((receivedFileName.Length) - 3, 3);
|
string fName = Path.GetFileNameWithoutExtension(receivedFileName);
|
||||||
if (supportedTypes.Contains(fileExt.ToLower())) //file type check
|
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
|
///Local Buffer File Part
|
||||||
string filePath;
|
string filePath;
|
||||||
|
string previevPath;
|
||||||
filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName;
|
filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName;
|
||||||
|
previevPath = Directory.GetCurrentDirectory() + "\\Buffer\\" + previevFileName;
|
||||||
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
|
||||||
|
|
||||||
|
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
|
||||||
JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName));
|
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);
|
Console.WriteLine("Загружен файл:" + "http://static.vdk2ch.ru:15555/thread-pics/" + receivedFileName);
|
||||||
return picAdress;
|
return picAdress;
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
public class MinIo
|
public class MinIo
|
||||||
{
|
{
|
||||||
public static string endpoint = "static.vdk2ch.ru:15555";
|
public static string endpoint = "static.vdk2ch.ru:15555";
|
||||||
public static string bucketName = "thread-pics";
|
public static string bucketName = "thread-pics thread-pics-small";
|
||||||
public static string username = "admin";
|
public static string username = "admin";
|
||||||
public static string password = "password2ch";
|
public static string password = "password2ch";
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ namespace NeDvachAPI.DBControllers
|
|||||||
{
|
{
|
||||||
public class MinIOchat
|
public class MinIOchat
|
||||||
{
|
{
|
||||||
public static async Task<JsonResult> PictureUpload(string fileroute, string filename)
|
public static async Task<JsonResult> PictureUpload(string fileroute, string filename, string bucket)
|
||||||
{
|
{
|
||||||
MinioClient DvachIo = new MinioClient()
|
MinioClient DvachIo = new MinioClient()
|
||||||
.WithEndpoint(AuthInfo.MinIo.endpoint)
|
.WithEndpoint(AuthInfo.MinIo.endpoint)
|
||||||
@ -20,7 +20,7 @@ namespace NeDvachAPI.DBControllers
|
|||||||
aesEncryption.GenerateKey();
|
aesEncryption.GenerateKey();
|
||||||
var ssec = new SSEC(aesEncryption.Key);
|
var ssec = new SSEC(aesEncryption.Key);
|
||||||
PutObjectArgs putObjectArgs = new PutObjectArgs()
|
PutObjectArgs putObjectArgs = new PutObjectArgs()
|
||||||
.WithBucket(AuthInfo.MinIo.bucketName)
|
.WithBucket(bucket)
|
||||||
.WithObject(filename)
|
.WithObject(filename)
|
||||||
.WithFileName(fileroute)
|
.WithFileName(fileroute)
|
||||||
.WithContentType("image/png");
|
.WithContentType("image/png");
|
||||||
|
@ -10,7 +10,13 @@
|
|||||||
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.9" />
|
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.9" />
|
||||||
<PackageReference Include="Minio" Version="4.0.6" />
|
<PackageReference Include="Minio" Version="4.0.6" />
|
||||||
<PackageReference Include="Npgsql" Version="6.0.7" />
|
<PackageReference Include="Npgsql" Version="6.0.7" />
|
||||||
|
<PackageReference Include="SkiaSharp" Version="2.88.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||||
|
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Buffer\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Reference in New Issue
Block a user