Skip to content
This repository was archived by the owner on Jul 26, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/GroupDocs.Editor.MVC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="GroupDocs.Editor, Version=19.5.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56, processorArchitecture=MSIL">
<HintPath>..\packages\GroupDocs.Editor.19.5.0\lib\GroupDocs.Editor.dll</HintPath>
<Reference Include="GroupDocs.Editor, Version=19.11.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56, processorArchitecture=MSIL">
<HintPath>..\packages\GroupDocs.Editor.19.11.0\lib\net20\GroupDocs.Editor.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion src/Products/Common/Resources/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ExceptionEntity GenerateException(System.Exception ex, String password)
// Initiate exception
ExceptionEntity exceptionEntity = new ExceptionEntity();
// Check if exception message contains password and password is empty
if (ex.Message.Contains("password") && String.IsNullOrEmpty(password))
if (ex.Message.ToLower().Contains("password") && String.IsNullOrEmpty(password))
{
exceptionEntity.message = "Password Required";
}
Expand Down
187 changes: 103 additions & 84 deletions src/Products/Editor/Controllers/EditorApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using GroupDocs.Editor.MVC.Products.Editor.Config;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
Expand All @@ -14,6 +13,8 @@
using System.Web.Http;
using System.Web.Http.Cors;
using GroupDocs.Editor.MVC.Products.Editor.Entity.Web.Request;
using GroupDocs.Editor.Formats;
using System.Globalization;

namespace GroupDocs.Editor.MVC.Products.Editor.Controllers
{
Expand Down Expand Up @@ -129,11 +130,16 @@ public HttpResponseMessage LoadDocumentDescription(PostedDataEntity postedData)
// return document description
return Request.CreateResponse(HttpStatusCode.OK, loadDocumentEntity);
}
catch (System.Exception ex)
catch (PasswordRequiredException ex)
{
// set exception message
return Request.CreateResponse(HttpStatusCode.Forbidden, new Resources().GenerateException(ex, postedData.password));
}
catch (System.Exception ex)
{
// set exception message
return Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex, postedData.password));
}
}

/// <summary>
Expand Down Expand Up @@ -243,34 +249,37 @@ public HttpResponseMessage SaveFile(EditDocumentRequest postedData)
try
{
string htmlContent = postedData.getContent(); // Initialize with HTML markup of the edited document

string saveFilePath = Path.Combine(globalConfiguration.GetEditorConfiguration().GetFilesDirectory(), postedData.GetGuid());
if (File.Exists(saveFilePath))
{
File.Delete(saveFilePath);
}
using (OutputHtmlDocument editedHtmlDoc = new OutputHtmlDocument(htmlContent, null))

string tempFilename = Path.GetFileNameWithoutExtension(saveFilePath) + ".tmp";
string tempPath = Path.Combine(Path.GetDirectoryName(saveFilePath), tempFilename);

using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(postedData.GetGuid()))
{
dynamic options = GetSaveOptions(saveFilePath);
if (options.GetType().Equals(typeof(WordProcessingSaveOptions)))
{
options.EnablePagination = true;
}
options.Password = postedData.getPassword();
options.OutputFormat = GetSaveFormat(saveFilePath);
using (System.IO.FileStream outputStream = System.IO.File.Create(saveFilePath))
ISaveOptions saveOptions = GetSaveOptions(saveFilePath);
EditableDocument htmlContentDoc = EditableDocument.FromMarkup(htmlContent, null);

using (FileStream outputStream = File.Create(tempPath))
{
EditorHandler.ToDocument(editedHtmlDoc, outputStream, options);
editor.Save(htmlContentDoc, outputStream, saveOptions);
}
}

if (File.Exists(saveFilePath))
{
File.Delete(saveFilePath);
}

File.Move(tempPath, saveFilePath);

LoadDocumentEntity loadDocumentEntity = LoadDocument(saveFilePath, postedData.getPassword());
// return document description
return Request.CreateResponse(HttpStatusCode.OK, loadDocumentEntity);
}
catch (System.Exception ex)
catch (Exception ex)
{
// set exception message
return Request.CreateResponse(HttpStatusCode.Forbidden, new Resources().GenerateException(ex, postedData.getPassword()));
return Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex, postedData.getPassword()));
}
}

Expand Down Expand Up @@ -309,30 +318,15 @@ private dynamic GetSaveFormat(string saveFilePath)
case "Ott":
format = WordProcessingFormats.Ott;
break;
case "txt":
format = WordProcessingFormats.Text;
break;
case "Html":
format = WordProcessingFormats.Html;
break;
case "Mhtml":
format = WordProcessingFormats.Mhtml;
break;
case "WordML":
format = WordProcessingFormats.WordML;
break;
case "Csv":
format = SpreadsheetFormats.Csv;
break;
case "Ods":
format = SpreadsheetFormats.Ods;
break;
case "SpreadsheetML":
format = SpreadsheetFormats.SpreadsheetML;
break;
case "TabDelimited":
format = SpreadsheetFormats.TabDelimited;
break;
case "Xls":
format = SpreadsheetFormats.Xls;
break;
Expand All @@ -354,114 +348,139 @@ private dynamic GetSaveFormat(string saveFilePath)
default:
format = WordProcessingFormats.Docx;
break;

}

