adding picture posting controller
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
RakVhalate 2022-11-02 17:39:39 +10:00
parent b2cf921b9a
commit f83b76b2de
10 changed files with 89 additions and 34 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
obj obj
bin bin
dist dist
AuthInfo.cs

BIN
Buffer/15935082509663.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 324 KiB

BIN
Buffer/15973952325820.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 MiB

BIN
Buffer/lvy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 KiB

View File

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Net.Http.Headers; using System.IO;
using System.Text.Json;
namespace NeDvachAPI.Controllers namespace NeDvachAPI.Controllers
{ {
@ -8,12 +8,28 @@ namespace NeDvachAPI.Controllers
[Route("[controller]")] [Route("[controller]")]
public class UploadPic : ControllerBase public class UploadPic : ControllerBase
{ {
///Local Buffer File Part
string filePath;
[HttpPost(Name = "UploadPicture")] [HttpPost(Name = "UploadPicture")]
public JsonResult ReceivePost([FromForm] IFormFile PostPicture) public async Task<JsonResult> ReceivePost([FromForm] IFormFile PostPicture)
{ {
Console.Write("Принята картинка "); var supportedTypes = new[] { "jpg", "png" };
Console.WriteLine(PostPicture.FileName); string receivedFileName = PostPicture.FileName;
return new JsonResult(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("Неверный тип файла");
} }
} }
} }

View File

@ -6,11 +6,7 @@ namespace NeDvachAPI
public class DBchat public class DBchat
{ {
// Obtain connection string information from the portal // 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<Post> DbList() public static List<Post> DbList()
{ {
@ -22,11 +18,11 @@ namespace NeDvachAPI
string connString = string connString =
String.Format( String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
Host, AuthInfo.DB.Host,
User, AuthInfo.DB.User,
DBname, AuthInfo.DB.DBname,
Port, AuthInfo.DB.Port,
Password); AuthInfo.DB.Password);
using (var conn = new NpgsqlConnection(connString)) using (var conn = new NpgsqlConnection(connString))
{ {
@ -42,7 +38,8 @@ namespace NeDvachAPI
// ORDER BY post_id DESC // ORDER BY post_id DESC
// limit 10 // limit 10
// ) // )
// subquery ORDER BY post_id ASC", conn) ) // subquery
// ORDER BY post_id ASC", conn) )
using ( var command = new NpgsqlCommand(@" using ( var command = new NpgsqlCommand(@"
SELECT * FROM SELECT * FROM
( (
@ -86,11 +83,11 @@ namespace NeDvachAPI
string connString = string connString =
String.Format( String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
Host, AuthInfo.DB.Host,
User, AuthInfo.DB.User,
DBname, AuthInfo.DB.DBname,
Port, AuthInfo.DB.Port,
Password); AuthInfo.DB.Password);
using (var conn = new NpgsqlConnection(connString)) using (var conn = new NpgsqlConnection(connString))
{ {
@ -115,11 +112,11 @@ namespace NeDvachAPI
string connString = string connString =
String.Format( String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
Host, AuthInfo.DB.Host,
User, AuthInfo.DB.User,
DBname, AuthInfo.DB.DBname,
Port, AuthInfo.DB.Port,
Password); AuthInfo.DB.Password);
using (var conn = new NpgsqlConnection(connString)) using (var conn = new NpgsqlConnection(connString))
{ {
@ -150,11 +147,11 @@ namespace NeDvachAPI
string connString = string connString =
String.Format( String.Format(
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer", "Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
Host, AuthInfo.DB.Host,
User, AuthInfo.DB.User,
DBname, AuthInfo.DB.DBname,
Port, AuthInfo.DB.Port,
Password); AuthInfo.DB.Password);
using (var conn = new NpgsqlConnection(connString)) using (var conn = new NpgsqlConnection(connString))
{ {

35
MinIOchat.cs Normal file
View File

@ -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<string> 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;
}
}
}

View File

@ -7,8 +7,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Minio" Version="4.0.6" />
<PackageReference Include="Npgsql" Version="6.0.7" /> <PackageReference Include="Npgsql" Version="6.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Buffer\" />
</ItemGroup>
</Project> </Project>

View File

@ -12,8 +12,8 @@ builder.Services.AddCors(setup =>
setup.AddDefaultPolicy(policyBuilder => setup.AddDefaultPolicy(policyBuilder =>
{ {
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("*");
}); });
}); });
@ -31,6 +31,7 @@ if (app.Environment.IsDevelopment())
//app.UseHttpsRedirection(); //app.UseHttpsRedirection();
app.UseCors(); app.UseCors();
app.UseAuthorization(); app.UseAuthorization();

0
newFile.txt Normal file
View File