DocEdit.razor.cs 10 KB

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