return format;
}

private dynamic GetSaveOptions(string saveFilePath)
private ISaveOptions GetSaveOptions(string saveFilePath)
{
string extension = Path.GetExtension(saveFilePath).Replace(".", "");
extension = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(extension);

if (extension.Equals("Txt"))
{
extension = "Text";
}
dynamic options = null;
foreach (var item in Enum.GetNames(typeof(WordProcessingFormats)))

ISaveOptions options = null;

foreach (var item in typeof(WordProcessingFormats).GetFields())
{
if (item.Equals("Auto"))
if (item.Name.Equals("Auto"))
{
continue;
}
if (item.Equals(extension))

if (item.Name.Equals(extension))
{
options = new WordProcessingSaveOptions();
options = new WordProcessingSaveOptions(WordProcessingFormats.Docm);
break;
}
}

if (options == null)
{
options = new SpreadsheetSaveOptions();
options = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsb);
}

return options;
}

private ILoadOptions GetLoadOptions(string guid)
{
string extension = Path.GetExtension(guid).Replace(".", "");
extension = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(extension);

if (extension.Equals("Txt"))
{
extension = "Text";
}

ILoadOptions options = null;

foreach (var item in typeof(WordProcessingFormats).GetFields())
{
if (item.Name.Equals("Auto"))
{
continue;
}

if (item.Name.Equals(extension))
{
options = new WordProcessingLoadOptions();
break;
}
}

if (options == null)
{
options = new SpreadsheetLoadOptions();
}

return options;
}

private static List<string> PrepareFormats()
{
List<string> outputListItems = new List<string>();

foreach (var item in Enum.GetNames(typeof(WordProcessingFormats)))
foreach (var item in typeof(WordProcessingFormats).GetFields())
{
if (item.Equals("Auto"))
if (item.Name.Equals("Auto"))
{
continue;
}
if (item.Equals("Text"))

if (item.Name.Equals("Text"))
{
outputListItems.Add("Txt");
}

else
{
outputListItems.Add(item);
outputListItems.Add(item.Name);
}

}

foreach (var item in Enum.GetNames(typeof(SpreadsheetFormats)))
foreach (var item in typeof(SpreadsheetFormats).GetFields())
{
if (item.Equals("Auto"))
if (item.Name.Equals("Auto"))
{
continue;
}
outputListItems.Add(item);

outputListItems.Add(item.Name);
}

return outputListItems;
}

private LoadDocumentEntity LoadDocument(string guid, string password)
{
try
{
dynamic options = null;
//GroupDocs.Editor cannot detect text-based Cells documents formats (like CSV) automatically
if (guid.EndsWith("csv", StringComparison.OrdinalIgnoreCase))
{
options = new SpreadsheetToHtmlOptions();
}
else
{
options = EditorHandler.DetectOptionsFromExtension(guid);
}
LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();
ILoadOptions loadOptions = GetLoadOptions(guid);
loadOptions.Password = password;

if (options is SpreadsheetToHtmlOptions)
{
options.TextOptions = options.TextLoadOptions(",");
}
else
{
options.Password = password;
}
string bodyContent;
// Instantiate Editor object by loading the input file
using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(guid, delegate { return loadOptions; }))
{
// Open input document for edit — obtain an intermediate document, that can be edited
EditableDocument beforeEdit = editor.Edit();

using (System.IO.FileStream inputDoc = System.IO.File.OpenRead(guid))
// Get document as a single base64-encoded string, where all resources (images, fonts, etc)
// are embedded inside this string along with main textual content
string allEmbeddedInsideString = beforeEdit.GetEmbeddedHtml();

using (InputHtmlDocument htmlDoc = EditorHandler.ToHtml(inputDoc, options))
{
bodyContent = htmlDoc.GetEmbeddedHtml();
}
LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();
loadDocumentEntity.SetGuid(System.IO.Path.GetFileName(guid));
loadDocumentEntity.SetGuid(guid);
PageDescriptionEntity page = new PageDescriptionEntity();
page.SetData(bodyContent);
page.SetData(allEmbeddedInsideString);
loadDocumentEntity.SetPages(page);
return loadDocumentEntity;
}
catch
{
throw;

beforeEdit.Dispose();
}

return loadDocumentEntity;
}
}
}
2 changes: 1 addition & 1 deletion src/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Antlr" version="3.4.1.9004" targetFramework="net45" />
<package id="Castle.Core" version="4.3.1" targetFramework="net45" />
<package id="GroupDocs.Editor" version="19.5.0" targetFramework="net45" />
<package id="GroupDocs.Editor" version="19.11.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Cors" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
Expand Down