123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- using Azure.Storage.Blobs.Models;
- using Azure.Storage.Sas;
- using HTEXLib;
- using HTEXLib.Builders;
- using HTEXLib.Helpers.ShapeHelpers;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IdentityModel.Tokens.Jwt;
- using System.IO;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.Models.Dto;
- using TEAMModelOS.Models.PowerPoint;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Context.Constant;
- using TEAMModelOS.SDK.Context.Constant.Common;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
- using TEAMModelOS.SDK.Module.AzureBlob.Container;
- using TEAMModelOS.Services;
- using TEAMModelOS.Servicess.Exam;
- using TEAMModelOS.Servicess.PowerPoint.Implement;
- namespace TEAMModelOS.Controllers
- {
- [Route("common/import")]
- [ApiController]
- public class ImportExerciseController : BaseController
- {
- public HtexGenerator htexGenerator { get; set; }
- // private readonly IHtexService htexService;
- private readonly AzureStorageFactory _azureStorage;
- private readonly IWebHostEnvironment _webHostEnvironment;
- private List<LangConfig> langConfigs { get; set; }
- private readonly IHttpClientFactory _clientFactory;
- public ImportExerciseController( AzureStorageFactory azureStorage, IWebHostEnvironment webHostEnvironment, HtexGenerator htexGenerator, IHttpClientFactory clientFactory)
- {
- _clientFactory = clientFactory;
- this.htexGenerator = htexGenerator;
- _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("load-doc")]
- [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>
- /// {"url":"https://***.blob.core.cn/xxx/1.pptx"}
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("parse-doc")]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<IActionResult> ParsePPTX(JsonElement request)
- {
- //string id_token = HttpContext.GetXAuth("IdToken");
- //if (string.IsNullOrEmpty(id_token)) return BadRequest();
- //var jwt = new JwtSecurityToken(id_token);
- //if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
- //var id = jwt.Payload.Sub;
- request.TryGetProperty("file", out JsonElement code);
- string azureBlobSAS = System.Web.HttpUtility.UrlDecode(code.ToString(), Encoding.UTF8);
- (string, string) a = BlobUrlString(azureBlobSAS);
- string ContainerName = a.Item1;
- string BlobName = a.Item2;
- bool flg = IsBlobName(BlobName);
- var codes = azureBlobSAS.Split("/");
- var file = codes[codes.Length - 1].Split(".");
- var FileName = file[0];
- var ext = file[1];
- if (flg)
- {
- BlobAuth blobAuth = _azureStorage.GetBlobSasUriRead(ContainerName, BlobName);
- var response = await _clientFactory.CreateClient().GetAsync(new Uri(blobAuth.url));
- response.EnsureSuccessStatusCode();
- Stream stream= await response.Content.ReadAsStreamAsync();
- if (ext.ToLower() == "pptx" || ext.ToLower() == "xml")
- {
- string index = await PPTXTranslator(ContainerName, FileName, stream);
- return Ok(new { index = index });
- }
- else if (ext.ToLower() == "docx" || ext.ToLower() == "doc")
- {
- return Ok(new { index = "" });
- }
- else {
- return BadRequest("不支持该文件类型的解析!");
- }
- }
- else { return BadRequest("不是正确的Blob链接!"); }
- }
- private static (string, string) BlobUrlString(string sasUrl)
- {
- sasUrl = sasUrl.Substring(8);
- string[] sasUrls = sasUrl.Split("/");
- string ContainerName;
- ContainerName = sasUrls[1].Clone().ToString();
- string item = sasUrls[0] + "/" + sasUrls[1] + "/";
- string blob = sasUrl.Replace(item, "");
- return (ContainerName, blob);
- }
- public static bool IsBlobName(string BlobName)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(BlobName,
- @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\:\\<\\>\\*\\?\\\\\\""\\\\|]{1,255}$)");
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("upload-pptx")]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<IActionResult> UploadPPTX([FromForm] IFormFile file)
- {
- string id_token = HttpContext.GetXAuth("IdToken");
- if (string.IsNullOrEmpty(id_token)) return BadRequest();
- var jwt = new JwtSecurityToken(id_token);
- if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
- var id = jwt.Payload.Sub;
- if (FileType.GetExtention(file.FileName).ToLower().Equals("pptx") || FileType.GetExtention(file.FileName).ToLower().Equals("xml"))
- {
- string FileName = file.FileName.Split(".")[0];
- Stream streamFile = file.OpenReadStream();
- string index = await PPTXTranslator(id, FileName, streamFile);
- return Ok(new { index = index });
- }
- else {
- return BadRequest("type is not pptx or xml !");
- }
-
-
- }
- /// <summary>
- /// docUrl
- /// folder
- /// shaCode
- ///
- /// UploadWord
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("upload-word")]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<IActionResult> UploadWord([FromForm] IFormFile file)
- {
- // ResponseBuilder responseBuilder = new ResponseBuilder();
- if (!FileType.GetExtention(file.FileName).ToLower().Equals("docx"))
- {
- return BadRequest(new Dictionary<string, object> { {"msg", "type is not docx!" },{ "code",ResponseCode.FAILED} });
- }
- Dictionary<string, object> model = await ImportExerciseService.UploadWord(_azureStorage, file);
- return Ok(model);
- }
- /// <summary>
- /// htmlString AnalyzeHtml
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("parse-html")]
- public IActionResult 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<ItemInfo> exercises = htmlAnalyzeService.AnalyzeWordAsync(htmlString.ToString());
- return Ok(exercises);
- }
- else
- {
- return BadRequest();
- }
- }
- /// <summary>
- /// htmlString
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("HtmlToHtex")]
- public async Task<IActionResult> 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);
- Models.PowerPoint.Htex exercises = await HtexService.AnalyzeHtmlToHtex(_azureStorage, htmlString.ToString(), lang.ToString(), htmlAnalyzeService);
- return Ok(exercises);
- }
- else
- {
- return BadRequest("语言设置错误");
- }
- }
- private async Task<string> PPTXTranslator(string id, string FileName, Stream streamFile)
- {
- var status = await _azureStorage.GetBlobServiceClient().DelectBlobs(id, $"res/{FileName}");
- string shaCode = Guid.NewGuid().ToString("N");
- HTEXLib.Htex htex = htexGenerator.Generator(streamFile);
- htex.name = FileName;
- var slides = htex.slides;
- List<Task> tasks = new List<Task>();
- HTEX hTEX = new HTEX() { name = FileName, size = htex.size, thumbnail = htex.thumbnail, id = shaCode };
- Dictionary<string, string> texts = new Dictionary<string, string>();
- List<string> shas = new List<string>();
- foreach (var slide in slides)
- {
- string text = JsonHelper.ToJson(slide, ignoreNullValue: false);
- string sha = Guid.NewGuid().ToString("N");
- texts.Add(sha, text);
- shas.Add(sha);
- }
- Dictionary<string, string> bloburls = new Dictionary<string, string>();
- foreach (var key in texts.Keys)
- {
- tasks.Add(_azureStorage.UploadFileByContainer(id, texts[key], "res", FileName + "/" + key + ".json", false)
- .ContinueWith((Task<AzureBlobModel> blob) =>
- {
- bloburls.Add(key, blob.Result.BlobUrl);
- })
- );
- }
- await Task.WhenAll(tasks);
- List<Sld> slds = new List<Sld>();
- foreach (string sha in shas)
- {
- slds.Add(new Sld { type = "normal", url = System.Web.HttpUtility.UrlDecode(bloburls[sha], Encoding.UTF8), scoring = null }); ;
- }
- Dictionary<string, Store> dict = new Dictionary<string, Store>();
- List<Task> tasksFiles = new List<Task>();
- foreach (var key in htex.stores.Keys)
- {
- if (key.EndsWith(".wdp") || key.EndsWith(".xlsx"))
- {
- htex.stores.Remove(key);
- continue;
- }
- var store = htex.stores[key];
- Store str = new Store() { path = key, contentType = store.contentType, isLazy = store.isLazy };
- if (!store.isLazy && store.contentType != null && ContentTypeDict.extdict.TryGetValue(store.contentType, out string ext) && store.url.Contains(";base64,"))
- {
- string[] strs = store.url.Split(',');
- Stream stream = new MemoryStream(Convert.FromBase64String(strs[1]));
- var urlstrs = key.Split("/");
- var name = urlstrs[urlstrs.Length - 1];
- tasksFiles.Add(_azureStorage.UploadFileByContainer(id, stream, "res", FileName + "/" + name, false)
- .ContinueWith((Task<AzureBlobModel> blob) =>
- {
- str.url = System.Web.HttpUtility.UrlDecode(blob.Result.BlobUrl, Encoding.UTF8);
- })
- );
- }
- else
- {
- str.url = System.Web.HttpUtility.UrlDecode(store.url, Encoding.UTF8);
- }
- dict.TryAdd(key, str);
- }
- await Task.WhenAll(tasksFiles);
- hTEX.stores = dict;
- hTEX.slides = slds;
- var blob= await _azureStorage.UploadFileByContainer(id, JsonHelper.ToJson(hTEX, ignoreNullValue: false), "res", FileName + "/" + "index.json", false);
- return System.Web.HttpUtility.UrlDecode(blob.BlobUrl, Encoding.UTF8);
- }
- }
- public class HTEX {
- public string id { get; set; }
- public string version { get; set; } = "1.0.20201210";
- public string name { get; set; }
- public HTEXLib.HtexSize size { get; set; }
- public List<Sld> slides { get; set; }
- //缩略图
- public string thumbnail { get; set; }
- // public int page { get; set; }
- public Dictionary<string, Store> stores { get; set; }
- public List<string> points { get; set; }
- public string periodId { get; set; }
- public List<string> gradeIds { get; set; }
- public string subjectId { get; set; }
- public string subjectName { get; set; }
- public string score { get; set; }
- public string code { get; set; }
- public string scope { get; set; }
- public int? multipleRule { get; set; }
- }
- public class Sld {
- /// <summary>
- /// normal,普通的hte页面 single 单选题 multiple 多选题 judge 判断题 complete 填空题 subjective 问答题 compose 综合题
- /// </summary>
- public string type { get; set; }
- /// <summary>
- /// 单页PPTx htex 的解析链接或一个题目的链接
- /// </summary>
- public string url { get; set; }
- /// <summary>
- /// 题目的配分,如果为type为normal 及compose ,则 scoring=null
- /// </summary>
- public Scoring scoring { get; set; }
- /// <summary>
- /// 单页PPTx htex 的缩略图
- /// </summary>
- public string thumbnail { get; set; }
- }
- public class Scoring {
- public double score { get; set; }
- public List<string> ans { get; set; } = new List<string>();
- }
- }
|