DocEdit.razor 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. @page "/docedit"
  2. @page "/docedit/{docID:int}"
  3. @using HyperCube.Models;
  4. @inject NavigationManager NavigationManager;
  5. @inject IJSRuntime JsRuntime;
  6. @attribute [Authorize]
  7. @*@attribute [Authorize(Roles = "admin")]*@
  8. <EditForm Model="@articleModel" OnValidSubmit="@HandleValidSubmit">
  9. <DataAnnotationsValidator />
  10. <ValidationSummary />
  11. <h1>@header</h1>
  12. <br>
  13. <div style="width: 50%;">
  14. @if (docID < 1)
  15. {
  16. <p><InputFile id="inputDefault" OnChange="@HandleSelection" accept="application/pdf" /></p>
  17. //accept="application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  18. }
  19. else
  20. {
  21. //<p>Исходный документ: <a href="" @onclick="@DownloadDoc" @onclick:preventDefault>@articleModel.Filename</a></p>
  22. <p>Исходный документ: <a href=@FOLDER_NAME download="@articleModel.Filename" target="_top">@articleModel.Filename</a></p>
  23. }
  24. <p><InputText id="article_name" class="form-control" @bind-Value="articleModel.Name" placeholder="Наименование статьи" /></p>
  25. <p><InputDate id="date_publish" class="form-control" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" /></p>
  26. <p><InputText id="author" class="form-control" @bind-Value="articleModel.Authors" placeholder="Автор" /></p>
  27. <p><InputTextArea id="keywords" class="form-control" @bind-Value="articleModel.Keywords" placeholder="Ключевые слова" /></p>
  28. <p><InputTextArea rows="5" id="annotation" class="form-control" @bind-Value="articleModel.Annotation" placeholder="Аннотация" /></p>
  29. <p><InputTextArea rows="10" id="text" class="form-control" @bind-Value="articleModel.Text" placeholder="Текст" /></p>
  30. @if (docID < 1)
  31. {
  32. <p><button class="btn btn-primary" type="submit">Загрузить</button></p>
  33. }
  34. else
  35. {
  36. <p>
  37. <button class="btn btn-danger" type="button" @onclick="@Cancel">Отклонить</button>
  38. <button class="btn btn-primary" type="submit">Утвердить</button>
  39. </p>
  40. }
  41. <p>Статус: @status</p>
  42. </div>
  43. </EditForm>
  44. @code {
  45. [Parameter]
  46. public int docID { get; set; }
  47. const string FOLDER_NAME = "articles_storage";
  48. const long MAX_FILE_SIZE = 5120000; //bytes
  49. ArticleModel articleModel = new ArticleModel();
  50. string status;
  51. string header;
  52. string storageFolderPath;
  53. MemoryStream memoryStream;
  54. protected override async Task OnInitializedAsync()
  55. {
  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 id, filename, article_name, authors, date_publish, annotation, keywords FROM articles WHERE id={docID}";
  63. articleModel = await dbCon.SQLSelectArticle(stringSQL);
  64. status = $"Article ID={docID} loaded.";
  65. }
  66. else
  67. header = "Загрузка материала";
  68. }
  69. private async void HandleValidSubmit()
  70. {
  71. MySQLConnector dbCon = MySQLConnector.Instance();
  72. string stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords)" +
  73. $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
  74. $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
  75. long id = dbCon.SQLInsert(stringSQL);
  76. stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id)" +
  77. $"VALUES ('{id}', {1}, {1})";
  78. dbCon.SQLInsert(stringSQL);
  79. dbCon.Close();
  80. //Console.WriteLine($"memorystream: {memoryStream.Length}, filename: {articleModel.Filename}");
  81. string fullpath = Path.Combine(storageFolderPath, $"{id}_{articleModel.Filename}");
  82. Directory.CreateDirectory(storageFolderPath);
  83. FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
  84. memoryStream.Position = 0;
  85. await memoryStream.CopyToAsync(fs);
  86. Console.WriteLine($"User has saved new article data, {id}_{articleModel.Filename}, memory size:{memoryStream.Length}b, file size: {fs.Length}b");
  87. memoryStream.Close();
  88. fs.Close();
  89. status = "New article data saved.";
  90. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
  91. if (confirmed)
  92. NavigationManager.NavigateTo("docedit", true);
  93. else
  94. NavigationManager.NavigateTo("");
  95. }
  96. private async Task HandleSelection(InputFileChangeEventArgs e)
  97. {
  98. IBrowserFile file = e.File;
  99. if (file != null)
  100. {
  101. Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
  102. memoryStream = new();
  103. await stream.CopyToAsync(memoryStream);
  104. status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
  105. DocParse docParse = new DocParse();
  106. articleModel = DocParse.ReadPDF(memoryStream);
  107. articleModel.Filename = file.Name;
  108. }
  109. }
  110. private async void Cancel()
  111. {
  112. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Вы уверены, что хотите отклонить статью?");
  113. if (confirmed)
  114. {
  115. ///какая-то логика отмены...
  116. NavigationManager.NavigateTo("");
  117. }
  118. }
  119. //private async void DownloadDoc()
  120. //{
  121. // string fullpath = Path.Combine(storageFolderPath, $"{docID}_{articleModel.Filename}");
  122. // if (File.Exists(fullpath))
  123. // {
  124. // //await JsRuntime.InvokeAsync<string>("downloadFile", fullpath);
  125. // }
  126. // else
  127. // {
  128. // Console.WriteLine($"File {fullpath} not found.");
  129. // status = $"File {fullpath} not found.";
  130. // }
  131. //}
  132. }