From dbf20e72a57d1147037ee5c8539024b261b671a1 Mon Sep 17 00:00:00 2001 From: Pavel Egorov Date: Mon, 29 Jul 2019 23:17:40 +0400 Subject: [PATCH 1/5] front-end version update --- src/client/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/package.json b/src/client/package.json index 280fa61..cc99c3c 100644 --- a/src/client/package.json +++ b/src/client/package.json @@ -41,7 +41,7 @@ "rxjs": "~6.4.0", "zone.js": "^0.9.1", "@nrwl/angular": "^8.3.0", - "@groupdocs.examples.angular/editor": "^0.2.2" + "@groupdocs.examples.angular/editor": "^0.2.5" }, "devDependencies": { "cypress": "3.4.0", From 422b1bb4660985915567cf60d0b5260728bb792b Mon Sep 17 00:00:00 2001 From: Egor Date: Mon, 9 Dec 2019 11:06:32 +0300 Subject: [PATCH 2/5] Front-end version update. --- src/client/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/package.json b/src/client/package.json index e9c4701..68e19b4 100644 --- a/src/client/package.json +++ b/src/client/package.json @@ -41,7 +41,7 @@ "rxjs": "~6.4.0", "zone.js": "^0.9.1", "@nrwl/angular": "^8.3.0", - "@groupdocs.examples.angular/editor": "^0.4.0" + "@groupdocs.examples.angular/editor": "^0.5.0" }, "devDependencies": { "cypress": "3.4.0", From 131c4083b28d99cca180df14b4b827e4408d014e Mon Sep 17 00:00:00 2001 From: Egor Date: Mon, 16 Dec 2019 21:33:27 +0300 Subject: [PATCH 3/5] Initial changes in the editor api controller and related. --- src/GroupDocs.Editor.MVC.csproj | 4 +- src/Products/Common/Resources/Resources.cs | 2 +- .../Editor/Controllers/EditorApiController.cs | 178 ++++++++++-------- src/packages.config | 2 +- 4 files changed, 108 insertions(+), 78 deletions(-) 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..f71f9ef 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,31 +249,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()); - 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)) + dynamic 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())); @@ -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,8 +348,8 @@ private dynamic GetSaveFormat(string saveFilePath) default: format = WordProcessingFormats.Docx; break; - } + return format; } @@ -363,27 +357,67 @@ private dynamic 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 typeof(WordProcessingFormats).GetFields()) + { + if (item.Name.Equals("Auto")) + { + continue; + } + + if (item.Name.Equals(extension)) + { + options = new WordProcessingSaveOptions(WordProcessingFormats.Docm); + break; + } + } + + if (options == null) + { + options = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsb); + } + + return options; + } + + private dynamic GetLoadOptions(string guid) + { + string extension = Path.GetExtension(guid).Replace(".", ""); + extension = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(extension); + if (extension.Equals("Txt")) { extension = "Text"; } + dynamic options = null; - 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(extension)) + + if (item.Name.Equals(extension)) { - options = new WordProcessingSaveOptions(); + options = new WordProcessingLoadOptions(); break; } } + if (options == null) { - options = new SpreadsheetSaveOptions(); + 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; @@ -424,38 +460,32 @@ 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(); + dynamic loadOptions = GetLoadOptions(guid); + loadOptions.Password = password; - if (options is SpreadsheetToHtmlOptions) + // Instantiate Editor object by loading the input file + using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(guid, delegate { return loadOptions; })) { - options.TextOptions = options.TextLoadOptions(","); - } - else - { - options.Password = password; - } - string bodyContent; + PageDescriptionEntity pageData = new PageDescriptionEntity(); - using (System.IO.FileStream inputDoc = System.IO.File.OpenRead(guid)) + // Open input document for edit — obtain an intermediate document, that can be edited + EditableDocument beforeEdit = editor.Edit(); - using (InputHtmlDocument htmlDoc = EditorHandler.ToHtml(inputDoc, options)) - { - bodyContent = htmlDoc.GetEmbeddedHtml(); + // 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(); + + loadDocumentEntity.SetGuid(Path.GetFileName(guid)); + PageDescriptionEntity page = new PageDescriptionEntity(); + page.SetData(allEmbeddedInsideString); + loadDocumentEntity.SetPages(page); + + // Dispose both EditableDocument instances + beforeEdit.Dispose(); + editor.Dispose(); } - LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity(); - loadDocumentEntity.SetGuid(System.IO.Path.GetFileName(guid)); - PageDescriptionEntity page = new PageDescriptionEntity(); - page.SetData(bodyContent); - loadDocumentEntity.SetPages(page); + return loadDocumentEntity; } catch 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 @@ - + From a867e6a3d21c8e4162253965b44a613b955abe4f Mon Sep 17 00:00:00 2001 From: Egor Date: Wed, 18 Dec 2019 17:38:39 +0300 Subject: [PATCH 4/5] Returned saving password, changed error response code, fixed guid. --- src/Products/Editor/Controllers/EditorApiController.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Products/Editor/Controllers/EditorApiController.cs b/src/Products/Editor/Controllers/EditorApiController.cs index f71f9ef..403f174 100644 --- a/src/Products/Editor/Controllers/EditorApiController.cs +++ b/src/Products/Editor/Controllers/EditorApiController.cs @@ -257,6 +257,7 @@ public HttpResponseMessage SaveFile(EditDocumentRequest postedData) using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(postedData.GetGuid())) { dynamic saveOptions = GetSaveOptions(saveFilePath); + saveOptions.Password = postedData.getPassword(); EditableDocument htmlContentDoc = EditableDocument.FromMarkup(htmlContent, null); using (FileStream outputStream = File.Create(tempPath)) @@ -279,7 +280,7 @@ public HttpResponseMessage SaveFile(EditDocumentRequest postedData) 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())); } } @@ -467,8 +468,6 @@ private LoadDocumentEntity LoadDocument(string guid, string password) // Instantiate Editor object by loading the input file using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(guid, delegate { return loadOptions; })) { - PageDescriptionEntity pageData = new PageDescriptionEntity(); - // Open input document for edit — obtain an intermediate document, that can be edited EditableDocument beforeEdit = editor.Edit(); @@ -476,7 +475,7 @@ private LoadDocumentEntity LoadDocument(string guid, string password) // are embedded inside this string along with main textual content string allEmbeddedInsideString = beforeEdit.GetEmbeddedHtml(); - loadDocumentEntity.SetGuid(Path.GetFileName(guid)); + loadDocumentEntity.SetGuid(guid); PageDescriptionEntity page = new PageDescriptionEntity(); page.SetData(allEmbeddedInsideString); loadDocumentEntity.SetPages(page); From becfb591ad967ab67e5ea98afa1847f55d3e88fb Mon Sep 17 00:00:00 2001 From: Egor Date: Mon, 23 Dec 2019 10:37:21 +0300 Subject: [PATCH 5/5] Improvements by comments. --- .../Editor/Controllers/EditorApiController.cs | 56 ++++++++----------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/src/Products/Editor/Controllers/EditorApiController.cs b/src/Products/Editor/Controllers/EditorApiController.cs index 403f174..242f110 100644 --- a/src/Products/Editor/Controllers/EditorApiController.cs +++ b/src/Products/Editor/Controllers/EditorApiController.cs @@ -256,8 +256,7 @@ public HttpResponseMessage SaveFile(EditDocumentRequest postedData) using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(postedData.GetGuid())) { - dynamic saveOptions = GetSaveOptions(saveFilePath); - saveOptions.Password = postedData.getPassword(); + ISaveOptions saveOptions = GetSaveOptions(saveFilePath); EditableDocument htmlContentDoc = EditableDocument.FromMarkup(htmlContent, null); using (FileStream outputStream = File.Create(tempPath)) @@ -354,7 +353,7 @@ private dynamic GetSaveFormat(string saveFilePath) return format; } - private dynamic GetSaveOptions(string saveFilePath) + private ISaveOptions GetSaveOptions(string saveFilePath) { string extension = Path.GetExtension(saveFilePath).Replace(".", ""); extension = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(extension); @@ -364,7 +363,7 @@ private dynamic GetSaveOptions(string saveFilePath) extension = "Text"; } - dynamic options = null; + ISaveOptions options = null; foreach (var item in typeof(WordProcessingFormats).GetFields()) { @@ -388,7 +387,7 @@ private dynamic GetSaveOptions(string saveFilePath) return options; } - private dynamic GetLoadOptions(string guid) + private ILoadOptions GetLoadOptions(string guid) { string extension = Path.GetExtension(guid).Replace(".", ""); extension = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(extension); @@ -398,7 +397,7 @@ private dynamic GetLoadOptions(string guid) extension = "Text"; } - dynamic options = null; + ILoadOptions options = null; foreach (var item in typeof(WordProcessingFormats).GetFields()) { @@ -459,38 +458,29 @@ private static List PrepareFormats() private LoadDocumentEntity LoadDocument(string guid, string password) { - try - { - LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity(); - dynamic loadOptions = GetLoadOptions(guid); - loadOptions.Password = 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; })) - { - // Open input document for edit — obtain an intermediate document, that can be edited - EditableDocument beforeEdit = editor.Edit(); + // 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(); - // 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(); + // 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(); - loadDocumentEntity.SetGuid(guid); - PageDescriptionEntity page = new PageDescriptionEntity(); - page.SetData(allEmbeddedInsideString); - loadDocumentEntity.SetPages(page); + loadDocumentEntity.SetGuid(guid); + PageDescriptionEntity page = new PageDescriptionEntity(); + page.SetData(allEmbeddedInsideString); + loadDocumentEntity.SetPages(page); - // Dispose both EditableDocument instances - beforeEdit.Dispose(); - editor.Dispose(); - } - - return loadDocumentEntity; - } - catch - { - throw; + beforeEdit.Dispose(); } + + return loadDocumentEntity; } } } \ No newline at end of file