ImportExerciseController.cs 5.9 KB

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