DocEdit.razor.cs 10.0 KB

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