Skip to content

Commit

Permalink
875307: implemented the CompareFiles and CompareUrlFiles api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ragu thanigasalam committed Mar 8, 2024
1 parent 12952fa commit 58b5c96
Showing 1 changed file with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,96 @@ private WDocument GetDocument(IFormCollection data)
stream.Dispose();
return document;
}

[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("Compare")]
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;
string json = "";

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);
WordDocument document = WordDocument.Load(originalDocument);
json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
originalDocument.Dispose();
revisedDocument.Dispose();
document.Dispose();
}
}
return json;
}

[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("CompareUrlFiles")]
public string CompareDocumentsFromUrl([FromBody] CompareUrlParameter data)
{
if (data.OriginalFilePath == null || data.RevisedFilePath == null)
return null;
string json = "";
using (WDocument originalDocument = new WDocument(GetDocFromURL(data.OriginalFilePath).Result, WFormatType.Docx))
{
using (WDocument revisedDocument = new WDocument(GetDocFromURL(data.RevisedFilePath).Result, WFormatType.Docx))
{
originalDocument.Compare(revisedDocument);
WordDocument document = WordDocument.Load(originalDocument);
json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
originalDocument.Dispose();
revisedDocument.Dispose();
document.Dispose();
}
}
return json;
}
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<Stream> 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;
}
}

}

0 comments on commit 58b5c96

Please sign in to comment.