55using GroupDocs . Editor . MVC . Products . Editor . Config ;
66using System ;
77using System . Collections . Generic ;
8- using System . Globalization ;
98using System . IO ;
109using System . Net ;
1110using System . Net . Http ;
1413using System . Web . Http ;
1514using System . Web . Http . Cors ;
1615using GroupDocs . Editor . MVC . Products . Editor . Entity . Web . Request ;
16+ using GroupDocs . Editor . Formats ;
17+ using System . Globalization ;
1718
1819namespace 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}
0 commit comments