Merge branch 'master' of http://git.vdk2ch.ru:3000/RakVhalate/2chBackAPI
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
commit
2b1a229623
@ -22,6 +22,7 @@ steps:
|
||||
commands:
|
||||
- cd /usr/share/$DRONE_REPO_NAME
|
||||
- dotnet build
|
||||
|
||||
- name: recreate dist folder
|
||||
commands:
|
||||
- rm -rf /usr/share/$DRONE_REPO_NAME/dist
|
||||
|
12
.idea/NeDvachAPI.iml
Normal file
12
.idea/NeDvachAPI.iml
Normal file
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/NeDvachAPI.iml" filepath="$PROJECT_DIR$/.idea/NeDvachAPI.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
45
.idea/workspace.xml
Normal file
45
.idea/workspace.xml
Normal file
@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="afeb71ff-aaed-4a5b-8ece-55d4f78fc0c7" name="Changes" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="Git.Settings">
|
||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="MarkdownSettingsMigration">
|
||||
<option name="stateVersion" value="1" />
|
||||
</component>
|
||||
<component name="ProjectId" id="2HppPxAx0SVc4HMyQfWgDlfmGn2" />
|
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"WebServerToolWindowFactoryState": "false",
|
||||
"vue.rearranger.settings.migration": "true"
|
||||
}
|
||||
}]]></component>
|
||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="Default task">
|
||||
<changelist id="afeb71ff-aaed-4a5b-8ece-55d4f78fc0c7" name="Changes" comment="" />
|
||||
<created>1668991859805</created>
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1668991859805</updated>
|
||||
<workItem from="1668991862249" duration="12000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
<option name="version" value="3" />
|
||||
</component>
|
||||
</project>
|
Binary file not shown.
Binary file not shown.
1026
.vs/NeDvachAPI/config/applicationhost.config
Normal file
1026
.vs/NeDvachAPI/config/applicationhost.config
Normal file
File diff suppressed because it is too large
Load Diff
BIN
.vs/NeDvachAPI/v17/.wsuo
Normal file
BIN
.vs/NeDvachAPI/v17/.wsuo
Normal file
Binary file not shown.
@ -1,8 +1,7 @@
|
||||
{
|
||||
"ExpandedNodes": [
|
||||
"",
|
||||
"\\Controllers"
|
||||
""
|
||||
],
|
||||
"SelectedNode": "\\Program.cs",
|
||||
"SelectedNode": "\\appsettings.json",
|
||||
"PreviewInSolutionExplorer": false
|
||||
}
|
BIN
.vs/slnx.sqlite
BIN
.vs/slnx.sqlite
Binary file not shown.
@ -1,6 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Data;
|
||||
using System.Text.Json;
|
||||
using NeDvachAPI.DBControllers;
|
||||
|
||||
namespace NeDvachAPI.Controllers
|
||||
{
|
21
APIControllers/GetPosts.cs
Normal file
21
APIControllers/GetPosts.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NeDvachAPI.BufferControllers;
|
||||
using System.Text.Json;
|
||||
using System.Web.Http;
|
||||
using HttpGetAttribute = Microsoft.AspNetCore.Mvc.HttpGetAttribute;
|
||||
using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;
|
||||
|
||||
namespace NeDvachAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class ListController : ControllerBase
|
||||
{
|
||||
[HttpGet(Name = "GetPosts")]
|
||||
public string Get([FromUri] string board, int thread)
|
||||
{
|
||||
string postsJson = JsonSerializer.Serialize(APIThreadBuffer.GetThread(thread));
|
||||
return postsJson ;
|
||||
}
|
||||
}
|
||||
}
|
50
APIControllers/PictureUpload.cs
Normal file
50
APIControllers/PictureUpload.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NeDvachAPI.DBControllers;
|
||||
|
||||
|
||||
namespace NeDvachAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class UploadPic : ControllerBase
|
||||
{
|
||||
|
||||
|
||||
[HttpPost(Name = "UploadPicture")]
|
||||
public async Task<JsonResult> ReceivePost([FromForm] IFormFile PostPicture)
|
||||
{
|
||||
|
||||
string receivedFileName = PostPicture.FileName;
|
||||
string fName = Path.GetFileNameWithoutExtension(receivedFileName);
|
||||
string fExtention = Path.GetExtension(receivedFileName);
|
||||
string previevFileName = fName + 's' + fExtention;
|
||||
|
||||
Console.WriteLine("Расширение файла: " + fExtention);
|
||||
|
||||
if (ImageController.CheckExtention(fExtention)) //file type check
|
||||
{
|
||||
///Local Buffer File Part
|
||||
string filePath;
|
||||
string previevPath;
|
||||
filePath = Directory.GetCurrentDirectory() + "\\Buffer\\" + receivedFileName;
|
||||
previevPath = Directory.GetCurrentDirectory() + "\\Buffer\\" + previevFileName;
|
||||
//creating original file buffer
|
||||
Stream picBuffer = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
|
||||
PostPicture.CopyTo(picBuffer);
|
||||
picBuffer.Close();
|
||||
//resizing
|
||||
ImageController.ResizeImage(filePath, previevPath, 300, 400);
|
||||
///MinIo part
|
||||
await MinIOchat.PictureUpload(previevPath, receivedFileName, "thread-pics-small"); //upload thumbnail
|
||||
JsonResult picAdress = new(await MinIOchat.PictureUpload(filePath, receivedFileName, "thread-pics")); //upload fullsize
|
||||
Console.WriteLine("Загружен файл:" + "http://static.vdk2ch.ru:15555/thread-pics/" + receivedFileName);
|
||||
return picAdress;
|
||||
}
|
||||
|
||||
else return new JsonResult("Неверный тип файла");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
34
APIControllers/PostPost.cs
Normal file
34
APIControllers/PostPost.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NeDvachAPI.BufferControllers;
|
||||
using NeDvachAPI.DBControllers;
|
||||
using NeDvachAPI.Models;
|
||||
|
||||
namespace NeDvachAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class PostToController : ControllerBase
|
||||
{
|
||||
[HttpPost(Name = "PostPosts")]
|
||||
public JsonResult ReceivePost([FromBody] Post ReceivedPost)
|
||||
{
|
||||
string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
|
||||
ReceivedPost.Ip = ipAddress;
|
||||
|
||||
if(ReceivedPost.Text.Length == 0)
|
||||
{
|
||||
return new JsonResult("Вы отправили пустое сообщение!");
|
||||
}
|
||||
try
|
||||
{
|
||||
DBchat.SendPost(ReceivedPost);
|
||||
APIThreadBuffer.RefreshThread(ReceivedPost.Thread_Id);
|
||||
return new JsonResult("Сообщение успешно добавлено.");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new JsonResult("Произошла ошибка постинга");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
21
APIControllers/ShowThreads.cs
Normal file
21
APIControllers/ShowThreads.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NeDvachAPI.BufferControllers;
|
||||
using System.Text.Json;
|
||||
using System.Web.Http;
|
||||
using HttpGetAttribute = Microsoft.AspNetCore.Mvc.HttpGetAttribute;
|
||||
using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;
|
||||
|
||||
namespace NeDvachAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class ThreadsController : ControllerBase
|
||||
{
|
||||
[HttpGet(Name = "GetThreads")]
|
||||
public string Get([FromUri] string board)
|
||||
{
|
||||
string postsJson = JsonSerializer.Serialize(APIThreadBuffer.GetThreadPreviews());
|
||||
return postsJson;
|
||||
}
|
||||
}
|
||||
}
|
24
AuthInfo.cs
Normal file
24
AuthInfo.cs
Normal file
@ -0,0 +1,24 @@
|
||||
namespace NeDvachAPI
|
||||
{
|
||||
public class AuthInfo
|
||||
{
|
||||
public class MinIo
|
||||
{
|
||||
public static string endpoint = "static.vdk2ch.ru:15555";
|
||||
public static string bucketName = "thread-pics thread-pics-small";
|
||||
public static string username = "admin";
|
||||
public static string password = "password2ch";
|
||||
}
|
||||
|
||||
public class DB
|
||||
{
|
||||
public static string Host = "postgres.vdk2ch.ru";
|
||||
public static string User = "postgres";
|
||||
public static string DBname = "dvaach";
|
||||
public static string Password = "postgres";
|
||||
public static string Port = "5432";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
82
BufferControllers/APIThreadBuffer.cs
Normal file
82
BufferControllers/APIThreadBuffer.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using NeDvachAPI.DBControllers;
|
||||
using NeDvachAPI.Models;
|
||||
|
||||
namespace NeDvachAPI.BufferControllers
|
||||
{
|
||||
public class APIThreadBuffer
|
||||
{
|
||||
private static List<Post>[] ThreadsPrewievs = new List<Post>[0];
|
||||
private static List<Post>[] Threads = new List<Post>[0];
|
||||
public static void WriteThreadPreviewsBuffer(List<Post> OPPostsToWrite) //add list of OP-posts and last post to buffer
|
||||
{
|
||||
for(int pos = 0; pos < OPPostsToWrite.Count; pos ++)
|
||||
{
|
||||
var deb = new List<Post>();
|
||||
deb.Add(OPPostsToWrite[pos]);
|
||||
ThreadsPrewievs = ThreadsPrewievs.Append(deb).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static void AppendToThreadPreviews(int index,Post toAppend)
|
||||
{
|
||||
ThreadsPrewievs[index].Add(toAppend);
|
||||
}
|
||||
|
||||
public static List<Post>[] GetThreadPreviews()
|
||||
{
|
||||
return ThreadsPrewievs;
|
||||
}
|
||||
|
||||
public static void UpdateThreadsPreviews(int threadId)
|
||||
{
|
||||
Console.WriteLine("Пытаюсь обновить превью треда " + threadId);
|
||||
|
||||
var newPreview = new List<Post>();
|
||||
newPreview.Add(ThreadsPrewievs[threadId -1][0]);
|
||||
int targetLength = APIThreadBuffer.GetThreadLength(threadId - 1);
|
||||
for (int lastofthree = targetLength- 2; lastofthree <= targetLength; lastofthree ++)
|
||||
{
|
||||
newPreview.Add(GetSinglePost(threadId - 1,lastofthree));
|
||||
}
|
||||
ThreadsPrewievs[threadId -1] = newPreview;
|
||||
}
|
||||
|
||||
public static void AppendThreadsBuffer(List<Post> ThreadToAdd) //add thread's posts from DB to buffer
|
||||
{
|
||||
Threads = Threads.Append(ThreadToAdd).ToArray();
|
||||
}
|
||||
|
||||
public static int GetThreadLength(int threadId)
|
||||
{
|
||||
return Threads[threadId].Count;
|
||||
}
|
||||
|
||||
public static List<Post> GetThread(int threadId)
|
||||
{
|
||||
if (threadId < Threads.Length + 1 & threadId != 0)
|
||||
{
|
||||
return Threads[threadId - 1];
|
||||
}
|
||||
else return null;
|
||||
}
|
||||
|
||||
public static Post GetSinglePost(int thread, int postPosition)
|
||||
{
|
||||
List<Post> postsList = Threads[thread];
|
||||
return postsList[postPosition-1];
|
||||
}
|
||||
|
||||
public static void UpdateThreadPosts(int threadId)
|
||||
{
|
||||
Console.WriteLine("Пытаюсь обновить тред " + threadId);
|
||||
Threads[threadId - 1] = DBchat.PostsList("b", threadId);
|
||||
}
|
||||
|
||||
public static void RefreshThread(int threadId)
|
||||
{
|
||||
UpdateThreadPosts(threadId);
|
||||
UpdateThreadsPreviews(threadId);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
15
BufferControllers/AccessController.cs
Normal file
15
BufferControllers/AccessController.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using NeDvachAPI.Models;
|
||||
using Npgsql;
|
||||
|
||||
namespace NeDvachAPI.DBControllers
|
||||
{
|
||||
public class BanList
|
||||
{
|
||||
public static string[] GetBanned()
|
||||
{
|
||||
string[] bannedIPs = new string[0];
|
||||
//
|
||||
return bannedIPs;
|
||||
}
|
||||
}
|
||||
}
|
33
BufferControllers/BufferFill.cs
Normal file
33
BufferControllers/BufferFill.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using NeDvachAPI.DBControllers;
|
||||
using NeDvachAPI.Models;
|
||||
|
||||
namespace NeDvachAPI.BufferControllers
|
||||
{
|
||||
public class BufferFill
|
||||
{
|
||||
|
||||
public static bool FillBuffer(string board) //method to get buffer of information, which is being ran while API is started
|
||||
{
|
||||
List<Post> Previews = DBchat.ThreadsList(board);
|
||||
APIThreadBuffer.WriteThreadPreviewsBuffer(Previews);
|
||||
Console.WriteLine("Adding OP-posts to buffer");
|
||||
|
||||
for (int threadId = 1; threadId <= Previews.Count; threadId++)
|
||||
{
|
||||
APIThreadBuffer.AppendThreadsBuffer(DBchat.PostsList(board, threadId));
|
||||
}
|
||||
Console.WriteLine($@"Buffering threads {1} to {Previews.Count}.");
|
||||
|
||||
for (int preview = 0; preview < Previews.Count; preview++)
|
||||
{
|
||||
int targetLength = APIThreadBuffer.GetThreadLength(preview);
|
||||
for (int lastofthree = targetLength- 2; lastofthree <= targetLength; lastofthree ++)
|
||||
{
|
||||
APIThreadBuffer.AppendToThreadPreviews(preview,APIThreadBuffer.GetSinglePost(preview,lastofthree));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace NeDvachAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class ListController : ControllerBase
|
||||
{
|
||||
[HttpGet(Name = "GetPosts")]
|
||||
public string Get()
|
||||
{
|
||||
|
||||
//Post[] posts = DBchat.DbList();
|
||||
List<Post> posts = DBchat.DbList();
|
||||
string postsJson = JsonSerializer.Serialize(posts);
|
||||
Console.WriteLine("Запрошен список постов");
|
||||
return postsJson ;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace NeDvachAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class UploadPic : ControllerBase
|
||||
{
|
||||
[HttpPost(Name = "UploadPicture")]
|
||||
public JsonResult ReceivePost([FromForm] IFormFile PostPicture)
|
||||
{
|
||||
Console.Write("Принята картинка ");
|
||||
Console.WriteLine(PostPicture.FileName);
|
||||
return new JsonResult(PostPicture.FileName + " получен!");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace NeDvachAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class PostToController : ControllerBase
|
||||
{
|
||||
[HttpPost(Name = "PostPosts")]
|
||||
public JsonResult ReceivePost([FromBody] Post ReceivedPost)
|
||||
{
|
||||
Console.WriteLine("Принят пост");
|
||||
DBchat.SendPost(ReceivedPost);
|
||||
return new JsonResult("Posted Successfully!");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace NeDvachAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class TestPostingController : ControllerBase
|
||||
{
|
||||
[HttpPost]
|
||||
public int Area([FromForm] int height , int altitude)
|
||||
{
|
||||
//DBchat.SendPost(ReceivedPost);
|
||||
Console.WriteLine("Received! " + height + " " + altitude );
|
||||
return 200;
|
||||
}
|
||||
}
|
||||
}
|
211
DBControllers/DBchat.cs
Normal file
211
DBControllers/DBchat.cs
Normal file
@ -0,0 +1,211 @@
|
||||
using NeDvachAPI.Models;
|
||||
using Npgsql;
|
||||
|
||||
namespace NeDvachAPI.DBControllers
|
||||
{
|
||||
public class DBchat
|
||||
{
|
||||
public static List<Post> PostsList(string boardName, int thread)
|
||||
{
|
||||
List<Post> posts = new List<Post>();
|
||||
string connString =
|
||||
string.Format(
|
||||
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
||||
AuthInfo.DB.Host,
|
||||
AuthInfo.DB.User,
|
||||
AuthInfo.DB.DBname,
|
||||
AuthInfo.DB.Port,
|
||||
AuthInfo.DB.Password);
|
||||
|
||||
using (var conn = new NpgsqlConnection(connString))
|
||||
{
|
||||
|
||||
|
||||
conn.Open();
|
||||
using (var command = new NpgsqlCommand($@"
|
||||
SELECT * FROM
|
||||
(
|
||||
SELECT post_id, post_text, content, post_timestamp
|
||||
FROM posts
|
||||
WHERE thread_id = {thread}
|
||||
ORDER BY post_id DESC
|
||||
) subquery
|
||||
ORDER BY post_id ASC", conn))
|
||||
{
|
||||
|
||||
var reader = command.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
Post receivedPost = new()
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Text = reader.GetString(1),
|
||||
ImgURL = reader.IsDBNull(2) ? null : reader.GetFieldValue<string[]>(2),
|
||||
Timestamp = reader.GetString(3)
|
||||
|
||||
};
|
||||
posts.Add(receivedPost);
|
||||
}
|
||||
reader.Close();
|
||||
}
|
||||
}
|
||||
return posts;
|
||||
}
|
||||
|
||||
public static List<Post> ThreadsList(string boardName)
|
||||
{
|
||||
List<Post> posts = new List<Post>();
|
||||
string connString =
|
||||
string.Format(
|
||||
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
||||
AuthInfo.DB.Host,
|
||||
AuthInfo.DB.User,
|
||||
AuthInfo.DB.DBname,
|
||||
AuthInfo.DB.Port,
|
||||
AuthInfo.DB.Password);
|
||||
|
||||
using (var conn = new NpgsqlConnection(connString))
|
||||
{
|
||||
|
||||
|
||||
conn.Open();
|
||||
|
||||
using (var command = new NpgsqlCommand($@"
|
||||
SELECT * FROM
|
||||
(
|
||||
SELECT post_id, post_text, content, post_timestamp, is_op, thread_id
|
||||
FROM posts
|
||||
WHERE
|
||||
is_op = {true}
|
||||
ORDER BY thread_id DESC
|
||||
) subquery
|
||||
ORDER BY thread_id ASC", conn))
|
||||
{
|
||||
|
||||
var reader = command.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
Post receivedPost = new()
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Text = reader.GetString(1),
|
||||
ImgURL = reader.IsDBNull(2) ? null : reader.GetFieldValue<string[]>(2),
|
||||
Timestamp = reader.GetString(3),
|
||||
Is_OP = reader.GetBoolean(4),
|
||||
Thread_Id = reader.GetInt32(5)
|
||||
|
||||
};
|
||||
posts.Add(receivedPost);
|
||||
}
|
||||
reader.Close();
|
||||
}
|
||||
}
|
||||
return posts;
|
||||
}
|
||||
|
||||
public static void DbUpdate(string id, string text)
|
||||
{
|
||||
string connString =
|
||||
string.Format(
|
||||
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
||||
AuthInfo.DB.Host,
|
||||
AuthInfo.DB.User,
|
||||
AuthInfo.DB.DBname,
|
||||
AuthInfo.DB.Port,
|
||||
AuthInfo.DB.Password);
|
||||
|
||||
using (var conn = new NpgsqlConnection(connString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (var command = new NpgsqlCommand("UPDATE dvach " +
|
||||
"SET post = @postText WHERE post_id = @ID", conn))
|
||||
{
|
||||
command.Parameters.AddWithValue("ID", int.Parse(id));
|
||||
command.Parameters.AddWithValue("postText", text);
|
||||
|
||||
int nRows = command.ExecuteNonQuery();
|
||||
Console.Out.WriteLine(string.Format("Number of rows updated={0}", nRows));
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Данные обновлены!");
|
||||
Console.ReadLine();
|
||||
}
|
||||
public static void SendPost(Post postToSend) //sending post to database
|
||||
{
|
||||
string connString =
|
||||
string.Format(
|
||||
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
||||
AuthInfo.DB.Host,
|
||||
AuthInfo.DB.User,
|
||||
AuthInfo.DB.DBname,
|
||||
AuthInfo.DB.Port,
|
||||
AuthInfo.DB.Password);
|
||||
|
||||
using (var conn = new NpgsqlConnection(connString))
|
||||
{
|
||||
conn.Open();
|
||||
int postNum = 14;
|
||||
|
||||
using (var command = new NpgsqlCommand(@"
|
||||
INSERT INTO posts (post_id, post_text, post_number, content, thread_id, auth_ip, is_op, post_timestamp)
|
||||
VALUES (
|
||||
DEFAULT,
|
||||
@postText,
|
||||
@postNum,
|
||||
@postImgUrl,
|
||||
@threadId,
|
||||
@ip,
|
||||
false,
|
||||
(
|
||||
SELECT date_trunc(
|
||||
'second',
|
||||
(now()::timestamp(0) AT TIME ZONE 'UTC+10')::TIMESTAMP
|
||||
)
|
||||
) )", conn))
|
||||
{
|
||||
command.Parameters.AddWithValue("postText", postToSend.Text);
|
||||
command.Parameters.AddWithValue("postNum", postNum);
|
||||
command.Parameters.AddWithValue("postImgUrl", postToSend.ImgURL);
|
||||
command.Parameters.AddWithValue("threadId", postToSend.Thread_Id);
|
||||
command.Parameters.AddWithValue("ip", postToSend.Ip);
|
||||
int nRows = command.ExecuteNonQuery();
|
||||
Console.Out.WriteLine("Добавлен пост с текстом " + postToSend.Text + " номером " + 22);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public static void DeletePost(string idToDel) //удалялка
|
||||
{
|
||||
string connString =
|
||||
string.Format(
|
||||
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
||||
AuthInfo.DB.Host,
|
||||
AuthInfo.DB.User,
|
||||
AuthInfo.DB.DBname,
|
||||
AuthInfo.DB.Port,
|
||||
AuthInfo.DB.Password);
|
||||
|
||||
using (var conn = new NpgsqlConnection(connString))
|
||||
{
|
||||
|
||||
conn.Open();
|
||||
|
||||
using (var command = new NpgsqlCommand(@"
|
||||
DELETE FROM dvach
|
||||
WHERE post_id = @ID", conn))
|
||||
{
|
||||
command.Parameters.AddWithValue("ID", int.Parse(idToDel)); ;
|
||||
|
||||
int nRows = command.ExecuteNonQuery();
|
||||
Console.Out.WriteLine(string.Format("Number of rows updated={0}", nRows));
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Данные удалены!");
|
||||
Console.ReadLine();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
43
DBControllers/ImageController.cs
Normal file
43
DBControllers/ImageController.cs
Normal file
@ -0,0 +1,43 @@
|
||||
|
||||
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 bitmap = SKBitmap.Decode(picPath);
|
||||
|
||||
double ratio = Math.Max((double)bitmap.Width / (double)width , (double)bitmap.Height / (double)height);
|
||||
int targetWidth = (int)(bitmap.Width/ ratio);
|
||||
int targetHeight = (int)(bitmap.Height / ratio);
|
||||
|
||||
Console.WriteLine($"Ресайзим картинку до {targetWidth} ширины и {targetHeight} высоты.");
|
||||
|
||||
var dstInfo = new SKImageInfo(targetWidth , targetHeight);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
36
DBControllers/MinIOchat.cs
Normal file
36
DBControllers/MinIOchat.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Minio.DataModel;
|
||||
using Minio;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace NeDvachAPI.DBControllers
|
||||
{
|
||||
public class MinIOchat
|
||||
{
|
||||
public static async Task<JsonResult> PictureUpload(string fileroute, string filename, string bucket)
|
||||
{
|
||||
MinioClient DvachIo = new MinioClient()
|
||||
.WithEndpoint(AuthInfo.MinIo.endpoint)
|
||||
.WithCredentials(AuthInfo.MinIo.username, AuthInfo.MinIo.password)
|
||||
//.WithSSL()
|
||||
.Build();
|
||||
|
||||
Aes aesEncryption = Aes.Create();
|
||||
aesEncryption.KeySize = 256;
|
||||
aesEncryption.GenerateKey();
|
||||
var ssec = new SSEC(aesEncryption.Key);
|
||||
PutObjectArgs putObjectArgs = new PutObjectArgs()
|
||||
.WithBucket(bucket)
|
||||
.WithObject(filename)
|
||||
.WithFileName(fileroute)
|
||||
.WithContentType("image/png");
|
||||
//.WithServerSideEncryption(ssec);
|
||||
await DvachIo.PutObjectAsync(putObjectArgs);
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
File.Delete(fileroute);
|
||||
|
||||
return new JsonResult("http://static.vdk2ch.ru:15555/thread-pics/" + filename);
|
||||
}
|
||||
}
|
||||
}
|
181
DBchat.cs
181
DBchat.cs
@ -1,181 +0,0 @@
|
||||
using Npgsql;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
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<Post> DbList()
|
||||
{
|
||||
// Build connection string using parameters from portal
|
||||
//Post[] posts = new Post[10];
|
||||
List<Post> posts = new List<Post>();
|
||||
//int postCount = 0;
|
||||
string received = "";
|
||||
string connString =
|
||||
String.Format(
|
||||
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
||||
Host,
|
||||
User,
|
||||
DBname,
|
||||
Port,
|
||||
Password);
|
||||
|
||||
using (var conn = new NpgsqlConnection(connString))
|
||||
{
|
||||
|
||||
|
||||
conn.Open();
|
||||
|
||||
|
||||
// using ( var command = new NpgsqlCommand(@"
|
||||
// SELECT * FROM
|
||||
// (SELECT post_id, substring(post,1,200)
|
||||
// FROM dvach
|
||||
// ORDER BY post_id DESC
|
||||
// limit 10
|
||||
// )
|
||||
// subquery ORDER BY post_id ASC", conn) )
|
||||
using ( var command = new NpgsqlCommand(@"
|
||||
SELECT * FROM
|
||||
(
|
||||
SELECT post_id, post, post_timestamp, post_pic
|
||||
FROM dvach
|
||||
ORDER BY post_id DESC
|
||||
) subquery
|
||||
ORDER BY post_id ASC", conn) )
|
||||
{
|
||||
|
||||
var reader = command.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
//received +=(
|
||||
//string.Format(
|
||||
//"#{0}: {1}" + "\n",
|
||||
// reader.GetInt32(0).ToString(),
|
||||
//reader.GetString(1)
|
||||
//)
|
||||
//);
|
||||
Post receivedPost = new()
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Text = reader.GetString(1),
|
||||
Timestamp = reader.GetString(2),
|
||||
ImgURL = (string)reader.GetString(3)
|
||||
|
||||
};
|
||||
posts.Add(receivedPost);
|
||||
//posts[postCount] = receivedPost;
|
||||
//postCount++;
|
||||
}
|
||||
reader.Close();
|
||||
}
|
||||
}
|
||||
return posts;
|
||||
}
|
||||
|
||||
public static void DbUpdate(string id, string text)
|
||||
{
|
||||
string connString =
|
||||
String.Format(
|
||||
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
||||
Host,
|
||||
User,
|
||||
DBname,
|
||||
Port,
|
||||
Password);
|
||||
|
||||
using (var conn = new NpgsqlConnection(connString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (var command = new NpgsqlCommand("UPDATE dvach " +
|
||||
"SET post = @postText WHERE post_id = @ID", conn))
|
||||
{
|
||||
command.Parameters.AddWithValue("ID", int.Parse(id));
|
||||
command.Parameters.AddWithValue("postText", text);
|
||||
|
||||
int nRows = command.ExecuteNonQuery();
|
||||
Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Данные обновлены!");
|
||||
Console.ReadLine();
|
||||
}
|
||||
public static void SendPost(Post postToSend) //добавлялка
|
||||
{
|
||||
string connString =
|
||||
String.Format(
|
||||
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
||||
Host,
|
||||
User,
|
||||
DBname,
|
||||
Port,
|
||||
Password);
|
||||
|
||||
using (var conn = new NpgsqlConnection(connString))
|
||||
{
|
||||
|
||||
Console.Out.WriteLine("Opening connection");
|
||||
conn.Open();
|
||||
|
||||
using (var command = new NpgsqlCommand(@"
|
||||
INSERT INTO dvach (post_id, post, post_timestamp)
|
||||
VALUES ( DEFAULT, @postText, (
|
||||
SELECT date_trunc(
|
||||
'second',
|
||||
(now()::timestamp(0) AT TIME ZONE 'UTC+10')::TIMESTAMP
|
||||
)
|
||||
) )", conn))
|
||||
{
|
||||
command.Parameters.AddWithValue("postText", postToSend.Text);
|
||||
//command.Parameters.AddWithValue("postTimeStamp", DateTime.Now.ToString("dd/MM/yyyy HH:mm"));
|
||||
|
||||
int nRows = command.ExecuteNonQuery();
|
||||
Console.Out.WriteLine("Добавлен пост");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public static void DeletePost(string idToDel) //удалялка
|
||||
{
|
||||
string connString =
|
||||
String.Format(
|
||||
"Server={0}; User Id={1}; Database={2}; Port={3}; Password={4};SSLMode=Prefer",
|
||||
Host,
|
||||
User,
|
||||
DBname,
|
||||
Port,
|
||||
Password);
|
||||
|
||||
using (var conn = new NpgsqlConnection(connString))
|
||||
{
|
||||
|
||||
//Console.Out.WriteLine("Opening connection");
|
||||
conn.Open();
|
||||
|
||||
using (var command = new NpgsqlCommand(@"
|
||||
DELETE FROM dvach
|
||||
WHERE post_id = @ID", conn))
|
||||
{
|
||||
command.Parameters.AddWithValue("ID", int.Parse(idToDel)); ;
|
||||
|
||||
int nRows = command.ExecuteNonQuery();
|
||||
Console.Out.WriteLine(String.Format("Number of rows updated={0}", nRows));
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Данные удалены!");
|
||||
Console.ReadLine();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:6.0
|
||||
|
||||
|
||||
|
||||
ADD dist /app
|
||||
|
||||
WORKDIR /app
|
||||
|
14
Models/Post.cs
Normal file
14
Models/Post.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace NeDvachAPI.Models
|
||||
{
|
||||
public class Post
|
||||
{
|
||||
public string Timestamp { get; set; }
|
||||
public int Id { get; set; }
|
||||
public string Text { get; set; }
|
||||
public string[]? ImgURL { get; set; }
|
||||
public int Thread_Id { get; set; }
|
||||
public bool Is_OP { get; set; }
|
||||
public bool Is_Deleted { get; set; }
|
||||
public string? Ip { get; set; }
|
||||
}
|
||||
}
|
@ -7,8 +7,17 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Npgsql" Version="6.0.7" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.9" />
|
||||
<PackageReference Include="Minio" Version="4.0.6" />
|
||||
<PackageReference Include="Npgsql" Version="7.0.0" />
|
||||
<PackageReference Include="SkiaSharp" Version="2.88.3" />
|
||||
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Buffer\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -3,5 +3,9 @@
|
||||
<PropertyGroup>
|
||||
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
|
||||
<Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath>
|
||||
<ActiveDebugProfile>NeDvachAPI</ActiveDebugProfile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
13
Post.cs
13
Post.cs
@ -1,13 +0,0 @@
|
||||
namespace NeDvachAPI
|
||||
{
|
||||
public class Post
|
||||
{
|
||||
public string? Timestamp { get; set; }
|
||||
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Text { get; set; }
|
||||
|
||||
public string ImgURL { get; set; }
|
||||
}
|
||||
}
|
23
Program.cs
23
Program.cs
@ -1,12 +1,11 @@
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
using NeDvachAPI.BufferControllers;
|
||||
|
||||
// Add services to the container.
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
|
||||
builder.Services.AddCors(setup =>
|
||||
{
|
||||
setup.AddDefaultPolicy(policyBuilder =>
|
||||
@ -19,18 +18,12 @@ builder.Services.AddCors(setup =>
|
||||
|
||||
});
|
||||
|
||||
//builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
//app.UseSwagger();
|
||||
//app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
}
|
||||
|
||||
app.UseCors();
|
||||
|
||||
@ -38,4 +31,12 @@ app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
if (BufferFill.FillBuffer("b"))
|
||||
{
|
||||
Console.WriteLine("Ready for work.");
|
||||
app.Run();
|
||||
}
|
||||
else Console.WriteLine("Ошибка буферизации, проверьте доступность базы данных.");
|
||||
|
||||
|
@ -1,4 +1,22 @@
|
||||
{
|
||||
{
|
||||
"profiles": {
|
||||
"NeDvachAPI": {
|
||||
"commandName": "Project",
|
||||
"launchUrl": "Home",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://0.0.0.0:7141"
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchUrl": "Home",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
},
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
@ -7,25 +25,5 @@
|
||||
"applicationUrl": "http://0.0.0.0:31551",
|
||||
"sslPort": 44346
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"NeDvachAPI": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "Home",
|
||||
"applicationUrl": "http://0.0.0.0:7141;http://0.0.0.0:5141",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "Home",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
{
|
||||
"https_port": 443,
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
|
@ -10,3 +10,8 @@ services:
|
||||
restart: always
|
||||
ports:
|
||||
- "5000:80"
|
||||
logging:
|
||||
driver: "gelf"
|
||||
options:
|
||||
gelf-address: "udp://graylog.vdk2ch.ru:12201"
|
||||
tag: "backend-logs"
|
||||
|
5
libman.json
Normal file
5
libman.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"version": "1.0",
|
||||
"defaultProvider": "cdnjs",
|
||||
"libraries": []
|
||||
}
|
0
newFile.txt
Normal file
0
newFile.txt
Normal file
Reference in New Issue
Block a user