|
@@ -12,6 +12,7 @@ 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;
|
|
@@ -23,6 +24,7 @@ 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;
|
|
@@ -39,8 +41,10 @@ namespace TEAMModelOS.Controllers
|
|
|
private readonly AzureStorageFactory _azureStorage;
|
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
|
|
private List<LangConfig> langConfigs { get; set; }
|
|
|
- public ImportExerciseController( AzureStorageFactory azureStorage, IWebHostEnvironment webHostEnvironment, HtexGenerator htexGenerator)
|
|
|
+ private readonly IHttpClientFactory _clientFactory;
|
|
|
+ public ImportExerciseController( AzureStorageFactory azureStorage, IWebHostEnvironment webHostEnvironment, HtexGenerator htexGenerator, IHttpClientFactory clientFactory)
|
|
|
{
|
|
|
+ _clientFactory = clientFactory;
|
|
|
this.htexGenerator = htexGenerator;
|
|
|
_webHostEnvironment = webHostEnvironment;
|
|
|
_azureStorage = azureStorage;
|
|
@@ -74,9 +78,57 @@ namespace TEAMModelOS.Controllers
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
- /// docUrl
|
|
|
- /// folder
|
|
|
- /// shaCode
|
|
|
+ /// {"url":"https://***.blob.core.cn/xxx/1.pptx"}
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("analysis-pptx")]
|
|
|
+ [RequestSizeLimit(102_400_000_00)] //最大10000m左右
|
|
|
+ public async Task<IActionResult> AnalysisPPTX(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("code", 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 = code.ToString().Split("/");
|
|
|
+ var FileName = codes[codes.Length - 1];
|
|
|
+ if (flg)
|
|
|
+ {
|
|
|
+ //TODO 需驗證
|
|
|
+ BlobAuth blobAuth = _azureStorage.GetBlobSasUriRead(ContainerName, BlobName);
|
|
|
+ var response = await _clientFactory.CreateClient().GetAsync(new Uri(blobAuth.url + blobAuth.sas));
|
|
|
+ response.EnsureSuccessStatusCode();
|
|
|
+ Stream stream= await response.Content.ReadAsStreamAsync();
|
|
|
+ HTEXLib.Htex htex = await PPTXTranslator(id, FileName, stream);
|
|
|
+ return Ok(JsonSerializer.Deserialize<JsonElement>(json: JsonHelper.ToJson(htex, ignoreNullValue: true)));
|
|
|
+ }
|
|
|
+ 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>
|
|
@@ -84,9 +136,7 @@ namespace TEAMModelOS.Controllers
|
|
|
[RequestSizeLimit(102_400_000_00)] //最大10000m左右
|
|
|
public async Task<IActionResult> UploadPPTX([FromForm] IFormFile file)
|
|
|
{
|
|
|
-
|
|
|
- var day= new DateTimeOffset(DateTime.UtcNow).ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
|
|
|
- string shaCode = ShaHashHelper.GetSHA1(file.OpenReadStream());
|
|
|
+
|
|
|
string id_token = HttpContext.GetXAuth("IdToken");
|
|
|
if (string.IsNullOrEmpty(id_token)) return BadRequest();
|
|
|
var jwt = new JwtSecurityToken(id_token);
|
|
@@ -94,58 +144,75 @@ namespace TEAMModelOS.Controllers
|
|
|
var id = jwt.Payload.Sub;
|
|
|
if (!FileType.GetExtention(file.FileName).ToLower().Equals("pptx"))
|
|
|
{
|
|
|
- return BadRequest( "type is not pptx!");
|
|
|
+ return BadRequest("type is not pptx!");
|
|
|
}
|
|
|
- HTEXLib.Htex htex = htexGenerator.Generator(file.OpenReadStream());
|
|
|
- htex.name = file.FileName;
|
|
|
+ string FileName = file.FileName;
|
|
|
+ Stream streamFile = file.OpenReadStream();
|
|
|
+
|
|
|
+ HTEXLib.Htex htex = await PPTXTranslator(id, FileName, streamFile);
|
|
|
+ return Ok(JsonSerializer.Deserialize<JsonElement>(json: JsonHelper.ToJson(htex, ignoreNullValue: true)));
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<HTEXLib.Htex> PPTXTranslator(string id, string FileName, Stream streamFile)
|
|
|
+ {
|
|
|
+ var day = new DateTimeOffset(DateTime.UtcNow).ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
|
|
|
+ string shaCode = ShaHashHelper.GetSHA1(streamFile);
|
|
|
+ HTEXLib.Htex htex = htexGenerator.Generator(streamFile);
|
|
|
+ htex.name = FileName;
|
|
|
var slides = htex.slides;
|
|
|
int index = 1;
|
|
|
List<Task> tasks = new List<Task>();
|
|
|
- foreach (var slide in slides) {
|
|
|
- string text= JsonHelper.ToJson(slide, ignoreNullValue: false);
|
|
|
+ foreach (var slide in slides)
|
|
|
+ {
|
|
|
+ string text = JsonHelper.ToJson(slide, ignoreNullValue: false);
|
|
|
tasks.Add(_azureStorage.UploadFileByContainer(id, text, "pptx", day + "/" + shaCode + "/" + index + ".json", false)
|
|
|
- .ContinueWith((Task<AzureBlobModel> blob)=> {
|
|
|
+ .ContinueWith((Task<AzureBlobModel> blob) =>
|
|
|
+ {
|
|
|
htex.urls.Add(blob.Result.BlobUrl);
|
|
|
})
|
|
|
);
|
|
|
- // var blob= await _azureStorage.UploadFileByContainer(id, text, "pptx", day+"/"+ shaCode +"/"+ index + ".json", false);
|
|
|
- // htex.urls.Add(blob.BlobUrl);
|
|
|
+ // var blob= await _azureStorage.UploadFileByContainer(id, text, "pptx", day+"/"+ shaCode +"/"+ index + ".json", false);
|
|
|
+ // htex.urls.Add(blob.BlobUrl);
|
|
|
index++;
|
|
|
}
|
|
|
await Task.WhenAll(tasks);
|
|
|
htex.slides = 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")) {
|
|
|
+ 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};
|
|
|
+ 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 urlstrs = key.Split("/");
|
|
|
var name = urlstrs[urlstrs.Length - 1];
|
|
|
tasksFiles.Add(_azureStorage.UploadFileByContainer(id, stream, "pptx", day + "/" + shaCode + "/" + name, false)
|
|
|
- .ContinueWith((Task<AzureBlobModel> blob)=> {
|
|
|
+ .ContinueWith((Task<AzureBlobModel> blob) =>
|
|
|
+ {
|
|
|
str.url = blob.Result.BlobUrl;
|
|
|
})
|
|
|
);
|
|
|
// var blob = await _azureStorage.UploadFileByContainer(id, stream, "pptx", day + "/" + shaCode + "/" + name, false);
|
|
|
// str.url = blob.BlobUrl;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
str.url = store.url;
|
|
|
}
|
|
|
dict.TryAdd(key, str);
|
|
|
}
|
|
|
await Task.WhenAll(tasksFiles);
|
|
|
htex.stores = dict;
|
|
|
- return Ok(JsonSerializer.Deserialize<JsonElement>(json: JsonHelper.ToJson(htex, ignoreNullValue: true)));
|
|
|
+ return htex;
|
|
|
}
|
|
|
|
|
|
|