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.11.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56, processorArchitecture=MSIL">
<HintPath>..\packages\GroupDocs.Editor.19.11.0\lib\net20\GroupDocs.Editor.dll</HintPath>
<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>
<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.ToLower().Contains("password") && String.IsNullOrEmpty(password))
if (ex.Message.Contains("password") && String.IsNullOrEmpty(password))
{
exceptionEntity.message = "Password Required";
}
Expand Down
187 changes: 84 additions & 103 deletions src/Products/Editor/Controllers/EditorApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@ -13,8 +14,6 @@
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 @@ -130,15 +129,10 @@ public HttpResponseMessage LoadDocumentDescription(PostedDataEntity postedData)
// return document description
return Request.CreateResponse(HttpStatusCode.OK, loadDocumentEntity);
}
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));
return Request.CreateResponse(HttpStatusCode.Forbidden, new Resources().GenerateException(ex, postedData.password));
}
}

Expand Down Expand Up @@ -249,37 +243,34 @@ 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());

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()))
{
ISaveOptions saveOptions = GetSaveOptions(saveFilePath);
EditableDocument htmlContentDoc = EditableDocument.FromMarkup(htmlContent, null);

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

string saveFilePath = Path.Combine(globalConfiguration.GetEditorConfiguration().GetFilesDirectory(), postedData.GetGuid());
if (File.Exists(saveFilePath))
{
File.Delete(saveFilePath);
}

File.Move(tempPath, saveFilePath);

using (OutputHtmlDocument editedHtmlDoc = new OutputHtmlDocument(htmlContent, null))
{
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))
{
EditorHandler.ToDocument(editedHtmlDoc, outputStream, options);
}
}
LoadDocumentEntity loadDocumentEntity = LoadDocument(saveFilePath, postedData.getPassword());
// return document description
return Request.CreateResponse(HttpStatusCode.OK, loadDocumentEntity);
}
catch (Exception ex)
catch (System.Exception ex)
{
// set exception message
return Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex, postedData.getPassword()));
return Request.CreateResponse(HttpStatusCode.Forbidden, new Resources().GenerateException(ex, postedData.getPassword()));
}
}

Expand Down Expand Up @@ -318,15 +309,30 @@ 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 @@ -348,139 +354,114 @@ private dynamic GetSaveFormat(string saveFilePath)
default:
format = WordProcessingFormats.Docx;
break;
}

}
return format;
}

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

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

ISaveOptions options = null;

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

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

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

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 typeof(WordProcessingFormats).GetFields())
foreach (var item in Enum.GetNames(typeof(WordProcessingFormats)))
{
if (item.Name.Equals("Auto"))
if (item.Equals("Auto"))
{
continue;
}

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

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

}

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

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

return outputListItems;
}

private LoadDocumentEntity LoadDocument(string guid, string password)
{
LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();
ILoadOptions loadOptions = GetLoadOptions(guid);
loadOptions.Password = password;

// Instantiate Editor object by loading the input file
using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(guid, delegate { return loadOptions; }))
try
{
// Open input document for edit — obtain an intermediate document, that can be edited
EditableDocument beforeEdit = editor.Edit();
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);
}

// 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();
if (options is SpreadsheetToHtmlOptions)
{
options.TextOptions = options.TextLoadOptions(",");
}
else
{
options.Password = password;
}
string bodyContent;

loadDocumentEntity.SetGuid(guid);
using (System.IO.FileStream inputDoc = System.IO.File.OpenRead(guid))

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

beforeEdit.Dispose();
return loadDocumentEntity;
}
catch
{
throw;
}

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.11.0" targetFramework="net45" />
<package id="GroupDocs.Editor" version="19.5.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