Skip to content
This repository was archived by the owner on Jul 26, 2021. It is now read-only.

Commit 132abf4

Browse files
authored
Merge pull request #8 from groupdocs-editor/update-api-version
Update API version.
2 parents 07cd3e4 + 947f02a commit 132abf4

File tree

4 files changed

+107
-88
lines changed

4 files changed

+107
-88
lines changed

src/GroupDocs.Editor.MVC.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
4949
<HintPath>..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll</HintPath>
5050
</Reference>
51-
<Reference Include="GroupDocs.Editor, Version=19.5.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56, processorArchitecture=MSIL">
52-
<HintPath>..\packages\GroupDocs.Editor.19.5.0\lib\GroupDocs.Editor.dll</HintPath>
51+
<Reference Include="GroupDocs.Editor, Version=19.11.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56, processorArchitecture=MSIL">
52+
<HintPath>..\packages\GroupDocs.Editor.19.11.0\lib\net20\GroupDocs.Editor.dll</HintPath>
5353
</Reference>
5454
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5555
<HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>

src/Products/Common/Resources/Resources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ExceptionEntity GenerateException(System.Exception ex, String password)
7272
// Initiate exception
7373
ExceptionEntity exceptionEntity = new ExceptionEntity();
7474
// Check if exception message contains password and password is empty
75-
if (ex.Message.Contains("password") && String.IsNullOrEmpty(password))
75+
if (ex.Message.ToLower().Contains("password") && String.IsNullOrEmpty(password))
7676
{
7777
exceptionEntity.message = "Password Required";
7878
}

src/Products/Editor/Controllers/EditorApiController.cs

Lines changed: 103 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using GroupDocs.Editor.MVC.Products.Editor.Config;
66
using System;
77
using System.Collections.Generic;
8-
using System.Globalization;
98
using System.IO;
109
using System.Net;
1110
using System.Net.Http;
@@ -14,6 +13,8 @@
1413
using System.Web.Http;
1514
using System.Web.Http.Cors;
1615
using GroupDocs.Editor.MVC.Products.Editor.Entity.Web.Request;
16+
using GroupDocs.Editor.Formats;
17+
using System.Globalization;
1718

1819
namespace GroupDocs.Editor.MVC.Products.Editor.Controllers
1920
{
@@ -129,11 +130,16 @@ public HttpResponseMessage LoadDocumentDescription(PostedDataEntity postedData)
129130
// return document description
130131
return Request.CreateResponse(HttpStatusCode.OK, loadDocumentEntity);
131132
}
132-
catch (System.Exception ex)
133+
catch (PasswordRequiredException ex)
133134
{
134135
// set exception message
135136
return Request.CreateResponse(HttpStatusCode.Forbidden, new Resources().GenerateException(ex, postedData.password));
136137
}
138+
catch (System.Exception ex)
139+
{
140+
// set exception message
141+
return Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex, postedData.password));
142+
}
137143
}
138144

