|
@@ -0,0 +1,137 @@
|
|
|
|
+using HTEXLib;
|
|
|
|
+using HTEXLib.Translator;
|
|
|
|
+using Ionic.Zip;
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.IO;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using TEAMModelOS.Controllers;
|
|
|
|
+using TEAMModelOS.SDK;
|
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
|
+
|
|
|
|
+namespace TEAMModelOS.Services
|
|
|
|
+{
|
|
|
|
+ public class FileExtractService
|
|
|
|
+ {
|
|
|
|
+ private readonly PPTX2HTEXTranslator _PPTX2HTEXTranslator;
|
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
|
+
|
|
|
|
+ public FileExtractService(AzureStorageFactory azureStorage)
|
|
|
|
+ {
|
|
|
|
+ _azureStorage = azureStorage;
|
|
|
|
+ _PPTX2HTEXTranslator = new PPTX2HTEXTranslator();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<string> ExtractPPTX(string containerid, string FileName, Stream streamFile)
|
|
|
|
+ {
|
|
|
|
+ if (string.IsNullOrWhiteSpace(containerid))
|
|
|
|
+ {
|
|
|
|
+ containerid = "teammodelos";
|
|
|
|
+ }
|
|
|
|
+ string shaCode = Guid.NewGuid().ToString();
|
|
|
|
+ Htex htex = _PPTX2HTEXTranslator.Translate(streamFile);
|
|
|
|
+ htex.name = FileName;
|
|
|
|
+ var slides = htex.slides;
|
|
|
|
+ List<Task<string>> tasks = new List<Task<string>>();
|
|
|
|
+ HTEXIndex index = new HTEXIndex() { name = FileName, size = htex.size, thumbnail = htex.thumbnail, id = shaCode };
|
|
|
|
+
|
|
|
|
+ List<KeyValuePair<string, string>> blobslidenames = new List<KeyValuePair<string, string>>();
|
|
|
|
+ foreach (var slide in slides)
|
|
|
|
+ {
|
|
|
|
+ string json = JsonHelper.ToJson(slide, ignoreNullValue: false);
|
|
|
|
+ string guid = Guid.NewGuid().ToString();
|
|
|
|
+ blobslidenames.Add(new KeyValuePair<string, string>(guid, json));
|
|
|
|
+ }
|
|
|
|
+ List<Sld> slds = new List<Sld>();
|
|
|
|
+ foreach (var key in blobslidenames)
|
|
|
|
+ {
|
|
|
|
+ slds.Add(new Sld { type = "normal", url = $"{key.Key}.json", scoring = null }); ;
|
|
|
|
+ tasks.Add(_azureStorage.GetBlobContainerClient(containerid).UploadFileByContainer(key.Value, "res", $"{FileName}/{key.Key}.json", false));
|
|
|
|
+ }
|
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
|
+ // 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 = key.Replace("/", "");
|
|
|
|
+ str.url = $"{name}";
|
|
|
|
+ tasks.Add(_azureStorage.GetBlobContainerClient(containerid).UploadFileByContainer(stream, "res", $"{FileName}/{name}", false));
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ str.url = System.Web.HttpUtility.UrlDecode(store.url, Encoding.UTF8);
|
|
|
|
+ }
|
|
|
|
+ // dict.TryAdd(key, str);
|
|
|
|
+ }
|
|
|
|
+ await Task.WhenAll(tasksFiles);
|
|
|
|
+ // index.stores = dict;
|
|
|
|
+ index.slides = slds;
|
|
|
|
+ var BlobUrl = await _azureStorage.GetBlobContainerClient(containerid).UploadFileByContainer(JsonHelper.ToJson(index, ignoreNullValue: false), "res", FileName + "/" + "index.json", false);
|
|
|
|
+ return System.Web.HttpUtility.UrlDecode(BlobUrl, Encoding.UTF8);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<string> ExtractHTEX(string containerid, string FileName, Stream stream)
|
|
|
|
+ {
|
|
|
|
+ Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
|
|
+ //处理中文乱码问题
|
|
|
|
+ Encoding encoding = Encoding.GetEncoding("GB2312");
|
|
|
|
+ var options = new ReadOptions { Encoding = encoding };
|
|
|
|
+ string index = "";
|
|
|
|
+ bool hasindex = false;
|
|
|
|
+ List<Task<string>> tasks = new List<Task<string>>();
|
|
|
|
+ ZipFile zip = ZipFile.Read(stream, options);
|
|
|
|
+ zip.AlternateEncoding = encoding;
|
|
|
|
+ List<Stream> streams = new List<Stream>();
|
|
|
|
+ foreach (var f in zip.Entries)
|
|
|
|
+ {
|
|
|
|
+ string name = FileName + "/" + f.FileName;
|
|
|
|
+ if (f.IsDirectory)
|
|
|
|
+ {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ var uploadStream = f.OpenReader();
|
|
|
|
+ byte[] buffer = new byte[uploadStream.Length];
|
|
|
|
+ uploadStream.Read(buffer, 0, buffer.Length);
|
|
|
|
+ Stream blobstream = new MemoryStream(buffer);
|
|
|
|
+ streams.Add(blobstream);
|
|
|
|
+ tasks.Add(_azureStorage.GetBlobContainerClient(containerid).UploadFileByContainer(blobstream, "res", $"{name}", false));
|
|
|
|
+ if (name.Contains($"{FileName}/index.json"))
|
|
|
|
+ {
|
|
|
|
+ hasindex = true;
|
|
|
|
+ }
|
|
|
|
+ uploadStream.Close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ zip.Dispose();
|
|
|
|
+ stream.Close();
|
|
|
|
+ if (hasindex)
|
|
|
|
+ {
|
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
|
+ foreach (var task in tasks)
|
|
|
|
+ {
|
|
|
|
+ var url = System.Web.HttpUtility.UrlDecode(task.Result, Encoding.UTF8);
|
|
|
|
+ if (url.Contains($"{FileName}/index.json"))
|
|
|
|
+ {
|
|
|
|
+ index = url;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //释放资源
|
|
|
|
+ streams.ForEach(x => { x.Close(); });
|
|
|
|
+ return index;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|