-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProofreadController.cs
More file actions
105 lines (87 loc) · 4.28 KB
/
ProofreadController.cs
File metadata and controls
105 lines (87 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using DevExpress.AIIntegration.Docs;
using DevExpress.XtraRichEdit;
using Microsoft.AspNetCore.Mvc;
using RichEditOpenAIWebApi.BusinessObjects;
using Swashbuckle.AspNetCore.Annotations;
using System.Globalization;
using System.Net;
namespace RichEditOpenAIWebApi.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
public class ProofreadController : ControllerBase
{
private readonly IAIDocProcessingService docProcessingService;
public ProofreadController(IAIDocProcessingService docService)
{
docProcessingService = docService;
}
[HttpPost]
[SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))]
public async Task<IActionResult> ProofreadWordDocument(IFormFile file, [FromQuery] RichEditFormat outputFormat, [FromQuery] RichEditDocumentPart part)
{
try
{
using (var wordProcessor = new RichEditDocumentServer())
{
await RichEditHelper.LoadFile(wordProcessor, file);
CultureInfo cultureInfo = wordProcessor.Document.DefaultCharacterProperties.Language.Value.Latin;
switch (part)
{
case RichEditDocumentPart.FirstPage:
var fixedRange = wordProcessor.DocumentLayout.GetPage(0).MainContentRange;
var pageRange = wordProcessor.Document.CreateRange(fixedRange.Start, fixedRange.Length); // Translate the first page
await docProcessingService.ProofreadAsync(pageRange, cultureInfo);
break;
case RichEditDocumentPart.FirstSection:
var sectionRange = wordProcessor.Document.Sections[0].Range;
await docProcessingService.ProofreadAsync(sectionRange, cultureInfo);
break;
case RichEditDocumentPart.WholeDocument:
default:
await docProcessingService.ProofreadAsync(wordProcessor, cultureInfo);
break;
}
Stream result = RichEditHelper.SaveDocument(wordProcessor, outputFormat);
string contentType = RichEditHelper.GetContentType(outputFormat);
string outputStringFormat = outputFormat.ToString().ToLower();
return File(result, contentType, $"result.{outputStringFormat}");
}
}
catch (Exception e)
{
return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace);
}
}
[HttpPost]
[SwaggerResponse((int)HttpStatusCode.OK, "Download a file", typeof(FileContentResult))]
public async Task<IActionResult> ProofreadPresentation(IFormFile file, [FromQuery] PresentationPart part, [FromQuery] PresentationFormat outputFormat)
{
try
{
var presentation = await PresentationHelper.LoadPresentation(file);
CultureInfo cultureSettings = presentation.DefaultTextStyle.ParagraphProperties.TextProperties.Language;
switch (part)
{
case PresentationPart.FirstSlide:
var slide = presentation.Slides[0];
await docProcessingService.ProofreadAsync(slide, cultureSettings);
break;
case PresentationPart.WholePresentation:
await docProcessingService.ProofreadAsync(presentation, cultureSettings);
break;
default:
break;
}
Stream result = PresentationHelper.SaveDocument(presentation, outputFormat);
string contentType = PresentationHelper.GetContentType(outputFormat);
string outputStringFormat = outputFormat.ToString().ToLower();
return File(result, contentType, $"result.{outputStringFormat}");
}
catch (Exception e)
{
return StatusCode(500, e.Message + Environment.NewLine + e.StackTrace);
}
}
}
}