139145
/// <summary>
@@ -243,34 +249,37 @@ public HttpResponseMessage SaveFile(EditDocumentRequest postedData)
243249
try
244250
{
245251
string htmlContent = postedData.getContent(); // Initialize with HTML markup of the edited document
246-
247252
string saveFilePath = Path.Combine(globalConfiguration.GetEditorConfiguration().GetFilesDirectory(), postedData.GetGuid());
248-
if (File.Exists(saveFilePath))
249-
{
250-
File.Delete(saveFilePath);
251-
}
252-
using (OutputHtmlDocument editedHtmlDoc = new OutputHtmlDocument(htmlContent, null))
253+
254+
string tempFilename = Path.GetFileNameWithoutExtension(saveFilePath) + ".tmp";
255+
string tempPath = Path.Combine(Path.GetDirectoryName(saveFilePath), tempFilename);
256+
257+
using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(postedData.GetGuid()))
253258
{
254-
dynamic options = GetSaveOptions(saveFilePath);
255-
if (options.GetType().Equals(typeof(WordProcessingSaveOptions)))
256-
{
257-
options.EnablePagination = true;
258-
}
259-
options.Password = postedData.getPassword();
260-
options.OutputFormat = GetSaveFormat(saveFilePath);
261-
using (System.IO.FileStream outputStream = System.IO.File.Create(saveFilePath))
259+
ISaveOptions saveOptions = GetSaveOptions(saveFilePath);
260+
EditableDocument htmlContentDoc = EditableDocument.FromMarkup(htmlContent, null);
261+
262+
using (FileStream outputStream = File.Create(tempPath))
262263
{
263-
EditorHandler.ToDocument(editedHtmlDoc, outputStream, options);
264+
editor.Save(htmlContentDoc, outputStream, saveOptions);
264265
}
265266
}
267+
268+
if (File.Exists(saveFilePath))
269+
{
270+
File.Delete(saveFilePath);
271+
}
272+
273+
File.Move(tempPath, saveFilePath);
274+
266275
LoadDocumentEntity loadDocumentEntity = LoadDocument(saveFilePath, postedData.getPassword());
267276
// return document description
268277
return Request.CreateResponse(HttpStatusCode.OK, loadDocumentEntity);
269278
}
270-
catch (System.Exception ex)
279+
catch (Exception ex)
271280
{
272281
// set exception message
273-
return Request.CreateResponse(HttpStatusCode.Forbidden, new Resources().GenerateException(ex, postedData.getPassword()));
282+
return Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex, postedData.getPassword()));
274283
}
275284
}
276285

@@ -309,30 +318,15 @@ private dynamic GetSaveFormat(string saveFilePath)
309318
case "Ott":
310319
format = WordProcessingFormats.Ott;
311320
break;
312-
case "txt":
313-
format = WordProcessingFormats.Text;
314-
break;
315-
case "Html":
316-
format = WordProcessingFormats.Html;
317-
break;
318-
case "Mhtml":
319-
format = WordProcessingFormats.Mhtml;
320-
break;
321321
case "WordML":
322322
format = WordProcessingFormats.WordML;
323323
break;
324-
case "Csv":
325-
format = SpreadsheetFormats.Csv;
326-
break;
327324
case "Ods":
328325
format = SpreadsheetFormats.Ods;
329326
break;
330327
case "SpreadsheetML":
331328
format = SpreadsheetFormats.SpreadsheetML;
332329
break;
333-
case "TabDelimited":
334-
format = SpreadsheetFormats.TabDelimited;
335-
break;
336330
case "Xls":
337331
format = SpreadsheetFormats.Xls;
338332
break;
@@ -354,114 +348,139 @@ private dynamic GetSaveFormat(string saveFilePath)
354348
default:
355349
format = WordProcessingFormats.Docx;
356350
break;
357-
358351
}
352+
359353
return format;
360354
}
361355

362-
private dynamic GetSaveOptions(string saveFilePath)
356+
private ISaveOptions GetSaveOptions(string saveFilePath)
363357
{
364358
string extension = Path.GetExtension(saveFilePath).Replace(".", "");
365359
extension = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(extension);
360+
366361
if (extension.Equals("Txt"))
367362
{
368363
extension = "Text";
369364
}
370-
dynamic options = null;
371-
foreach (var item in Enum.GetNames(typeof(WordProcessingFormats)))
365+
366+
ISaveOptions options = null;
367+
368+
foreach (var item in typeof(WordProcessingFormats).GetFields())
372369
{
373-
if (item.Equals("Auto"))
370+
if (item.Name.Equals("Auto"))
374371
{
375372
continue;
376373
}
377-
if (item.Equals(extension))
374+
375+
if (item.Name.Equals(extension))
378376
{
379-
options = new WordProcessingSaveOptions();
377+
options = new WordProcessingSaveOptions(WordProcessingFormats.Docm);
380378
break;
381379
}
382380
}
381+
383382
if (options == null)
384383
{
385-
options = new SpreadsheetSaveOptions();
384+
options = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsb);
386385
}
386+
387+
return options;
388+
}
389+
390+
private ILoadOptions GetLoadOptions(string guid)
391+
{
392+
string extension = Path.GetExtension(guid).Replace(".", "");
393+
extension = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(extension);
394+
395+
if (extension.Equals("Txt"))
396+
{
397+
extension = "Text";
398+
}
399+
400+
ILoadOptions options = null;
401+
402+
foreach (var item in typeof(WordProcessingFormats).GetFields())
403+
{
404+
if (item.Name.Equals("Auto"))
405+
{
406+
continue;
407+
}
408+
409+
if (item.Name.Equals(extension))
410+
{
411+
options = new WordProcessingLoadOptions();
412+
break;
413+
}
414+
}
415+
416+
if (options == null)
417+
{
418+
options = new SpreadsheetLoadOptions();
419+
}
420+
387421
return options;
388422
}
389423

