You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
7.7 KiB

  1. ///-----------------------------------------------------------------------
  2. /// <copyright file="FileUploadHelper.cs" company="Origtek">
  3. /// ArsenalDBMgr belongs to Copyright (c) Origtek. All rights reserved.
  4. /// 程式代號: FileUploadHelper
  5. /// 程式名稱: 檔案上傳Helper
  6. /// 程式說明: 提供上傳功能
  7. /// 起始作者: Hercules
  8. /// 起始日期: 2017/05/15
  9. /// 最新修改人: Hercules
  10. /// 最新修日期: 2017/05/18 17:45:54
  11. /// </copyright>
  12. ///-----------------------------------------------------------------------
  13. #region 程式異動記錄
  14. /// xx.YYYY/MM/DD VER AUTHOR COMMENTS(說明修改的內容)
  15. /// 01.2017/05/15 1.000 Hercules 檔案上傳 獨立成Helper(RV.4293)
  16. /// 02.2017/05/18 1.001 Hercules 補程式Header註解(RV.4375)
  17. #endregion
  18. using CounsellorBL.BLStructure;
  19. using MonumentDefine;
  20. using Newtonsoft.Json.Linq;
  21. using OT.COM.ArsenalDB;
  22. using OT.COM.SignalerMessage;
  23. using SoldierData.EnterprizeV4;
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Globalization;
  27. using System.IO;
  28. namespace CounsellorBL.Helper
  29. {
  30. /// <summary>
  31. /// 類別名稱: FileUploadHelper
  32. /// 類別說明 檔案上傳Helper
  33. /// 起始作者: Hercules
  34. /// 起始日期: 2017/05/15
  35. /// 最新修改人: Hercules
  36. /// 最新修改日: 2017/05/15
  37. /// </summary>
  38. public partial class FileUploadHelper : DBService
  39. {
  40. /// <summary>
  41. /// 檔案上傳Helper必要資訊資料結構實體
  42. /// </summary>
  43. private readonly FileUploadInfo _fuiInst = null;
  44. public override string MainTable => typeof(tb_sys_uploadlog).Name;
  45. /// <summary>
  46. /// 函式名稱:FileUploadHelper
  47. /// 函式說明:建構式
  48. /// 起始作者:
  49. /// 起始日期:
  50. /// 最新修改人:
  51. /// 最新修改日:
  52. /// </summary>
  53. /// <param name="i_fuiInst">檔案上傳Helper必要資訊資料結構,可透過 DBService.GetFileUploadInfo快速取得</param>
  54. /// 參數說明
  55. /// </param>
  56. /// <returns>
  57. /// 回傳
  58. /// </returns>
  59. public FileUploadHelper(FileUploadInfo i_fuiInst, CRequestMessage i_crm)
  60. {
  61. OriRequest = i_crm;
  62. _fuiInst = i_fuiInst;
  63. }
  64. /// <summary>
  65. /// 函式名稱:UploadFile
  66. /// 函式說明:上傳檔案
  67. /// 起始作者:
  68. /// 起始日期:
  69. /// 最新修改人:
  70. /// 最新修改日:
  71. /// </summary>
  72. /// <param name="i_crm">參數說明i_crmInput(Object):前端上傳所需資訊</param>
  73. /// <param name="o_lRes">上傳後所有檔案路徑</param>
  74. /// <param name="i_sfolderpath">指定上傳路徑</param>
  75. /// <returns>錯誤訊息,若無則回傳Null</returns>
  76. /// <summary>
  77. public string UploadFile(CRequestMessage i_crm, out List<tb_sys_uploadlog> o_lRes, string i_sfolderpath)
  78. {
  79. o_lRes = null;
  80. return i_crm != null ? getUploadFiles(i_crm, out o_lRes, i_sfolderpath) : MessageWording.PARAM_NOT_EXPECTED;
  81. }
  82. /// <summary>
  83. /// 函式名稱:getUploadFiles
  84. /// 函式說明:實際檔案上傳程式
  85. /// 起始作者:
  86. /// 起始日期:
  87. /// 最新修改人:
  88. /// 最新修改日:
  89. /// </summary>
  90. /// <param name="i_crm">參數說明i_crmInput(Object):前端上傳所需資訊</param>
  91. /// <param name="o_lRes">上傳後所有檔案路徑</param>
  92. /// <param name="i_sfolderpath">指定上傳路徑</param>
  93. /// <returns>錯誤訊息,若無則回傳Null</returns>
  94. /// <summary>
  95. private string getUploadFiles(CRequestMessage i_crmInput, out List<tb_sys_uploadlog> o_lRes, string i_sfolderpath = null)
  96. {
  97. List<tb_sys_uploadlog> lRes = null;
  98. string sMsg = null;
  99. do
  100. {
  101. List<UploadFiles> lResTemp = new List<UploadFiles>();
  102. if (!(i_crmInput.param[BLWording.FILES] is JArray jaObjects) || jaObjects.Count == 0)
  103. {
  104. break;
  105. }
  106. if (_fuiInst.Logger == null || _fuiInst.RootPath == null)
  107. {
  108. sMsg = MessageWording.PARAM_NOT_EXPECTED;
  109. break;
  110. }
  111. string sFolderPath = generateUploadFolderPath(i_sfolderpath);
  112. List<tb_sys_uploadlog> lUploadLogs = new List<tb_sys_uploadlog>();
  113. foreach (JToken jo in jaObjects)
  114. {
  115. UploadFiles uf = new UploadFiles()
  116. {
  117. UploadName = jo[BLWording.NAME].ToString(),
  118. };
  119. string sBase64 = (string)jo[BLWording.BSTRING];
  120. int nIdx = sBase64.IndexOf(',', StringComparison.OrdinalIgnoreCase);
  121. uf.FileMeta = sBase64.Substring(0, nIdx);
  122. var fileName = generateUploadFolderName(i_crmInput);
  123. byte[] bRawData = Convert.FromBase64String(sBase64.Substring(nIdx + 1));
  124. string sFilePath = Path.Combine(sFolderPath, string.Format(CultureInfo.CurrentCulture, "{0}{1}", fileName, Path.GetExtension(jo[BLWording.NAME].ToString())));
  125. System.IO.File.WriteAllBytes(sFilePath, bRawData);
  126. uf.FilePath = sFilePath;
  127. lResTemp.Add(uf);
  128. lUploadLogs.Add(new tb_sys_uploadlog()
  129. {
  130. uid = Guid.NewGuid().ToString(),
  131. module = i_crmInput.module,
  132. name = jo[BLWording.NAME].ToString(),
  133. path = sFilePath.Substring(_fuiInst.RootPath.Length)
  134. });
  135. }
  136. lRes = lUploadLogs;
  137. if (lRes.Count > 0)
  138. {
  139. List<Command> lcInsert = new List<Command>();
  140. foreach (tb_sys_uploadlog ul in lUploadLogs)
  141. {
  142. lcInsert.Add(Command.SetupInsertCmd(ul));
  143. }
  144. this.OriRequest = i_crmInput;
  145. ArsenalInterface ai = ArsenalDBMgr.GetInst(lcInsert[0], GetDefaultSystemColumnInfo());
  146. ai.RunEditCmds(lcInsert);
  147. }
  148. }
  149. while (false);
  150. o_lRes = lRes;
  151. return sMsg;
  152. }
  153. public string UploadLocalFile(string i_sLocalFile, out tb_sys_uploadlog o_ulData)
  154. {
  155. string sMsg = null;
  156. tb_sys_uploadlog ulRes = null;
  157. do
  158. {
  159. if (i_sLocalFile == null)
  160. {
  161. sMsg = MessageWording.PARAM_NOT_EXPECTED;
  162. break;
  163. }
  164. string[] saPath = i_sLocalFile.Split("\\/".ToCharArray());
  165. tb_sys_uploadlog ulInsert = new tb_sys_uploadlog()
  166. {
  167. uid = Guid.NewGuid().ToString(),
  168. module = OriRequest?.module,
  169. path = i_sLocalFile,
  170. name = saPath[^1]
  171. };
  172. Command cInsert = Command.SetupInsertCmd(ulInsert);
  173. ArsenalInterface ai = ArsenalDBMgr.GetInst(cInsert, GetDefaultSystemColumnInfo());
  174. ai.RunEditSingleCmd(cInsert);
  175. if (!cInsert.IsSuccess)
  176. {
  177. sMsg = GetLastErrorCode(cInsert);
  178. break;
  179. }
  180. ulRes = ulInsert;
  181. }
  182. while (false);
  183. o_ulData = ulRes;
  184. return sMsg;
  185. }
  186. }
  187. }