DocEdit.razor 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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"
  17. accept="application/pdf" /></p>
  18. //accept="application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  19. }
  20. else
  21. {
  22. //<p>Исходный документ: <a href="" @onclick="@DownloadDoc" @onclick:preventDefault>@articleModel.Filename</a></p>
  23. <p>Исходный документ: <a href=@FOLDER_NAME download="@articleModel.Filename" target="_top">@articleModel.Filename</a></p>
  24. }
  25. <p><InputText id="article_name" class="form-control" @bind-Value="articleModel.Name" placeholder="Наименование статьи" /></p>
  26. <p><InputDate id="date_publish" class="form-control" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" /></p>
  27. <p><InputText id="author" class="form-control" @bind-Value="articleModel.Authors" placeholder="Автор" /></p>
  28. <p><InputTextArea id="keywords" class="form-control" @bind-Value="articleModel.Keywords" placeholder="Ключевые слова" /></p>
  29. <p><InputTextArea rows="5" id="annotation" class="form-control" @bind-Value="articleModel.Annotation" placeholder="Аннотация" /></p>
  30. <p><InputTextArea rows="10" id="text" class="form-control" @bind-Value="articleModel.Text" placeholder="Текст" /></p>
  31. @if (docID < 1)
  32. {
  33. <p><button class="btn btn-primary" type="submit">Загрузить</button></p>
  34. }
  35. else
  36. {
  37. <p>
  38. <button class="btn btn-danger" type="button" @onclick="@Cancel">Отклонить</button>
  39. <button class="btn btn-primary" type="submit">Утвердить</button>
  40. </p>
  41. }
  42. <p>Статус: @status</p>
  43. </div>
  44. </EditForm>
  45. @code {
  46. [Parameter]
  47. public int docID { get; set; }
  48. const string FOLDER_NAME = "articles_storage";
  49. const long MAX_FILE_SIZE = 5120000; //bytes
  50. ArticleModel articleModel = new ArticleModel();
  51. string status;
  52. string header;
  53. string storageFolderPath;
  54. MemoryStream memoryStream;
  55. protected override async Task OnInitializedAsync()
  56. {
  57. string path = AppDomain.CurrentDomain.BaseDirectory;
  58. storageFolderPath = (Path.Combine(path, FOLDER_NAME));
  59. if (docID > 0)
  60. {
  61. header = "Проверка материала";
  62. MySQLConnector dbCon = MySQLConnector.Instance();
  63. string stringSQL = $"SELECT id, filename, article_name, authors, date_publish, annotation, keywords FROM articles WHERE id={docID}";
  64. articleModel = await dbCon.SQLSelectArticle(stringSQL);
  65. status = $"Article ID={docID} loaded.";
  66. }
  67. else
  68. header = "Загрузка материала";
  69. }
  70. private async void HandleValidSubmit()
  71. {
  72. MySQLConnector dbCon = MySQLConnector.Instance();
  73. string stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords)" +
  74. $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
  75. $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
  76. long id = dbCon.SQLInsert(stringSQL);
  77. stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id)" +
  78. $"VALUES ('{id}', {1}, {1})";
  79. dbCon.SQLInsert(stringSQL);
  80. dbCon.Close();
  81. //Console.WriteLine($"memorystream: {memoryStream.Length}, filename: {articleModel.Filename}");
  82. string fullpath = Path.Combine(storageFolderPath, $"{id}_{articleModel.Filename}");
  83. Directory.CreateDirectory(storageFolderPath);
  84. FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
  85. await memoryStream.CopyToAsync(fs);
  86. memoryStream.Close();
  87. fs.Close();
  88. Console.WriteLine($"User has saved new article data, {id}_{articleModel.Filename}");
  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. }