390424
private static List<string> PrepareFormats()
391425
{
392426
List<string> outputListItems = new List<string>();
393427

394-
foreach (var item in Enum.GetNames(typeof(WordProcessingFormats)))
428+
foreach (var item in typeof(WordProcessingFormats).GetFields())
395429
{
396-
if (item.Equals("Auto"))
430+
if (item.Name.Equals("Auto"))
397431
{
398432
continue;
399433
}
400-
if (item.Equals("Text"))
434+
435+
if (item.Name.Equals("Text"))
401436
{
402437
outputListItems.Add("Txt");
403438
}
439+
404440
else
405441
{
406-
outputListItems.Add(item);
442+
outputListItems.Add(item.Name);
407443
}
408-
409444
}
410445

411-
foreach (var item in Enum.GetNames(typeof(SpreadsheetFormats)))
446+
foreach (var item in typeof(SpreadsheetFormats).GetFields())
412447
{
413-
if (item.Equals("Auto"))
448+
if (item.Name.Equals("Auto"))
414449
{
415450
continue;
416451
}
417-
outputListItems.Add(item);
452+
453+
outputListItems.Add(item.Name);
418454
}
419455

420456
return outputListItems;
421457
}
422458

423459
private LoadDocumentEntity LoadDocument(string guid, string password)
424460
{
425-
try
426-
{
427-
dynamic options = null;
428-
//GroupDocs.Editor cannot detect text-based Cells documents formats (like CSV) automatically
429-
if (guid.EndsWith("csv", StringComparison.OrdinalIgnoreCase))
430-
{
431-
options = new SpreadsheetToHtmlOptions();
432-
}
433-
else
434-
{
435-
options = EditorHandler.DetectOptionsFromExtension(guid);
436-
}
461+
LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();
462+
ILoadOptions loadOptions = GetLoadOptions(guid);
463+
loadOptions.Password = password;
437464

438-
if (options is SpreadsheetToHtmlOptions)
439-
{
440-
options.TextOptions = options.TextLoadOptions(",");
441-
}
442-
else
443-
{
444-
options.Password = password;
445-
}
446-
string bodyContent;
465+
// Instantiate Editor object by loading the input file
466+
using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(guid, delegate { return loadOptions; }))
467+
{
468+
// Open input document for edit — obtain an intermediate document, that can be edited
469+
EditableDocument beforeEdit = editor.Edit();
447470

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

450-
using (InputHtmlDocument htmlDoc = EditorHandler.ToHtml(inputDoc, options))
451-
{
452-
bodyContent = htmlDoc.GetEmbeddedHtml();
453-
}
454-
LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();
455-
loadDocumentEntity.SetGuid(System.IO.Path.GetFileName(guid));
475+
loadDocumentEntity.SetGuid(guid);
456476
PageDescriptionEntity page = new PageDescriptionEntity();
457-
page.SetData(bodyContent);
477+
page.SetData(allEmbeddedInsideString);
458478
loadDocumentEntity.SetPages(page);
459-
return loadDocumentEntity;
460-
}
461-
catch
462-
{
463-
throw;
479+
480+
beforeEdit.Dispose();
464481
}
482+
483+
return loadDocumentEntity;
465484
}
466485
}
467486
}

src/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="Antlr" version="3.4.1.9004" targetFramework="net45" />
44
<package id="Castle.Core" version="4.3.1" targetFramework="net45" />
5-
<package id="GroupDocs.Editor" version="19.5.0" targetFramework="net45" />
5+
<package id="GroupDocs.Editor" version="19.11.0" targetFramework="net45" />
66
<package id="Microsoft.AspNet.Cors" version="5.2.7" targetFramework="net45" />
77
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
88
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />

0 commit comments

Comments
 (0)