DocEdit.razor.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using Microsoft.AspNetCore.Components;
  2. using Microsoft.AspNetCore.Components.Authorization;
  3. using Microsoft.AspNetCore.Components.Forms;
  4. using Microsoft.JSInterop;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Reflection;
  9. using System.Threading.Tasks;
  10. using HyperCube.Models;
  11. using Console = HyperCube.Utils.AdvConsole;
  12. using System.Net;
  13. namespace HyperCube.Pages
  14. {
  15. public partial class DocEdit
  16. {
  17. [Parameter]
  18. public int docID { get; set; }
  19. const string FOLDER_NAME = "articles_storage";
  20. const long MAX_FILE_SIZE = 5120000; //bytes
  21. string transactionId;
  22. ArticleModel articleModelClone = new();
  23. ArticleModel articleModel = new();
  24. AccountModel currentAcc = new();
  25. AccountModel initiatorAcc = new();
  26. string status;
  27. string header;
  28. string storageFolderPath;
  29. MemoryStream memoryStream;
  30. Modal modal { get; set; }
  31. int editsCount;
  32. async Task<string> Verify()
  33. {
  34. Console.WriteLine($"Verify starting");
  35. try
  36. {
  37. VerifyContract verifyContract = SmartContract.Find("Verify", Blockchain.GetMain()) as VerifyContract;
  38. if (verifyContract != null)
  39. {
  40. Console.WriteLine($"VerifyContract found");
  41. transactionId = await verifyContract.Run(articleModel);
  42. return transactionId;
  43. }
  44. else
  45. Console.WriteLine($"VerifyContract null");
  46. }
  47. catch (Exception e)
  48. {
  49. Console.WriteLine(e.Message + "stack trace" + e.StackTrace);
  50. }
  51. return "Verify failed";
  52. }
  53. protected override async Task OnInitializedAsync()
  54. {
  55. currentAcc = await GetCurrentAcc();
  56. string path = AppDomain.CurrentDomain.BaseDirectory;
  57. storageFolderPath = (Path.Combine(path, FOLDER_NAME));
  58. if (docID > 0)
  59. {
  60. header = "Проверка материала";
  61. MySQLConnector dbCon = MySQLConnector.Instance();
  62. string stringSQL = $"SELECT articles.id, filename, article_name, authors, date_publish, annotation, keywords, action_type, rating " +
  63. $"FROM articles " +
  64. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  65. $"WHERE articles.id={docID} " +
  66. $"ORDER BY actions_history.id DESC LiMIT 1";
  67. articleModelClone = await dbCon.SQLSelectArticle(stringSQL);
  68. articleModel = (ArticleModel)articleModelClone.Clone();
  69. string initiator = await articleModel.GetInitiatorUUID();
  70. initiatorAcc = AccountModel.Find(initiator);
  71. status = $"Article ID={docID} loaded, status: {articleModel.Status}, initiator: {initiatorAcc.Name}";
  72. }
  73. else
  74. header = "Загрузка материала";
  75. await InitializeAccount();
  76. //int count = await articleModel.GetEditsCount();
  77. //int countbyid = await articleModel.GetEditsCount(currentAcc.UUID);
  78. //header += $", uuid:{currentAcc.UUID}, name: {currentAcc.Name}, edits count:{count}, count by accid: {countbyid}";
  79. }
  80. public async Task<string> NewProjectSmopp(long articleId)
  81. {
  82. WebRequest wrGETURL;
  83. wrGETURL = WebRequest.Create("http://dev.prmsys.net/makeproject.php?pt=232&aid="+ articleId);
  84. var response = await wrGETURL.GetResponseAsync();
  85. Stream dataStream = response.GetResponseStream();
  86. StreamReader reader = new StreamReader(dataStream);
  87. string rt = reader.ReadToEnd();
  88. return rt;
  89. }
  90. private async Task HandleValidSubmit()
  91. {
  92. MySQLConnector dbCon = MySQLConnector.Instance();
  93. long id = 0;
  94. string stringSQL;
  95. Console.WriteLine($"HandleValidSubmit, docID: {docID}");
  96. if (docID > 0)
  97. {
  98. id = docID;
  99. stringSQL = $"UPDATE articles " +
  100. $"SET filename='{articleModel.Filename}', article_name='{articleModel.Name}', authors='{articleModel.Authors}', " +
  101. $"date_publish='{articleModel.PublishDate.ToString("yyyy-MM-dd")}', annotation='{articleModel.Annotation}', " +
  102. $"keywords='{articleModel.Keywords}', rating={articleModel.Rating} " +
  103. $"WHERE id={docID}";
  104. dbCon.SQLInsert(stringSQL);
  105. }
  106. else
  107. {
  108. stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords) " +
  109. $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
  110. $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
  111. id = dbCon.SQLInsert(stringSQL);
  112. NewProjectSmopp(id);
  113. }
  114. ///temp
  115. int action_type = docID > 0 ? 2 : 1;
  116. stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
  117. $"VALUES ('{id}', '{action_type}', '{currentAcc.UUID}')";
  118. dbCon.SQLInsert(stringSQL);
  119. Dictionary<string, PropertyInfo> propDict = Compare.SimpleCompare<ArticleModel>(articleModel, articleModelClone);
  120. foreach (KeyValuePair<string, PropertyInfo> prop in propDict)
  121. {
  122. //Console.WriteLine($"property name: {prop.Key}, value: {prop.Value.GetValue(articleModel, null)}");
  123. stringSQL = $"INSERT INTO articles_edit_log (article_id, acc_id, field_name, field_prevvalue, field_newvalue) " +
  124. $"VALUES ('{id}', '{currentAcc.UUID}', '{prop.Key}', '{prop.Value.GetValue(articleModelClone, null)}', '{prop.Value.GetValue(articleModel, null)}')";
  125. dbCon.SQLInsert(stringSQL);
  126. }
  127. dbCon.Close();
  128. if (docID > 0)
  129. {
  130. status = propDict.Count > 0 ? "All changes saved, article has veryfied." : "Article verifyed without any changes.";
  131. transactionId = await Verify();
  132. Console.WriteLine("transactionId found " + transactionId);
  133. ///tmp
  134. editsCount = await articleModel.GetEditsCount(currentAcc.UUID);
  135. modal.Open();
  136. }
  137. else
  138. {
  139. string fullpath = Path.Combine(storageFolderPath, $"{id}_{articleModel.Filename}");
  140. Directory.CreateDirectory(storageFolderPath);
  141. FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
  142. memoryStream.Position = 0;
  143. await memoryStream.CopyToAsync(fs);
  144. Console.WriteLine($"User has saved new article data, {id}_{articleModel.Filename}, memory size:{memoryStream.Length}b, file size: {fs.Length}b");
  145. memoryStream.Close();
  146. fs.Close();
  147. status = "New article data saved.";
  148. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
  149. if (confirmed)
  150. NavigationManager.NavigateTo("docedit", true);
  151. else
  152. NavigationManager.NavigateTo("");
  153. }
  154. }
  155. private async Task HandleSelection(InputFileChangeEventArgs e)
  156. {
  157. IBrowserFile file = e.File;
  158. if (file != null)
  159. {
  160. Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
  161. memoryStream = new();
  162. await stream.CopyToAsync(memoryStream);
  163. status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
  164. DocParse docParse = new DocParse();
  165. articleModelClone = DocParse.ReadPDF(memoryStream);
  166. articleModelClone.Filename = file.Name;
  167. articleModel = (ArticleModel)articleModelClone.Clone();
  168. }
  169. }
  170. private async Task Cancel()
  171. {
  172. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Вы уверены, что хотите отклонить статью?");
  173. if (confirmed)
  174. {
  175. ///какая-то логика отмены...
  176. NavigationManager.NavigateTo("");
  177. }
  178. }
  179. public async Task InitializeAccount()
  180. {
  181. AccountModel.Current = await GetCurrentAcc();
  182. Console.WriteLine("InitializeAccount in DocEdit " + AccountModel.Current.Name);
  183. }
  184. private async Task<AccountModel> GetCurrentAcc()
  185. {
  186. AccountModel account = new();
  187. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  188. var user = authState.User;
  189. if (user.Identity.IsAuthenticated)
  190. {
  191. var currentUser = await UserManager.GetUserAsync(user);
  192. account.UUID = currentUser.Id;
  193. //account.Name = currentUser.UserName;
  194. //account.Email = currentUser.Email;
  195. var acc = AccountModel.Find(account.UUID);
  196. if (acc != null)
  197. account = acc;
  198. ///tmp
  199. //account.AccRole = Role.User;
  200. return account;
  201. }
  202. return null;
  203. }
  204. }
  205. }