diff --git a/src/GroupDocs.Editor.MVC.csproj b/src/GroupDocs.Editor.MVC.csproj index 461e986..dec9dd1 100644 --- a/src/GroupDocs.Editor.MVC.csproj +++ b/src/GroupDocs.Editor.MVC.csproj @@ -48,8 +48,8 @@ ..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll - - ..\packages\GroupDocs.Editor.19.5.0\lib\GroupDocs.Editor.dll + + ..\packages\GroupDocs.Editor.19.11.0\lib\net20\GroupDocs.Editor.dll ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll diff --git a/src/Products/Common/Resources/Resources.cs b/src/Products/Common/Resources/Resources.cs index c1f6320..4cd0678 100644 --- a/src/Products/Common/Resources/Resources.cs +++ b/src/Products/Common/Resources/Resources.cs @@ -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"; } diff --git a/src/Products/Editor/Controllers/EditorApiController.cs b/src/Products/Editor/Controllers/EditorApiController.cs index d186f11..242f110 100644 --- a/src/Products/Editor/Controllers/EditorApiController.cs +++ b/src/Products/Editor/Controllers/EditorApiController.cs @@ -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; @@ -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 { @@ -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)); + } } /// @@ -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())); } } @@ -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; @@ -354,36 +348,76 @@ 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; } @@ -391,30 +425,32 @@ private static List PrepareFormats() { List outputListItems = new List(); - 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; @@ -422,46 +458,29 @@ private static List PrepareFormats() 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; } } } \ No newline at end of file diff --git a/src/packages.config b/src/packages.config index a90a80d..9b515e0 100644 --- a/src/packages.config +++ b/src/packages.config @@ -2,7 +2,7 @@ - +