From e86e1fbf6d046f730de54e0427022b30f19c899b Mon Sep 17 00:00:00 2001 From: Ragu thanigasalam Date: Thu, 7 Mar 2024 13:11:04 +0530 Subject: [PATCH 1/9] 875026: implemented the CompareFiles and CompareUrlFiles api. --- .../Controllers/DocumentEditorController.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/ASP.NET Core/src/Controllers/DocumentEditorController.cs b/ASP.NET Core/src/Controllers/DocumentEditorController.cs index 03dec4e..ce34c8c 100644 --- a/ASP.NET Core/src/Controllers/DocumentEditorController.cs +++ b/ASP.NET Core/src/Controllers/DocumentEditorController.cs @@ -612,5 +612,76 @@ private WDocument GetDocument(IFormCollection data) stream.Dispose(); return document; } + [AcceptVerbs("Post")] + [HttpPost] + [EnableCors("AllowAllOrigins")] + [Route("Compare")] + public string CompareFiles(IFormFile originalFile, IFormFile revisedFile) + { + if (originalFile == null || revisedFile == null) + return null; + Stream stream = new MemoryStream(); + int index = originalFile.FileName.LastIndexOf('.'); + string type = index > -1 && index < originalFile.FileName.Length - 1 ? originalFile.FileName.Substring(index) : ".docx"; + originalFile.CopyTo(stream); + stream.Position = 0; + Stream stream1 = new MemoryStream(); + int index1 = revisedFile.FileName.LastIndexOf('.'); + string type1 = index > -1 && index < revisedFile.FileName.Length - 1 ? revisedFile.FileName.Substring(index) : ".docx"; + revisedFile.CopyTo(stream1); + stream1.Position = 0; + string json = ""; + WordDocument.MetafileImageParsed -= OnMetafileImageParsed; + + using (WDocument originalDocument = new WDocument(stream, WFormatType.Docx)) + { + //Load the revised document. + using (WDocument revisedDocument = new WDocument(stream1, WFormatType.Docx)) + { + // Compare the original and revised Word documents. + originalDocument.Compare(revisedDocument); + //Save the Word document to MemoryStream + MemoryStream stream2 = new MemoryStream(); + WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower())); + json = Newtonsoft.Json.JsonConvert.SerializeObject(document); + originalDocument.Dispose(); + revisedDocument.Dispose(); + document.Dispose(); + } + } + return json; + } + + [AcceptVerbs("Post")] + [HttpPost] + [EnableCors("AllowAllOrigins")] + [Route("CompareUrlFiles")] + public string CompareUrlFiles(string originalFilePath, string revisedFilePath) + { + if (originalFilePath == null || revisedFilePath == null) + return null; + Stream stream = new MemoryStream(); + stream = GetDocumentFromURL(originalFilePath).Result; + stream.Position = 0; + Stream stream1 = new MemoryStream(); + stream1 = GetDocumentFromURL(revisedFilePath).Result; + stream1.Position = 0; + string json = ""; + WordDocument.MetafileImageParsed -= OnMetafileImageParsed; + using (WDocument originalDocument = new WDocument(stream, WFormatType.Docx)) + { + using (WDocument revisedDocument = new WDocument(stream1, WFormatType.Docx)) + { + originalDocument.Compare(revisedDocument); + MemoryStream stream2 = new MemoryStream(); + WordDocument document = WordDocument.Load(stream2, GetFormatType(".docx")); + json = Newtonsoft.Json.JsonConvert.SerializeObject(document); + originalDocument.Dispose(); + revisedDocument.Dispose(); + document.Dispose(); + } + } + return json; + } } } From 904f1d48f6f729c372a41ee603c8a0a9dba3d462 Mon Sep 17 00:00:00 2001 From: Ragu thanigasalam Date: Thu, 7 Mar 2024 13:51:35 +0530 Subject: [PATCH 2/9] 875026: implemented the CompareFiles and CompareUrlFiles api. --- ASP.NET Core/src/Controllers/DocumentEditorController.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ASP.NET Core/src/Controllers/DocumentEditorController.cs b/ASP.NET Core/src/Controllers/DocumentEditorController.cs index ce34c8c..a578a76 100644 --- a/ASP.NET Core/src/Controllers/DocumentEditorController.cs +++ b/ASP.NET Core/src/Controllers/DocumentEditorController.cs @@ -621,13 +621,9 @@ public string CompareFiles(IFormFile originalFile, IFormFile revisedFile) if (originalFile == null || revisedFile == null) return null; Stream stream = new MemoryStream(); - int index = originalFile.FileName.LastIndexOf('.'); - string type = index > -1 && index < originalFile.FileName.Length - 1 ? originalFile.FileName.Substring(index) : ".docx"; originalFile.CopyTo(stream); stream.Position = 0; Stream stream1 = new MemoryStream(); - int index1 = revisedFile.FileName.LastIndexOf('.'); - string type1 = index > -1 && index < revisedFile.FileName.Length - 1 ? revisedFile.FileName.Substring(index) : ".docx"; revisedFile.CopyTo(stream1); stream1.Position = 0; string json = ""; @@ -642,7 +638,7 @@ public string CompareFiles(IFormFile originalFile, IFormFile revisedFile) originalDocument.Compare(revisedDocument); //Save the Word document to MemoryStream MemoryStream stream2 = new MemoryStream(); - WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower())); + WordDocument document = WordDocument.Load(stream2, FormatType.Docx); json = Newtonsoft.Json.JsonConvert.SerializeObject(document); originalDocument.Dispose(); revisedDocument.Dispose(); @@ -674,7 +670,7 @@ public string CompareUrlFiles(string originalFilePath, string revisedFilePath) { originalDocument.Compare(revisedDocument); MemoryStream stream2 = new MemoryStream(); - WordDocument document = WordDocument.Load(stream2, GetFormatType(".docx")); + WordDocument document = WordDocument.Load(stream2, FormatType.Docx); json = Newtonsoft.Json.JsonConvert.SerializeObject(document); originalDocument.Dispose(); revisedDocument.Dispose(); From f972719fb91fe498ea9fe6daaa4cce596c5f494e Mon Sep 17 00:00:00 2001 From: Ragu thanigasalam Date: Thu, 7 Mar 2024 15:07:20 +0530 Subject: [PATCH 3/9] 875026: implemented the CompareFiles and CompareUrlFiles api. --- ASP.NET Core/src/Controllers/DocumentEditorController.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ASP.NET Core/src/Controllers/DocumentEditorController.cs b/ASP.NET Core/src/Controllers/DocumentEditorController.cs index a578a76..b40ff94 100644 --- a/ASP.NET Core/src/Controllers/DocumentEditorController.cs +++ b/ASP.NET Core/src/Controllers/DocumentEditorController.cs @@ -636,9 +636,7 @@ public string CompareFiles(IFormFile originalFile, IFormFile revisedFile) { // Compare the original and revised Word documents. originalDocument.Compare(revisedDocument); - //Save the Word document to MemoryStream - MemoryStream stream2 = new MemoryStream(); - WordDocument document = WordDocument.Load(stream2, FormatType.Docx); + WordDocument document = WordDocument.Load(stream, FormatType.Docx); json = Newtonsoft.Json.JsonConvert.SerializeObject(document); originalDocument.Dispose(); revisedDocument.Dispose(); @@ -669,8 +667,7 @@ public string CompareUrlFiles(string originalFilePath, string revisedFilePath) using (WDocument revisedDocument = new WDocument(stream1, WFormatType.Docx)) { originalDocument.Compare(revisedDocument); - MemoryStream stream2 = new MemoryStream(); - WordDocument document = WordDocument.Load(stream2, FormatType.Docx); + WordDocument document = WordDocument.Load(stream, FormatType.Docx); json = Newtonsoft.Json.JsonConvert.SerializeObject(document); originalDocument.Dispose(); revisedDocument.Dispose(); From 0add8189cd7629e8ed9f71d95d70c8bb5c6ab335 Mon Sep 17 00:00:00 2001 From: Ragu thanigasalam Date: Thu, 7 Mar 2024 15:33:22 +0530 Subject: [PATCH 4/9] 875026: implemented the CompareFiles and CompareUrlFiles api. --- ASP.NET Core/src/Controllers/DocumentEditorController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ASP.NET Core/src/Controllers/DocumentEditorController.cs b/ASP.NET Core/src/Controllers/DocumentEditorController.cs index b40ff94..ef88370 100644 --- a/ASP.NET Core/src/Controllers/DocumentEditorController.cs +++ b/ASP.NET Core/src/Controllers/DocumentEditorController.cs @@ -636,7 +636,7 @@ public string CompareFiles(IFormFile originalFile, IFormFile revisedFile) { // Compare the original and revised Word documents. originalDocument.Compare(revisedDocument); - WordDocument document = WordDocument.Load(stream, FormatType.Docx); + WordDocument document = WordDocument.Load(originalDocument); json = Newtonsoft.Json.JsonConvert.SerializeObject(document); originalDocument.Dispose(); revisedDocument.Dispose(); @@ -667,7 +667,7 @@ public string CompareUrlFiles(string originalFilePath, string revisedFilePath) using (WDocument revisedDocument = new WDocument(stream1, WFormatType.Docx)) { originalDocument.Compare(revisedDocument); - WordDocument document = WordDocument.Load(stream, FormatType.Docx); + WordDocument document = WordDocument.Load(originalDocument); json = Newtonsoft.Json.JsonConvert.SerializeObject(document); originalDocument.Dispose(); revisedDocument.Dispose(); From 5aaedced6965ab9088c661686d6d6a11bbed6611 Mon Sep 17 00:00:00 2001 From: Ragu thanigasalam Date: Thu, 7 Mar 2024 16:33:07 +0530 Subject: [PATCH 5/9] 875026: moved the review changes. --- .../src/Controllers/DocumentEditorController.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ASP.NET Core/src/Controllers/DocumentEditorController.cs b/ASP.NET Core/src/Controllers/DocumentEditorController.cs index ef88370..a2597a7 100644 --- a/ASP.NET Core/src/Controllers/DocumentEditorController.cs +++ b/ASP.NET Core/src/Controllers/DocumentEditorController.cs @@ -650,15 +650,15 @@ public string CompareFiles(IFormFile originalFile, IFormFile revisedFile) [HttpPost] [EnableCors("AllowAllOrigins")] [Route("CompareUrlFiles")] - public string CompareUrlFiles(string originalFilePath, string revisedFilePath) + public string CompareUrlFiles([FromBody] CompareParameter data) { - if (originalFilePath == null || revisedFilePath == null) + if (data.OriginalFilePath == null || data.RevisedFilePath == null) return null; Stream stream = new MemoryStream(); - stream = GetDocumentFromURL(originalFilePath).Result; + stream = GetDocumentFromURL(data.OriginalFilePath).Result; stream.Position = 0; Stream stream1 = new MemoryStream(); - stream1 = GetDocumentFromURL(revisedFilePath).Result; + stream1 = GetDocumentFromURL(data.RevisedFilePath).Result; stream1.Position = 0; string json = ""; WordDocument.MetafileImageParsed -= OnMetafileImageParsed; @@ -676,5 +676,10 @@ public string CompareUrlFiles(string originalFilePath, string revisedFilePath) } return json; } + public class CompareParameter + { + public string OriginalFilePath { get; set; } + public string RevisedFilePath { get; set; } + } } } From 3ae5b2bcc849d62b8198c3a3054f84420e733064 Mon Sep 17 00:00:00 2001 From: Ragu thanigasalam Date: Thu, 7 Mar 2024 18:41:00 +0530 Subject: [PATCH 6/9] 875026: moved the review changes. --- .../Controllers/DocumentEditorController.cs | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/ASP.NET Core/src/Controllers/DocumentEditorController.cs b/ASP.NET Core/src/Controllers/DocumentEditorController.cs index a2597a7..82a1634 100644 --- a/ASP.NET Core/src/Controllers/DocumentEditorController.cs +++ b/ASP.NET Core/src/Controllers/DocumentEditorController.cs @@ -616,15 +616,15 @@ private WDocument GetDocument(IFormCollection data) [HttpPost] [EnableCors("AllowAllOrigins")] [Route("Compare")] - public string CompareFiles(IFormFile originalFile, IFormFile revisedFile) + public string CompareFiles([FromBody] CompareParameter data) { - if (originalFile == null || revisedFile == null) + if (data.OriginalFile == null || data.RevisedFile == null) return null; Stream stream = new MemoryStream(); - originalFile.CopyTo(stream); + data.OriginalFile.CopyTo(stream); stream.Position = 0; Stream stream1 = new MemoryStream(); - revisedFile.CopyTo(stream1); + data.RevisedFile.CopyTo(stream1); stream1.Position = 0; string json = ""; WordDocument.MetafileImageParsed -= OnMetafileImageParsed; @@ -650,21 +650,15 @@ public string CompareFiles(IFormFile originalFile, IFormFile revisedFile) [HttpPost] [EnableCors("AllowAllOrigins")] [Route("CompareUrlFiles")] - public string CompareUrlFiles([FromBody] CompareParameter data) + public string CompareUrlFiles([FromBody] CompareUrlParameter data) { if (data.OriginalFilePath == null || data.RevisedFilePath == null) return null; - Stream stream = new MemoryStream(); - stream = GetDocumentFromURL(data.OriginalFilePath).Result; - stream.Position = 0; - Stream stream1 = new MemoryStream(); - stream1 = GetDocumentFromURL(data.RevisedFilePath).Result; - stream1.Position = 0; string json = ""; WordDocument.MetafileImageParsed -= OnMetafileImageParsed; - using (WDocument originalDocument = new WDocument(stream, WFormatType.Docx)) + using (WDocument originalDocument = new WDocument(GetDocFromURL(data.OriginalFilePath).Result, WFormatType.Docx)) { - using (WDocument revisedDocument = new WDocument(stream1, WFormatType.Docx)) + using (WDocument revisedDocument = new WDocument(GetDocFromURL(data.RevisedFilePath).Result, WFormatType.Docx)) { originalDocument.Compare(revisedDocument); WordDocument document = WordDocument.Load(originalDocument); @@ -676,10 +670,39 @@ public string CompareUrlFiles([FromBody] CompareParameter data) } return json; } - public class CompareParameter + public class CompareUrlParameter { public string OriginalFilePath { get; set; } public string RevisedFilePath { get; set; } } + public class CompareParameter + { + public IFormFile OriginalFile { get; set; } + public IFormFile RevisedFile { get; set; } + } + + async Task GetDocFromURL(string url) + { + string documentPath = Path.Combine(path, url); + Stream stream = null; + if (System.IO.File.Exists(documentPath)) + { + byte[] bytes = System.IO.File.ReadAllBytes(documentPath); + stream = new MemoryStream(bytes); + } + else + { + bool result = Uri.TryCreate(url, UriKind.Absolute, out Uri uriResult) + && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); + if (result) + { + stream = GetDocumentFromURL(url).Result; + if (stream != null) + stream.Position = 0; + } + } + return stream; + } + } } From 6db7bdce648045519f17d0a75e86cb77c6f23948 Mon Sep 17 00:00:00 2001 From: Ragu thanigasalam Date: Fri, 8 Mar 2024 12:23:07 +0530 Subject: [PATCH 7/9] 875026: moved the review changes. --- ASP.NET Core/src/Controllers/DocumentEditorController.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/ASP.NET Core/src/Controllers/DocumentEditorController.cs b/ASP.NET Core/src/Controllers/DocumentEditorController.cs index 82a1634..89e3e07 100644 --- a/ASP.NET Core/src/Controllers/DocumentEditorController.cs +++ b/ASP.NET Core/src/Controllers/DocumentEditorController.cs @@ -627,7 +627,6 @@ public string CompareFiles([FromBody] CompareParameter data) data.RevisedFile.CopyTo(stream1); stream1.Position = 0; string json = ""; - WordDocument.MetafileImageParsed -= OnMetafileImageParsed; using (WDocument originalDocument = new WDocument(stream, WFormatType.Docx)) { @@ -655,7 +654,6 @@ public string CompareUrlFiles([FromBody] CompareUrlParameter data) if (data.OriginalFilePath == null || data.RevisedFilePath == null) return null; string json = ""; - WordDocument.MetafileImageParsed -= OnMetafileImageParsed; using (WDocument originalDocument = new WDocument(GetDocFromURL(data.OriginalFilePath).Result, WFormatType.Docx)) { using (WDocument revisedDocument = new WDocument(GetDocFromURL(data.RevisedFilePath).Result, WFormatType.Docx)) From 949a068824d93227f746c262dd05039d9247d7a9 Mon Sep 17 00:00:00 2001 From: Ragu thanigasalam Date: Fri, 8 Mar 2024 15:14:04 +0530 Subject: [PATCH 8/9] 875026: moved the review changes. --- ASP.NET Core/src/Controllers/DocumentEditorController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ASP.NET Core/src/Controllers/DocumentEditorController.cs b/ASP.NET Core/src/Controllers/DocumentEditorController.cs index 89e3e07..50b3a56 100644 --- a/ASP.NET Core/src/Controllers/DocumentEditorController.cs +++ b/ASP.NET Core/src/Controllers/DocumentEditorController.cs @@ -616,7 +616,7 @@ private WDocument GetDocument(IFormCollection data) [HttpPost] [EnableCors("AllowAllOrigins")] [Route("Compare")] - public string CompareFiles([FromBody] CompareParameter data) + public string CompareDocuments([FromBody] CompareParameter data) { if (data.OriginalFile == null || data.RevisedFile == null) return null; @@ -649,7 +649,7 @@ public string CompareFiles([FromBody] CompareParameter data) [HttpPost] [EnableCors("AllowAllOrigins")] [Route("CompareUrlFiles")] - public string CompareUrlFiles([FromBody] CompareUrlParameter data) + public string CompareDocumentsFromUrl([FromBody] CompareUrlParameter data) { if (data.OriginalFilePath == null || data.RevisedFilePath == null) return null; From 0aa11706936882f19b5f84ccdac03cb05f5f93ce Mon Sep 17 00:00:00 2001 From: Ragu thanigasalam Date: Fri, 8 Mar 2024 16:00:48 +0530 Subject: [PATCH 9/9] 875307: moved the review changes. --- .../Controllers/DocumentEditorController.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ASP.NET Core/src/Controllers/DocumentEditorController.cs b/ASP.NET Core/src/Controllers/DocumentEditorController.cs index 50b3a56..76dbdc1 100644 --- a/ASP.NET Core/src/Controllers/DocumentEditorController.cs +++ b/ASP.NET Core/src/Controllers/DocumentEditorController.cs @@ -612,26 +612,27 @@ private WDocument GetDocument(IFormCollection data) stream.Dispose(); return document; } + [AcceptVerbs("Post")] [HttpPost] [EnableCors("AllowAllOrigins")] - [Route("Compare")] + [Route("CompareDocuments")] public string CompareDocuments([FromBody] CompareParameter data) { if (data.OriginalFile == null || data.RevisedFile == null) return null; - Stream stream = new MemoryStream(); - data.OriginalFile.CopyTo(stream); - stream.Position = 0; - Stream stream1 = new MemoryStream(); - data.RevisedFile.CopyTo(stream1); - stream1.Position = 0; + Stream originalDocumentStream = new MemoryStream(); + data.OriginalFile.CopyTo(originalDocumentStream); + originalDocumentStream.Position = 0; + Stream revisedDocumentStream = new MemoryStream(); + data.RevisedFile.CopyTo(revisedDocumentStream); + revisedDocumentStream.Position = 0; string json = ""; - using (WDocument originalDocument = new WDocument(stream, WFormatType.Docx)) + using (WDocument originalDocument = new WDocument(originalDocumentStream, WFormatType.Docx)) { //Load the revised document. - using (WDocument revisedDocument = new WDocument(stream1, WFormatType.Docx)) + using (WDocument revisedDocument = new WDocument(revisedDocumentStream, WFormatType.Docx)) { // Compare the original and revised Word documents. originalDocument.Compare(revisedDocument); @@ -648,7 +649,7 @@ public string CompareDocuments([FromBody] CompareParameter data) [AcceptVerbs("Post")] [HttpPost] [EnableCors("AllowAllOrigins")] - [Route("CompareUrlFiles")] + [Route("CompareDocumentsFromUrl")] public string CompareDocumentsFromUrl([FromBody] CompareUrlParameter data) { if (data.OriginalFilePath == null || data.RevisedFilePath == null) @@ -701,6 +702,5 @@ async Task GetDocFromURL(string url) } return stream; } - } }