Bladeren bron

Merge branch 'master' of https://gitlab.kdanmobile.com/windows-team/kdancommon

姜青儀 7 maanden geleden
bovenliggende
commit
9828d621bf
5 gewijzigde bestanden met toevoegingen van 297 en 3 verwijderingen
  1. 13 3
      CMSCollection/CMSCollection.cs
  2. 100 0
      DocAI/Data/RedactInfoExtractResponse.cs
  3. 51 0
      DocAI/Data/VerifyUserResponse.cs
  4. 130 0
      DocAI/DocAI.cs
  5. 3 0
      KdanCommon.csproj

+ 13 - 3
CMSCollection/CMSCollection.cs

@@ -70,6 +70,13 @@ namespace KdanCommon.CMSCollection
                 return _settingList.FirstOrDefault(s => s.SettingName == settingId);
         }
 
+        public async Task<List<ViewerEventBarSetting>> GetPdfEventBarSettings()
+        {
+            await LoadSettings();
+            lock (settingLock)
+                return _settingList.Where(s => s is ViewerEventBarSetting).Select(s => s as ViewerEventBarSetting).ToList();
+        }
+
         public async Task<List<WindowsCardSetting>> GetCardSettings()
         {
             await LoadSettings();
@@ -131,10 +138,13 @@ namespace KdanCommon.CMSCollection
             {
                 var jsonString = await result.Content.ReadAsStringAsync();
                 var response = JsonTool.DeserializeJSON<ViewerEventBarSettingResponse>(jsonString);
-                if (response != null && response.Data != null && response.Data.Length > 0)
+                if (response != null && response.Data != null)
                 {
-                    lock (settingLock)
-                        _settingList.Add(response.Data[0]);
+                    for (int i = 0; i < response.Data.Length; i++)
+                    {
+                        lock (settingLock)
+                            _settingList.Add(response.Data[i]);
+                    }
                 }
             }
         }

+ 100 - 0
DocAI/Data/RedactInfoExtractResponse.cs

@@ -0,0 +1,100 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace KdanCommon.DocAI.Data
+{
+    public partial class RedactInfoExtractResponse
+    {
+        [JsonProperty("usage")]
+        public Usage Usage { get; set; }
+
+        [JsonProperty("pages")]
+        public Page[] Pages { get; set; }
+
+        [JsonProperty("infos")]
+        public Info[] Infos { get; set; }
+    }
+
+
+    public partial class Usage
+    {
+        [JsonProperty("year")]
+        public int Year { get; set; }
+
+        [JsonProperty("month")]
+        public int Month { get; set; }
+
+        [JsonProperty("pages_used")]
+        public int PagesUsed { get; set; }
+
+        [JsonProperty("available_pages")]
+        public int AvailablePages { get; set; }
+    }
+
+    public partial class Info
+    {
+        [JsonProperty("type")]
+        public string Type { get; set; }
+
+        [JsonProperty("blocks")]
+        public Block[] Blocks { get; set; }
+    }
+
+    public partial class Block
+    {
+        [JsonProperty("content")]
+        public string Content { get; set; }
+
+        [JsonProperty("extractionContent")]
+        public string ExtractionContent { get; set; }
+
+        [JsonProperty("extractionType")]
+        public string ExtractionType { get; set; }
+
+        [JsonProperty("boundingRegions")]
+        public BoundingRegion[] BoundingRegions { get; set; }
+    }
+
+    public partial class BoundingRegion
+    {
+        [JsonProperty("pageNumber")]
+        public int PageNumber { get; set; }
+
+        [JsonProperty("boundingPolygon")]
+        public BoundingPolygon[] BoundingPolygon { get; set; }
+    }
+
+    public partial class BoundingPolygon
+    {
+        [JsonProperty("isEmpty")]
+        public bool IsEmpty { get; set; }
+
+        [JsonProperty("x")]
+        public double X { get; set; }
+
+        [JsonProperty("y")]
+        public double Y { get; set; }
+    }
+
+    public partial class Page
+    {
+        [JsonProperty("pageNumber")]
+        public int PageNumber { get; set; }
+
+        [JsonProperty("angle")]
+        public int Angle { get; set; }
+
+        [JsonProperty("width")]
+        public double Width { get; set; }
+
+        [JsonProperty("height")]
+        public double Height { get; set; }
+
+        [JsonProperty("unit")]
+        public long Unit { get; set; }
+    }
+}

+ 51 - 0
DocAI/Data/VerifyUserResponse.cs

@@ -0,0 +1,51 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace KdanCommon.DocAI.Data
+{
+    public partial class VerifyUserResponse
+    {
+        [JsonProperty("user_id")]
+        public long UserId { get; set; }
+
+        [JsonProperty("email")]
+        public string Email { get; set; }
+
+        [JsonProperty("redact")]
+        public Redact Redact { get; set; }
+
+        [JsonProperty("chatbot")]
+        public Chatbot Chatbot { get; set; }
+
+        [JsonProperty("available_licenses")]
+        public string AvailableLicenses { get; set; }
+
+        [JsonProperty("current_time")]
+        public DateTimeOffset CurrentTime { get; set; }
+
+        [JsonProperty("access_ai")]
+        public bool AccessAi { get; set; }
+    }
+
+    public partial class Chatbot
+    {
+        [JsonProperty("bot_used")]
+        public int BotUsed { get; set; }
+
+        [JsonProperty("available_bots")]
+        public int AvailableBots { get; set; }
+    }
+
+    public partial class Redact
+    {
+        [JsonProperty("pages_used")]
+        public int PagesUsed { get; set; }
+
+        [JsonProperty("available_pages")]
+        public int AvailablePages { get; set; }
+    }
+}

