Image upload function
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
RakVhalate 2022-11-03 15:13:58 +10:00
parent 3e9426ebd0
commit e7d4292d82
7 changed files with 31 additions and 13 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 324 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 494 KiB

View File

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using Microsoft.Win32.SafeHandles;
using System.IO; using System.IO;
@ -18,18 +20,25 @@ namespace NeDvachAPI.Controllers
var supportedTypes = new[] { "jpg", "png" }; var supportedTypes = new[] { "jpg", "png" };
string receivedFileName = PostPicture.FileName; string receivedFileName = PostPicture.FileName;
string fileExt = receivedFileName.Substring((receivedFileName.Length) - 3, 3); string fileExt = receivedFileName.Substring((receivedFileName.Length) - 3, 3);
Console.WriteLine("Тип файла: " + fileExt); //Console.WriteLine("Тип файла: " + fileExt);
if (supportedTypes.Contains(fileExt)) //file type check if (supportedTypes.Contains(fileExt)) //file type check
{ {
filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName; filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName;
Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.Write); Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
Console.WriteLine("Закидываю файл в " + filePath); //Console.WriteLine("Закидываю файл в " + filePath);
PostPicture.CopyTo(picBuffer); PostPicture.CopyTo(picBuffer);
picBuffer.Close(); picBuffer.Close();
return new JsonResult(await MinIOchat.PictureUpload(filePath, receivedFileName)); ///MinIo part
JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName));
Console.WriteLine("Загружен файл:" + "http://static.vdk2ch.ru:15555/thread-pics/" + receivedFileName);
return picAdress;
} }
else return new JsonResult("Неверный тип файла"); else return new JsonResult("Неверный тип файла");
} }
} }
} }

View File

@ -125,8 +125,12 @@ namespace NeDvachAPI
conn.Open(); conn.Open();
using (var command = new NpgsqlCommand(@" using (var command = new NpgsqlCommand(@"
INSERT INTO dvach (post_id, post, post_timestamp) INSERT INTO dvach (post_id, post, post_pic, post_timestamp)
VALUES ( DEFAULT, @postText, ( VALUES (
DEFAULT,
@postText,
@postImgUrl,
(
SELECT date_trunc( SELECT date_trunc(
'second', 'second',
(now()::timestamp(0) AT TIME ZONE 'UTC+10')::TIMESTAMP (now()::timestamp(0) AT TIME ZONE 'UTC+10')::TIMESTAMP
@ -134,6 +138,7 @@ namespace NeDvachAPI
) )", conn)) ) )", conn))
{ {
command.Parameters.AddWithValue("postText", postToSend.Text); command.Parameters.AddWithValue("postText", postToSend.Text);
command.Parameters.AddWithValue("postImgUrl", postToSend.ImgURL);
//command.Parameters.AddWithValue("postTimeStamp", DateTime.Now.ToString("dd/MM/yyyy HH:mm")); //command.Parameters.AddWithValue("postTimeStamp", DateTime.Now.ToString("dd/MM/yyyy HH:mm"));
int nRows = command.ExecuteNonQuery(); int nRows = command.ExecuteNonQuery();

View File

@ -7,14 +7,14 @@ namespace NeDvachAPI
{ {
public class MinIOchat public class MinIOchat
{ {
public static async Task<string> PictureUpload(string fileroute, string filename) public static async Task<JsonResult> PictureUpload(string fileroute, string filename)
{ {
MinioClient DvachIo = new MinioClient() MinioClient DvachIo = new MinioClient()
.WithEndpoint(AuthInfo.MinIo.endpoint) .WithEndpoint(AuthInfo.MinIo.endpoint)
.WithCredentials(AuthInfo.MinIo.username, AuthInfo.MinIo.password) .WithCredentials(AuthInfo.MinIo.username, AuthInfo.MinIo.password)
//.WithSSL() //.WithSSL()
.Build(); .Build();
Console.WriteLine(filename); //Console.WriteLine(filename);
Aes aesEncryption = Aes.Create(); Aes aesEncryption = Aes.Create();
aesEncryption.KeySize = 256; aesEncryption.KeySize = 256;
@ -26,10 +26,14 @@ namespace NeDvachAPI
.WithFileName(fileroute) .WithFileName(fileroute)
.WithContentType("image/png"); .WithContentType("image/png");
//.WithServerSideEncryption(ssec); //.WithServerSideEncryption(ssec);
Console.WriteLine("Кидаю в minio..."); //Console.WriteLine("Кидаю в minio...");
await DvachIo.PutObjectAsync(putObjectArgs); await DvachIo.PutObjectAsync(putObjectArgs);
//await Task.Delay(5000);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(fileroute);
return "http://static.vdk2ch.ru:15555/thread-pics/" + filename; return new JsonResult("http://static.vdk2ch.ru:15555/thread-pics/" + filename);
} }
} }
} }

View File

@ -13,7 +13,7 @@ builder.Services.AddCors(setup =>
{ {
policyBuilder.WithOrigins("http://www.vdk2ch.ru:4200").WithMethods("GET", "POST").WithHeaders("*"); policyBuilder.WithOrigins("http://www.vdk2ch.ru:4200").WithMethods("GET", "POST").WithHeaders("*");
//policyBuilder.WithOrigins("http://localhost:4200").WithMethods("GET", "POST").WithHeaders("*"); policyBuilder.WithOrigins("http://localhost:4200").WithMethods("GET", "POST").WithHeaders("*");
}); });
}); });