123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Context.Constant.Common;
- using TEAMModelOS.SDK.Context.Exception;
- using TEAMModelOS.SDK;
- using TEAMModelOS.Service.Model.PowerPoint;
- using TEAMModelOS.Service.Services.Exam.Implements;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.Service;
- using TEAMModelOS.Service.Services.PowerPoint.Implement;
- using Microsoft.AspNetCore.Hosting;
- using TEAMModelOS.Dto;
- using System.IO;
- using System.Text;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using System.Text.Json;
- using TEAMModelOS.SDK.Extension;
- namespace TEAMModelOS.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class ImportExerciseController : BaseController
- {
- // private readonly IHtexService htexService;
- private readonly AzureStorageFactory _azureStorage;
- private readonly IWebHostEnvironment _webHostEnvironment;
- private List<LangConfig> langConfigs { get; set; }
- public ImportExerciseController( AzureStorageFactory azureStorage, IWebHostEnvironment webHostEnvironment)
- {
- _webHostEnvironment = webHostEnvironment;
- _azureStorage = azureStorage;
- string path = _webHostEnvironment.ContentRootPath + "/JsonFile/Core/LangConfig.json";
- FileStream fs = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
- String line;
- StringBuilder builder = new StringBuilder();
- while ((line = sr.ReadLine()) != null)
- {
- builder.Append(line.ToString());
- }
- sr.Close();
- string text = builder.ToString();
- langConfigs = text.ToObject<List<LangConfig>>();
- }
- /// <summary>
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("LoadDoc")]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<BaseResponse> LoadDoc([FromForm] IFormFile file)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- Dictionary<string, object> model = await HtexService.LoadDoc(_azureStorage, file);
- return responseBuilder.Data(model).build();
- }
- /// <summary>
- /// docUrl
- /// folder
- /// shaCode
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("UploadWord")]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<BaseResponse> UploadWord([FromForm] IFormFile file)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- if (!FileType.GetExtention(file.FileName).ToLower().Equals("docx"))
- {
- return responseBuilder.Error(ResponseCode.FAILED, "type is not docx!").build();
- }
- Dictionary<string, object> model = await ImportExerciseService.UploadWord(_azureStorage, file);
- return responseBuilder.Data(model).build();
- }
- /// <summary>
- /// htmlString
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("AnalyzeHtml")]
- public BaseResponse AnalyzeHtml(JsonElement request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- Dictionary<string, object> dict = new Dictionary<string, object>();
- var emobj = request.EnumerateObject();
- while (emobj.MoveNext())
- {
- dict[emobj.Current.Name] = emobj.Current.Value;
- }
- bool flag = dict.TryGetValue("htmlString", out object htmlString);
- bool flagLang = dict.TryGetValue("lang", out object lang);
- if (flag && htmlString != null && !string.IsNullOrEmpty(htmlString.ToString()))
- {
- LangConfig langConfig= langConfigs.Where(x => x.Lang == lang.ToString()).FirstOrDefault();
- HtmlAnalyzeService htmlAnalyzeService = new HtmlAnalyzeService(langConfig);
- List<Service.Models.ItemInfo> exercises = htmlAnalyzeService.AnalyzeWordAsync(htmlString.ToString());
- return builder.Data(exercises).build();
- }
- else
- {
- return builder.Data(null).build();
- }
- }
- /// <summary>
- /// htmlString
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("HtmlToHtex")]
- public async Task<BaseResponse> HtmlToHtex(JsonElement request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- /* Dictionary<string, object> dict = new Dictionary<string, object>();
- var emobj = request.EnumerateObject();
- while (emobj.MoveNext())
- {
- dict[emobj.Current.Name] = emobj.Current.Value;
- }*/
- bool flag = request.TryGetProperty("htmlString", out JsonElement htmlString);
- bool flagLang = request.TryGetProperty("lang", out JsonElement lang);
- if (flag && htmlString.ToString() != null && !string.IsNullOrEmpty(htmlString.ToString()))
- {
- LangConfig langConfig = langConfigs.Where(x => x.Lang == lang.ToString()).FirstOrDefault();
- HtmlAnalyzeService htmlAnalyzeService = new HtmlAnalyzeService(langConfig);
- Htex exercises = await HtexService.AnalyzeHtmlToHtex(_azureStorage, htmlString.ToString(), lang.ToString(), htmlAnalyzeService);
- return builder.Data(exercises).build();
- }
- else
- {
- return builder.Data(null).build();
- }
- }
- }
- }
|