+ 130 - 0
DocAI/DocAI.cs

@@ -0,0 +1,130 @@
+using KdanCommon.DocAI.Data;
+using KdanCommon.Helpers;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net.Http;
+using System.Net.Mime;
+using System.Text;
+using System.Threading.Tasks;
+using Windows.Media.Protection.PlayReady;
+using Windows.Storage;
+using Windows.Web.Http.Headers;
+using System.Xml.Linq;
+using System.IO.Pipes;
+
+namespace KdanCommon.DocAI
+{
+    public class DocAI
+    {
+        // DocAI website : https://kdanai.azurewebsites.net/swagger/index.html
+
+        private static string DocAIDomain = "https://kdanai.azurewebsites.net";
+        private static string DocAIPreparingDomain = "https://kdanai-test.azurewebsites.net";
+
+        private HttpClient _httpClient = null;
+        private string _appName = null;
+        private string _appVersion = null;
+        private string _platform = null;
+
+        private Uri _verifyUserUri = null;
+        private Uri _redactInfoExtractUri = null;
+
+        public DocAI(string appName, string appVersion, bool isPreparing)
+        {
+            _httpClient = new HttpClient();
+            _appName = appName;
+            _appVersion = appVersion;
+            _platform = "uwp";
+
+            if (isPreparing)
+            {
+                _verifyUserUri = new Uri($"{DocAIPreparingDomain}/verify_user");
+                _redactInfoExtractUri = new Uri($"{DocAIPreparingDomain}/redact_info_extract");
+            }
+            else
+            {
+                _verifyUserUri = new Uri($"{DocAIDomain}/verify_user");
+                _redactInfoExtractUri = new Uri($"{DocAIDomain}/redact_info_extract");
+            }
+        }
+
+        ~DocAI()
+        {
+            _httpClient.Dispose();
+        }
+
+        #region Public Method
+        public void SetAccessToken(string accessToken)
+        {
+            _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
+        }
+
+        public async Task<VerifyUserResponse> VerifyUser()
+        {
+            VerifyUserResponse response = null;
+            HttpResponseMessage result = null;
+            try
+            {
+                result = await _httpClient.PostAsync(_verifyUserUri, new MultipartFormDataContent());
+            }
+            catch { }
+            if (result != null && result.StatusCode == System.Net.HttpStatusCode.OK)
+            {
+                var jsonString = await result.Content.ReadAsStringAsync();
+                response = JsonTool.DeserializeJSON<VerifyUserResponse>(jsonString);
+            }
+            return response;
+        }
+
+        public async Task<RedactInfoExtractResponse> RedactInfoExtract(StorageFile file, string fileName, string md5, string redactInfo, string docType, string pageRange = null, string redactCustomInfo = null)
+        {
+            RedactInfoExtractResponse response = null;
+            using (var fileStream = await file.OpenStreamForReadAsync())
+            {
+                var content = new MultipartFormDataContent();
+                var streamContent = new StreamContent(fileStream);
+                streamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
+                streamContent.Headers.ContentDisposition.Name = "redact_file";
+                streamContent.Headers.ContentDisposition.FileName = fileName;
+                content.Add(streamContent);
+                content.Add(CreateStringContent(md5, "md5"));
+                content.Add(CreateStringContent(redactInfo, "redact_info"));
+                content.Add(CreateStringContent(_appName, "app_name"));
+                content.Add(CreateStringContent(_appVersion, "app_version"));
+                content.Add(CreateStringContent(_platform, "platform"));
+                content.Add(CreateStringContent(docType, "doc_type"));
+                if (!string.IsNullOrEmpty(redactCustomInfo))
+                    content.Add(CreateStringContent(redactCustomInfo, "redact_custom_info"));
+                if (!string.IsNullOrEmpty(pageRange))
+                    content.Add(CreateStringContent(pageRange, "page_range"));
+                HttpResponseMessage result = null;
+                try
+                {
+                    result = await _httpClient.PostAsync(_redactInfoExtractUri, content);
+                }
+                catch { }
+                if (result != null && result.StatusCode == System.Net.HttpStatusCode.OK)
+                {
+                    var jsonString = await result.Content.ReadAsStringAsync();
+                    response = JsonTool.DeserializeJSON<RedactInfoExtractResponse>(jsonString);
+                }
+            }
+            return response;
+        }
+
+        #endregion
+
+        #region Private Method
+        private StringContent CreateStringContent(string content, string name)
+        {
+            var stringContent = new StringContent(content);
+            stringContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
+            stringContent.Headers.ContentDisposition.Name = name;
+            return stringContent;
+        }
+        #endregion
+    }
+}

+ 3 - 0
KdanCommon.csproj

@@ -126,6 +126,9 @@
     <Compile Include="CMSCollection\Data\ViewerEventBarSettingResponse.cs" />
     <Compile Include="CMSCollection\Data\WindowsCardsResponse.cs" />
     <Compile Include="CMSCollection\Data\WindowsEventbarResponse.cs" />
+    <Compile Include="DocAI\Data\RedactInfoExtractResponse.cs" />
+    <Compile Include="DocAI\Data\VerifyUserResponse.cs" />
+    <Compile Include="DocAI\DocAI.cs" />
     <Compile Include="GoogleCloud\Data\Lang.cs" />
     <Compile Include="GoogleCloud\Data\Vision\ImgesRequest.cs" />
     <Compile Include="GoogleCloud\Data\Vision\ImgesResponse.cs" />