Browse Source

file hash&updating article list fixes

ganahrhr 3 years ago
parent
commit
940db43877
5 changed files with 57 additions and 22 deletions
  1. 1 0
      Models/ArticleModel.cs
  2. 2 3
      Pages/DocEdit.razor
  3. 48 17
      Pages/DocEdit.razor.cs
  4. 0 2
      Pages/Verifying.razor
  5. 6 0
      Pages/Verifying.razor.cs

+ 1 - 0
Models/ArticleModel.cs

@@ -25,6 +25,7 @@ namespace HyperCube.Models
     {
         public int ID { get; set; }
         public string Filename { get; set; }
+        public string FileHashSum { get; set; }
         public string FilenameReal { get { return ID + "_" + Filename; } }
         [Required]        
         public string Name { get; set; }        

+ 2 - 3
Pages/DocEdit.razor

@@ -44,9 +44,8 @@
                 <InputTextArea class="form-control upload__textarea" id="text" @bind-Value="articleModel.Text" placeholder="Текст статьи" /> @*rows="10"*@
 
                 @if (docID < 1)
-                {
-                    @*<p><button class="btn btn-primary" type="submit">Загрузить</button></p>*@
-                    <button type="button" class="upload__btn" @onclick="@HandleValidSubmit">загрузить на верификацию</button>
+                {                    
+                    <button type="button" class="upload__btn" @onclick="@HandleValidSubmit" disabled=@IsSubmitDisabled>загрузить на верификацию</button>
                 }
                 else
                 {

+ 48 - 17
Pages/DocEdit.razor.cs

@@ -7,10 +7,10 @@ using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
 using System.Threading.Tasks;
+using System.Net;
+using System.Security.Cryptography;
 using HyperCube.Models;
 using Console = HyperCube.Utils.AdvConsole;
-using System.Net;
-
 
 namespace HyperCube.Pages
 {
@@ -35,6 +35,8 @@ namespace HyperCube.Pages
         string fullName { get { return FOLDER_NAME + "/" + articleModel.FilenameReal; } }
         int editsCount;
 
+        bool IsSubmitDisabled = false;
+
         async Task<string> Verify()
         {
             Console.WriteLine($"Verify starting");
@@ -82,7 +84,6 @@ namespace HyperCube.Pages
                 string initiator = await articleModel.GetInitiatorUUID();
                 initiatorAcc = AccountModel.Find(initiator);
                 status = $"Article ID={docID} loaded, status: {articleModel.Status}, initiator: {initiatorAcc.Name}";
-
             }
             else
                 header = "Загрузка материала";
@@ -102,16 +103,14 @@ namespace HyperCube.Pages
             var response = await wrGETURL.GetResponseAsync();
             Stream dataStream = response.GetResponseStream();
 
-            StreamReader reader = new StreamReader(dataStream);
-
-            string rt = reader.ReadToEnd();
-            return rt;
+            StreamReader reader = new(dataStream);            
+            return reader.ReadToEnd();
         }
 
         private async Task HandleValidSubmit()
         {
             MySQLConnector dbCon = MySQLConnector.Instance();
-            long id = 0;
+            long id;
             string stringSQL;
 
             Console.WriteLine($"HandleValidSubmit, docID: {docID}");
@@ -121,16 +120,16 @@ namespace HyperCube.Pages
                 id = docID;
                 stringSQL = $"UPDATE articles " +
                     $"SET filename='{articleModel.Filename}', article_name='{articleModel.Name}', authors='{articleModel.Authors}', " +
-                        $"date_publish='{articleModel.PublishDate.ToString("yyyy-MM-dd")}', annotation='{articleModel.Annotation}', " +
-                        $"keywords='{articleModel.Keywords}', rating={articleModel.Rating} " +
+                        $"date_publish='{articleModel.PublishDate:yyyy-MM-dd}', annotation='{articleModel.Annotation}', " +
+                        $"keywords='{articleModel.Keywords}', rating={articleModel.Rating}, file_hash={articleModel.FileHashSum} " +
                     $"WHERE id={docID}";
                 await dbCon.SQLInsert(stringSQL);
             }
             else
             {
-                stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords) " +
-                    $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
-                    $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
+                stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords, file_hash) " +
+                    $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate:yyyy-MM-dd}'," +
+                        $"'{articleModel.Annotation}', '{articleModel.Keywords}', '{articleModel.FileHashSum}')";
                 id = await dbCon.SQLInsert(stringSQL);
                 NewProjectSmopp(id);
             }          
@@ -198,11 +197,35 @@ namespace HyperCube.Pages
                 memoryStream = new();
                 await stream.CopyToAsync(memoryStream);
                 status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
+                Console.WriteLine(status);
 
-                DocParse docParse = new DocParse();
-                articleModelClone = DocParse.ReadPDF(memoryStream);
-                articleModelClone.Filename = file.Name;
-                articleModel = (ArticleModel)articleModelClone.Clone();
+                /// calculating hash
+                string hash = await CalculateHashSum(memoryStream);
+                Console.WriteLine($"Hash: {hash}");
+
+                /// checking hash
+                MySQLConnector dbCon = MySQLConnector.Instance();
+                string stringSQL;
+                stringSQL = $"SELECT COUNT(*) FROM articles WHERE file_hash='{hash}'";
+                int count = await dbCon.SQLSelectCount(stringSQL);
+                //Console.WriteLine($"File duplicate check, founded files: {count}");
+
+                if (count < 1)
+                {
+                    articleModelClone = DocParse.ReadPDF(memoryStream);
+                    articleModelClone.Filename = file.Name;
+                    articleModelClone.FileHashSum = hash;
+                    articleModel = (ArticleModel)articleModelClone.Clone();
+
+                    IsSubmitDisabled = false;
+                }
+                else
+                {
+                    status = $"File duplicate founded, hash: {hash}.";
+                    Console.WriteLine(status);
+
+                    IsSubmitDisabled = true;
+                }
             }
         }
 
@@ -247,5 +270,13 @@ namespace HyperCube.Pages
 
             return null;
         }
+
+        private async Task<string> CalculateHashSum(MemoryStream ms)
+        {
+            MD5CryptoServiceProvider md5Provider = new();
+            ms.Position = 0;
+            byte[] hash = await md5Provider.ComputeHashAsync(ms);
+            return Convert.ToBase64String(hash);
+        }
     }
 }

+ 0 - 2
Pages/Verifying.razor

@@ -1,7 +1,5 @@
 @page "/verifying"
 
-@using HyperCube.Models;
-
 @attribute [Authorize]
 
 <div class="tabs__content">

+ 6 - 0
Pages/Verifying.razor.cs

@@ -12,6 +12,12 @@ namespace HyperCube.Pages
     {
         private int counter = 1;
 
+        protected override async Task OnInitializedAsync()
+        {
+            /// refreshing articles
+            await AppData.LoadArticles();
+        }
+
         private static string GetDisplayName(Enum enumValue)
         {
             return enumValue.GetType()