diff --git a/.gitignore b/.gitignore index da6f134..5a504a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ obj bin dist +AuthInfo.cs \ No newline at end of file diff --git a/Buffer/15935082509663.jpg b/Buffer/15935082509663.jpg new file mode 100644 index 0000000..fa1a466 Binary files /dev/null and b/Buffer/15935082509663.jpg differ diff --git a/Buffer/15973952325820.jpg b/Buffer/15973952325820.jpg new file mode 100644 index 0000000..927004c Binary files /dev/null and b/Buffer/15973952325820.jpg differ diff --git a/Buffer/lvy.png b/Buffer/lvy.png new file mode 100644 index 0000000..6036ae1 Binary files /dev/null and b/Buffer/lvy.png differ diff --git a/Controllers/PictureUpload.cs b/Controllers/PictureUpload.cs index b5b65b6..1de4997 100644 --- a/Controllers/PictureUpload.cs +++ b/Controllers/PictureUpload.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Mvc; -using System.Net.Http.Headers; -using System.Text.Json; +using System.IO; + namespace NeDvachAPI.Controllers { @@ -8,12 +8,28 @@ namespace NeDvachAPI.Controllers [Route("[controller]")] public class UploadPic : ControllerBase { + ///Local Buffer File Part + + string filePath; + [HttpPost(Name = "UploadPicture")] - public JsonResult ReceivePost([FromForm] IFormFile PostPicture) + public async Task ReceivePost([FromForm] IFormFile PostPicture) { - Console.Write("Принята картинка "); - Console.WriteLine(PostPicture.FileName); - return new JsonResult(PostPicture.FileName + " получен!"); + var supportedTypes = new[] { "jpg", "png" }; + string receivedFileName = PostPicture.FileName; + string fileExt = receivedFileName.Substring((receivedFileName.Length) - 3, 3); + Console.WriteLine("Тип файла: " + fileExt); + if (supportedTypes.Contains(fileExt)) //file type check + { + + filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName; + Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.Write); + Console.WriteLine("Закидываю файл в " + filePath); + PostPicture.CopyTo(picBuffer); + picBuffer.Close(); + return new JsonResult(await MinIOchat.PictureUpload(filePath, receivedFileName)); + } + else return new JsonResult("Неверный тип файла"); } } } diff --git a/DBchat.cs b/DBchat.cs index d6be19b..f762262 100644 --- a/DBchat.cs +++ b/DBchat.cs @@ -6,11 +6,7 @@ namespace NeDvachAPI public class DBchat { // Obtain connection string information from the portal - private static string Host = "postgres.vdk2ch.ru"; - private static string User = "postgres"; - private static string DBname = "postgres"; - private static string Password = "postgres"; - private static string Port = "5432"; + public static List DbList() { @@ -22,11 +18,11 @@ namespace NeDvachAPI string connString = String.Format( "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", - Host, - User, - DBname, - Port, - Password); + AuthInfo.DB.Host, + AuthInfo.DB.User, + AuthInfo.DB.DBname, + AuthInfo.DB.Port, + AuthInfo.DB.Password); using (var conn = new NpgsqlConnection(connString)) { @@ -42,7 +38,8 @@ namespace NeDvachAPI // ORDER BY post_id DESC // limit 10 // ) - // subquery ORDER BY post_id ASC", conn) ) + // subquery + // ORDER BY post_id ASC", conn) ) using ( var command = new NpgsqlCommand(@" SELECT * FROM ( @@ -86,11 +83,11 @@ namespace NeDvachAPI string connString = String.Format( "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", - Host, - User, - DBname, - Port, - Password); + AuthInfo.DB.Host, + AuthInfo.DB.User, + AuthInfo.DB.DBname, + AuthInfo.DB.Port, + AuthInfo.DB.Password); using (var conn = new NpgsqlConnection(connString)) { @@ -115,11 +112,11 @@ namespace NeDvachAPI string connString = String.Format( "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", - Host, - User, - DBname, - Port, - Password); + AuthInfo.DB.Host, + AuthInfo.DB.User, + AuthInfo.DB.DBname, + AuthInfo.DB.Port, + AuthInfo.DB.Password); using (var conn = new NpgsqlConnection(connString)) { @@ -150,11 +147,11 @@ namespace NeDvachAPI string connString = String.Format( "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", - Host, - User, - DBname, - Port, - Password); + AuthInfo.DB.Host, + AuthInfo.DB.User, + AuthInfo.DB.DBname, + AuthInfo.DB.Port, + AuthInfo.DB.Password); using (var conn = new NpgsqlConnection(connString)) { diff --git a/MinIOchat.cs b/MinIOchat.cs new file mode 100644 index 0000000..99b6053 --- /dev/null +++ b/MinIOchat.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Mvc; +using Minio.DataModel; +using Minio; +using System.Security.Cryptography; + +namespace NeDvachAPI +{ + public class MinIOchat + { + public static async Task PictureUpload(string fileroute, string filename) + { + MinioClient DvachIo = new MinioClient() + .WithEndpoint(AuthInfo.MinIo.endpoint) + .WithCredentials(AuthInfo.MinIo.username, AuthInfo.MinIo.password) + //.WithSSL() + .Build(); + Console.WriteLine(filename); + + Aes aesEncryption = Aes.Create(); + aesEncryption.KeySize = 256; + aesEncryption.GenerateKey(); + var ssec = new SSEC(aesEncryption.Key); + PutObjectArgs putObjectArgs = new PutObjectArgs() + .WithBucket(AuthInfo.MinIo.bucketName) + .WithObject(filename) + .WithFileName(fileroute) + .WithContentType("image/png"); + //.WithServerSideEncryption(ssec); + Console.WriteLine("Кидаю в minio..."); + await DvachIo.PutObjectAsync(putObjectArgs); + + return "http://static.vdk2ch.ru:15555/thread-pics/" + filename; + } + } +} diff --git a/NeDvachAPI.csproj b/NeDvachAPI.csproj index fa2104b..3c9a38b 100644 --- a/NeDvachAPI.csproj +++ b/NeDvachAPI.csproj @@ -7,8 +7,13 @@ + + + + + diff --git a/Program.cs b/Program.cs index dacd95a..92c7979 100644 --- a/Program.cs +++ b/Program.cs @@ -12,8 +12,8 @@ builder.Services.AddCors(setup => setup.AddDefaultPolicy(policyBuilder => { - policyBuilder.WithOrigins("http://www.vdk2ch.ru:4200").WithMethods("GET", "POST").WithHeaders("*"); - //policyBuilder.WithOrigins("http://localhost: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("*"); }); }); @@ -31,6 +31,7 @@ if (app.Environment.IsDevelopment()) //app.UseHttpsRedirection(); + app.UseCors(); app.UseAuthorization(); diff --git a/newFile.txt b/newFile.txt new file mode 100644 index 0000000..e69de29