Skip to content
Open
92 changes: 92 additions & 0 deletions ASP.NET Core/src/Controllers/DocumentEditorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,5 +612,97 @@ private WDocument GetDocument(IFormCollection data)
stream.Dispose();
return document;
}
[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("Compare")]
public string CompareFiles([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 = "";
WordDocument.MetafileImageParsed -= OnMetafileImageParsed;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line


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 CompareUrlFiles([FromBody] CompareUrlParameter data)
{
if (data.OriginalFilePath == null || data.RevisedFilePath == null)
return null;
string json = "";
WordDocument.MetafileImageParsed -= OnMetafileImageParsed;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line

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;
}

}
}