ImportExerciseController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.Models.CommonInfo;
  12. using TEAMModelOS.Models.Dto;
  13. using TEAMModelOS.Models.PowerPoint;
  14. using TEAMModelOS.SDK;
  15. using TEAMModelOS.SDK.Context.Constant.Common;
  16. using TEAMModelOS.SDK.DI;
  17. using TEAMModelOS.SDK.Extension;
  18. using TEAMModelOS.Services;
  19. using TEAMModelOS.Servicess.Exam;
  20. using TEAMModelOS.Servicess.PowerPoint.Implement;
  21. namespace TEAMModelOS.Controllers
  22. {
  23. [Route("api/[controller]")]
  24. [ApiController]
  25. public class ImportExerciseController : BaseController
  26. {
  27. // private readonly IHtexService htexService;
  28. private readonly AzureStorageFactory _azureStorage;
  29. private readonly IWebHostEnvironment _webHostEnvironment;
  30. private List<LangConfig> langConfigs { get; set; }
  31. public ImportExerciseController( AzureStorageFactory azureStorage, IWebHostEnvironment webHostEnvironment)
  32. {
  33. _webHostEnvironment = webHostEnvironment;
  34. _azureStorage = azureStorage;
  35. string path = _webHostEnvironment.ContentRootPath + "/JsonFile/Core/LangConfig.json";
  36. FileStream fs = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  37. StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
  38. String line;
  39. StringBuilder builder = new StringBuilder();
  40. while ((line = sr.ReadLine()) != null)
  41. {
  42. builder.Append(line.ToString());
  43. }
  44. sr.Close();
  45. string text = builder.ToString();
  46. langConfigs = text.ToObject<List<LangConfig>>();
  47. }
  48. /// <summary>
  49. /// </summary>
  50. /// <param name="request"></param>
  51. /// <returns></returns>
  52. [HttpPost("LoadDoc")]
  53. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  54. public async Task<BaseResponse> LoadDoc([FromForm] IFormFile file)
  55. {
  56. ResponseBuilder responseBuilder = new ResponseBuilder();
  57. Dictionary<string, object> model = await HtexService.LoadDoc(_azureStorage, file);
  58. return responseBuilder.Data(model).build();
  59. }
  60. /// <summary>
  61. /// docUrl
  62. /// folder
  63. /// shaCode
  64. /// </summary>
  65. /// <param name="request"></param>
  66. /// <returns></returns>
  67. [HttpPost("UploadWord")]
  68. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  69. public async Task<BaseResponse> UploadWord([FromForm] IFormFile file)
  70. {
  71. ResponseBuilder responseBuilder = new ResponseBuilder();
  72. if (!FileType.GetExtention(file.FileName).ToLower().Equals("docx"))
  73. {
  74. return responseBuilder.Error(ResponseCode.FAILED, "type is not docx!").build();
  75. }
  76. Dictionary<string, object> model = await ImportExerciseService.UploadWord(_azureStorage, file);
  77. return responseBuilder.Data(model).build();
  78. }
  79. /// <summary>
  80. /// htmlString
  81. /// </summary>
  82. /// <param name="request"></param>
  83. /// <returns></returns>
  84. [HttpPost("AnalyzeHtml")]
  85. public BaseResponse AnalyzeHtml(JsonElement request)
  86. {
  87. ResponseBuilder builder = ResponseBuilder.custom();
  88. Dictionary<string, object> dict = new Dictionary<string, object>();
  89. var emobj = request.EnumerateObject();
  90. while (emobj.MoveNext())
  91. {
  92. dict[emobj.Current.Name] = emobj.Current.Value;
  93. }
  94. bool flag = dict.TryGetValue("htmlString", out object htmlString);
  95. bool flagLang = dict.TryGetValue("lang", out object lang);
  96. if (flag && htmlString != null && !string.IsNullOrEmpty(htmlString.ToString()))
  97. {
  98. LangConfig langConfig= langConfigs.Where(x => x.Lang == lang.ToString()).FirstOrDefault();
  99. HtmlAnalyzeService htmlAnalyzeService = new HtmlAnalyzeService(langConfig);
  100. List<ItemInfo> exercises = htmlAnalyzeService.AnalyzeWordAsync(htmlString.ToString());
  101. return builder.Data(exercises).build();
  102. }
  103. else
  104. {
  105. return builder.Data(null).build();
  106. }
  107. }
  108. /// <summary>
  109. /// htmlString
  110. /// </summary>
  111. /// <param name="request"></param>
  112. /// <returns></returns>
  113. [HttpPost("HtmlToHtex")]
  114. public async Task<BaseResponse> HtmlToHtex(JsonElement request)
  115. {
  116. ResponseBuilder builder = ResponseBuilder.custom();
  117. /* Dictionary<string, object> dict = new Dictionary<string, object>();
  118. var emobj = request.EnumerateObject();
  119. while (emobj.MoveNext())
  120. {
  121. dict[emobj.Current.Name] = emobj.Current.Value;
  122. }*/
  123. bool flag = request.TryGetProperty("htmlString", out JsonElement htmlString);
  124. bool flagLang = request.TryGetProperty("lang", out JsonElement lang);
  125. if (flag && htmlString.ToString() != null && !string.IsNullOrEmpty(htmlString.ToString()))
  126. {
  127. LangConfig langConfig = langConfigs.Where(x => x.Lang == lang.ToString()).FirstOrDefault();
  128. HtmlAnalyzeService htmlAnalyzeService = new HtmlAnalyzeService(langConfig);
  129. Htex exercises = await HtexService.AnalyzeHtmlToHtex(_azureStorage, htmlString.ToString(), lang.ToString(), htmlAnalyzeService);
  130. return builder.Data(exercises).build();
  131. }
  132. else
  133. {
  134. return builder.Data(null).build();
  135. }
  136. }
  137. }
  138. }