UpLoadFileRequest.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /* ==============================================================================
  2. * 功能描述:UpLoadFileRequest
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/3/23 15:19:52
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Text;
  13. using System.Windows;
  14. using SAGA.MBI.Common;
  15. using Utilities;
  16. using Aliyun.OSS;
  17. using Aliyun.OSS.Common;
  18. using Newtonsoft.Json.Linq;
  19. using SAGA.DotNetUtils;
  20. using SAGA.DotNetUtils.Encrypt;
  21. using SAGA.DotNetUtils.Extend;
  22. using SAGA.DotNetUtils.FileOperate;
  23. using SAGA.DotNetUtils.Http;
  24. using SAGA.DotNetUtils.Logger;
  25. using SAGA.DotNetUtils.Others;
  26. using SAGA.MBI.DataArrange;
  27. using SAGA.MBI.FileStream;
  28. using SAGA.MBI.JsonConvert;
  29. using SAGA.MBI.Login;
  30. using SAGA.MBI.Tools;
  31. using SAGA.RevitUtils;
  32. namespace SAGA.MBI.RequestData
  33. {
  34. /// <summary>
  35. /// UpLoadFileRequest
  36. /// </summary>
  37. public class UpLoadFileRequest
  38. {
  39. /// <summary>
  40. /// 是否使用oss
  41. /// </summary>
  42. /// <returns></returns>
  43. public static bool UseOss()
  44. {
  45. var flag = ConfigurationUtil.Default.GetSettingValue("UpDownFlag");
  46. return flag==null||flag.Trim() == "0";
  47. }
  48. #region Download
  49. /// <summary>
  50. /// 下载指定文件到指定
  51. /// </summary>
  52. /// <param name="downPath"></param>
  53. /// <param name="fileName"></param>
  54. /// <returns></returns>
  55. public static bool DownLoadRevitFile(string downPath, string fileName)
  56. {
  57. try
  58. {
  59. if (!Directory.Exists(downPath))
  60. Directory.CreateDirectory(downPath);
  61. var filePath = Path.Combine(downPath, fileName);
  62. //如果文件存在,删除重新下载
  63. File.Delete(filePath);
  64. //文件下载
  65. var url =
  66. $"{MBIConst.ImageServiceLocalHost}image-service/common/file_get/{fileName}?systemId={MBIConst.RevitServiceId}";
  67. WebClient wc = new WebClient();
  68. wc.Headers.Add("postman-token", "8efb60cb-a34c-592e-6e61-5412b1dfc1bb");
  69. wc.Headers.Add("cache-control", "no-cache");
  70. wc.DownloadFile(new Uri(url), filePath);
  71. wc.Dispose();
  72. }
  73. catch (Exception e)
  74. {
  75. Console.WriteLine(e);
  76. return false;
  77. }
  78. finally
  79. {
  80. }
  81. return true;
  82. }
  83. #endregion
  84. #region DownLoadAsync
  85. private static WinMFMDownloadProcress windown = null;
  86. public static WinMFMDownloadProcress GetProcessWin()
  87. {
  88. return windown;
  89. }
  90. /// <summary>
  91. /// 显示下载窗体
  92. /// </summary>
  93. public static void ShowDownloadWindow(string floorName, string fullPath, int count, int cur)
  94. {
  95. if (windown == null)
  96. {
  97. windown = new WinMFMDownloadProcress();
  98. windown.Show();
  99. }
  100. windown.LabelFloorName = $"正在下载{floorName}层模型文件,请稍后。。。";
  101. windown.LabelPath = fullPath;
  102. windown.Count = count;
  103. windown.Cur = cur;
  104. }
  105. /// <summary>
  106. /// 下载指定文件到指定
  107. /// </summary>
  108. /// <param name="downPath"></param>
  109. /// <param name="fileName"></param>
  110. /// <returns></returns>
  111. public static bool DownLoadFileAsync(Tuple<string, int, string> tuple, int count, int cur)
  112. {
  113. if (!UpLoadFileRequest.UseOss())
  114. {
  115. return FileContentTransferUnit.DownLoadFileAsync(tuple, count, cur);
  116. }
  117. bool result = true;
  118. string fullPath = tuple.Item1;
  119. string floorName = tuple.Item3;
  120. string cloudName = CommonTool.GetCloudRevitName(fullPath);
  121. int state = tuple.Item2;
  122. if (state == 2)
  123. FloorFileOperate.BakFile(fullPath);
  124. string key = $"{MBIConst.OssClientFileKey}{cloudName}";
  125. DownLoadNew:
  126. OssClient client = new OssClient(MBIConst.Endpoint, MBIConst.AccessKeyId, MBIConst.AccessKeySecret);
  127. int timeoutNum = 20;
  128. long totalLength = 0;
  129. try
  130. {
  131. string bucketName = MBIConst.BucketName;
  132. ShowDownloadWindow(tuple.Item3, fullPath, count, cur);
  133. var getObjectRequest = new GetObjectRequest(bucketName, key);
  134. //getObjectRequest.StreamTransferProgress += DownStreamProgressCallback;
  135. var buffer = new byte[1024 * 1024];
  136. long stargIndex = 0;
  137. getObjectRequest.StreamTransferProgress += (s, args) =>
  138. {
  139. var tempTrans = stargIndex + args.TransferredBytes;
  140. var tempTotal = stargIndex + args.TotalBytes;
  141. var currentData = Math.Round(((double)tempTrans)/ tempTotal *100d, 3);
  142. Debug.WriteLine("ProgressCallback - TotalBytes:{0}M, TransferredBytes:{1}M,上传百分比:{2}%",
  143. Math.Round(tempTotal / 1024d / 1024,3), Math.Round(tempTrans / 1024d / 1024,3), currentData);
  144. windown?.Update(currentData);
  145. //currentData >= 100d
  146. if (tempTrans >= tempTotal && windown!=null&&windown.Count == windown.Cur)
  147. {
  148. //MessageBox.Show("数据上传完毕!");
  149. CloseDownloadWin(true, false);
  150. }
  151. };
  152. using (var fs = new MemoryStream())
  153. {
  154. ReDownload:
  155. getObjectRequest.SetRange(stargIndex, -1);
  156. OssObject ossObject = client.GetObject(getObjectRequest);
  157. //设置下载超时时间
  158. ossObject.ResponseStream.ReadTimeout = 60 * 1000;//*20;
  159. if (totalLength == 0)
  160. {
  161. totalLength = ossObject.ContentLength;
  162. }
  163. using (var stream = ossObject.Content)
  164. {
  165. var bytesRead = 0;
  166. try
  167. {
  168. while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
  169. {
  170. fs.Write(buffer, 0, bytesRead);
  171. }
  172. }
  173. catch (WebException e)
  174. {
  175. #region 超时重试
  176. if (0 < timeoutNum-- && e.Message.Contains("超时"))
  177. {
  178. stargIndex = fs.Length;
  179. //timeoutNum--;
  180. goto ReDownload;
  181. }
  182. throw;
  183. #endregion
  184. }
  185. if (fs.Length < totalLength)
  186. {
  187. stargIndex = fs.Length;
  188. goto ReDownload;
  189. }
  190. }
  191. FileStreamOperate.WriteFile(fs.ToArray(), fullPath);
  192. #region 下载后Md5较验
  193. string md5 = MD5Tools.GetMD5HashFromFileByBase64(fullPath);
  194. //下载后的文件md5校验
  195. if (client.GetObjectMetadata(bucketName, key).ContentMd5 != md5)
  196. {
  197. string per = Math.Round((fs.Length / (double)totalLength) * 100, 3) + "%";
  198. string errorMessage = ($"楼层 {floorName} MD5校验失败,本地与服务器不一致!下载进度{per}");
  199. if (fs.Length== totalLength)
  200. {
  201. if (MessageShowBase.Question(errorMessage + "\r\n是否重新下载该层文件"))
  202. {
  203. goto DownLoadNew;
  204. }
  205. }
  206. else
  207. {
  208. MessageShowBase.Infomation(errorMessage);
  209. }
  210. MailLog.Log("下载MD5异常", errorMessage);
  211. result = false;
  212. }
  213. #endregion
  214. }
  215. Log4Net.Debug($"Get object:{key} succeeded");
  216. }
  217. catch (Exception ex)
  218. {
  219. MessageShowBase.Infomation("Revit文件下载失败!\r\n"+ex.Message+"\r\n"+ex.StackTrace);
  220. result = false;
  221. CloseDownloadWin(result,false);
  222. }
  223. return result;
  224. }
  225. /// <summary>
  226. /// 下载进度条
  227. /// </summary>
  228. /// <param name="sender"></param>
  229. /// <param name="args"></param>
  230. private static void DownStreamProgressCallback(object sender, StreamTransferProgressArgs args)
  231. {
  232. try
  233. {
  234. var currentData = Math.Round(args.TransferredBytes * 100d / args.TotalBytes, 3);
  235. Debug.WriteLine("ProgressCallback - TotalBytes:{0}M, TransferredBytes:{1}M,上传百分比:{2}%",
  236. args.TotalBytes / 1024 / 1024, args.TransferredBytes / 1024 / 1024, currentData);
  237. windown?.Update(currentData);
  238. if (currentData >= 100d && windown.Count == windown.Cur)
  239. {
  240. //MessageBox.Show("数据上传完毕!");
  241. CloseDownloadWin(true, false);
  242. }
  243. }
  244. catch (Exception ex)
  245. {
  246. throw;
  247. }
  248. }
  249. /// <summary>
  250. /// 关闭下载窗口
  251. /// </summary>
  252. public static void CloseDownloadWin(bool issucess, bool hasTip = true)
  253. {
  254. if (windown != null)
  255. {
  256. windown?.Close();
  257. windown = null;
  258. }
  259. if (hasTip)
  260. MessageShowBase.Infomation(issucess ? "Revit模型文件下载成功!" : "Revit模型文件下载失败!");
  261. }
  262. #endregion
  263. #region Upload
  264. /// <summary>
  265. /// 将本地文件上传到指定的服务器(HttpWebRequest方法)
  266. /// </summary>
  267. /// <returns></returns>
  268. public static string UpLoadFile(string downPath, string fileName, string url)
  269. {
  270. var responseValue = string.Empty;
  271. var filePath = downPath;
  272. if (!File.Exists(downPath))
  273. filePath = Path.Combine(downPath, fileName);
  274. try
  275. {
  276. var request = (HttpWebRequest)WebRequest.Create(url);
  277. request.Timeout = 60000;
  278. request.Method = "POST";
  279. //每次上传4k
  280. int bufferLength = 40960;
  281. byte[] buffer = new byte[bufferLength];
  282. // 要上传的文件
  283. System.IO.FileStream fs = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);
  284. BinaryReader r = new BinaryReader(fs);
  285. int size = r.Read(buffer, 0, bufferLength);
  286. //request.ContentLength = bufferLength;
  287. Stream postStream = null;
  288. try
  289. {
  290. while (size > 0)
  291. {
  292. postStream = request.GetRequestStream();
  293. postStream.Write(buffer, 0, bufferLength);
  294. size = r.Read(buffer, 0, bufferLength);
  295. }
  296. }
  297. catch (Exception e)
  298. {
  299. Console.WriteLine(e);
  300. throw;
  301. }
  302. finally
  303. {
  304. fs.Close();
  305. postStream?.Close();
  306. r.Close();
  307. }
  308. //返回数据
  309. using (var response = (HttpWebResponse)request.GetResponse())
  310. {
  311. if (response.StatusCode != HttpStatusCode.OK)
  312. {
  313. var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
  314. throw new ApplicationException(message);
  315. }
  316. // grab the response
  317. using (var responseStream = response.GetResponseStream())
  318. {
  319. if (responseStream != null)
  320. using (var reader = new StreamReader(responseStream))
  321. {
  322. responseValue = reader.ReadToEnd();
  323. }
  324. }
  325. }
  326. }
  327. catch (Exception e)
  328. {
  329. MessageShowBase.Show(e);
  330. }
  331. return responseValue;
  332. }
  333. /// <summary>
  334. /// 上传到文件服务器的Revit目录中
  335. /// </summary>
  336. /// <returns></returns>
  337. public static string UpLoadRevitFile(string downPath, string fileName)
  338. {
  339. string systemId = MBIConst.RevitServiceId;
  340. string secret = MBIConst.RevitServiceSecret;
  341. //文件下载
  342. string url =
  343. $"{MBIConst.ImageServiceLocalHost}image-service/common/file_upload?systemId={systemId}&secret={secret}&key={fileName}&overwrite=true";
  344. return UpLoadFile(downPath, fileName, url);
  345. }
  346. /// <summary>
  347. /// 上传到文件服务器的Dev目录中
  348. /// </summary>
  349. /// <returns></returns>
  350. public static string UpLoadDevFile(string downPath, string fileName)
  351. {
  352. string systemId = MBIConst.DevServiceId;
  353. string secret = MBIConst.DevServiceSecret;
  354. //文件下载
  355. string url =
  356. $"{MBIConst.ImageServiceLocalHost}image-service/common/file_upload?systemId={systemId}&secret={secret}&key={fileName}&overwrite=true";
  357. return UpLoadFile(downPath, fileName, url);
  358. }
  359. /// <summary>
  360. /// 上传到图片服务器的Dev目录中
  361. /// </summary>
  362. /// <returns></returns>
  363. public static string UpLoadDevImage(string downPath, string fileName)
  364. {
  365. string systemId = MBIConst.DevServiceId;
  366. string secret = MBIConst.DevServiceSecret;
  367. //文件下载
  368. string url =
  369. $"{MBIConst.ImageServiceLocalHost}image-service/common/image_upload?systemId={systemId}&secret={secret}&key={fileName}&overwrite=true";
  370. return UpLoadFile(downPath, fileName, url);
  371. }
  372. #endregion
  373. #region UploadAsync
  374. private static WinMFMUploadProcress win = null;
  375. public static WinMFMUploadProcress GetUpProcessWin()
  376. {
  377. return win;
  378. }
  379. /// <summary>
  380. /// 显示上传窗体
  381. /// </summary>
  382. /// <param name="filePath"></param>
  383. public static void ShowUploadWindow(string floorName, string filePath, int count, int cur)
  384. {
  385. if (win == null)
  386. {
  387. win = new WinMFMUploadProcress();
  388. win.Show();
  389. }
  390. win.FloorName = $"正在上传{floorName}层模型文件,请稍后。。。";
  391. win.LabelPath = filePath;
  392. win.Count = count;
  393. win.Cur = cur;
  394. }
  395. /// <summary>
  396. /// 关闭上传窗体
  397. /// </summary>
  398. public static void CloseUploadWindow(bool issucess, bool hasTip = false)
  399. {
  400. if (win != null)
  401. {
  402. win?.Close();
  403. win = null;
  404. }
  405. if (hasTip)
  406. MessageBox.Show(issucess ? "Revit模型文件上传成功!" : "Revit文件上传失败!");
  407. }
  408. /// <summary>
  409. /// 上传Revit文件到服务器
  410. /// </summary>
  411. /// <param name="filePath"></param>
  412. /// <param name="fileName"></param>
  413. /// <returns></returns>
  414. public static bool UpLoadRevitAsync(string floorName, string filePath, string fileName, int count, int cur)
  415. {
  416. if (!UpLoadFileRequest.UseOss())
  417. {
  418. return FileContentTransferUnit.UpLoadRevitAsync(floorName, filePath, fileName,count,cur);
  419. }
  420. bool result = true;
  421. string key = $"{MBIConst.OssClientFileKey}{fileName}";
  422. OssClient client = new OssClient(MBIConst.Endpoint, MBIConst.AccessKeyId, MBIConst.AccessKeySecret);
  423. try
  424. {
  425. using (var fs = new MemoryStream(FileStreamOperate.ReadFile(filePath)))
  426. {
  427. string bucketName = MBIConst.BucketName;
  428. var putObjectRequest = new PutObjectRequest(bucketName, key, fs);
  429. ShowUploadWindow(floorName, filePath, count, cur);
  430. putObjectRequest.StreamTransferProgress += StreamProgressCallback;
  431. client.PutObject(putObjectRequest);
  432. #region 上传后MD5较验
  433. string md5 = MD5Tools.GetMD5HashFromFileByBase64(filePath);
  434. //上传后的文件md5校验
  435. if (client.GetObjectMetadata(bucketName, key).ContentMd5 != md5)
  436. {
  437. string errorMessage = ($"楼层 {floorName} MD5校验失败,本地与服务器不一致!");
  438. MailLog.Log("上传MD5异常", errorMessage);
  439. MessageShowBase.Infomation(errorMessage);
  440. //不关闭窗体
  441. // throw new Exception(errorMessage);
  442. }
  443. #endregion
  444. }
  445. Console.WriteLine("Put object:{0} succeeded", key);
  446. }
  447. catch (Exception ex)
  448. {
  449. MessageShowBase.Infomation("Revit文件上传载失败!\r\n" + ex.Message + "\r\n" + ex.StackTrace);
  450. result = false;
  451. CloseUploadWindow(result,false);
  452. }
  453. return result;
  454. }
  455. /// <summary>
  456. /// 上传进度条
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="args"></param>
  460. private static void StreamProgressCallback(object sender, StreamTransferProgressArgs args)
  461. {
  462. var currentData = Math.Round(args.TransferredBytes * 100d / args.TotalBytes, 3);
  463. Debug.WriteLine("ProgressCallback - TotalBytes:{0}M, TransferredBytes:{1}M,上传百分比:{2}%",
  464. args.TotalBytes / 1024 / 1024, args.TransferredBytes / 1024 / 1024, currentData);
  465. win?.Update(currentData);
  466. if (currentData >= 100d)
  467. {
  468. CloseUploadWindow(true, false);
  469. }
  470. }
  471. #endregion
  472. #region Del
  473. /// <summary>
  474. /// 删除服务器文件
  475. /// </summary>
  476. /// <returns></returns>
  477. public static string DeleteFile(string url, string key)
  478. {
  479. //文件下载
  480. try
  481. {
  482. JObject jObject = new JObject();
  483. JArray keyArray = new JArray();
  484. keyArray.Add(key);
  485. jObject.Add("keys", keyArray);
  486. string postData = jObject.ToString();
  487. RestClient client = new RestClient(url, HttpVerb.POST, postData);
  488. string request = client.PostRequest();
  489. return request;
  490. }
  491. catch (Exception e)
  492. {
  493. MessageShowBase.Show(e);
  494. }
  495. return null;
  496. }
  497. /// <summary>
  498. /// 删除文件服务器Revit目录下的数据
  499. /// </summary>
  500. /// <param name="key"></param>
  501. /// <returns></returns>
  502. public static string DeleteRevitFile(string key)
  503. {
  504. string systemId = MBIConst.RevitServiceId;
  505. string secret = MBIConst.RevitServiceSecret;
  506. string url =
  507. $"{MBIConst.ImageServiceLocalHost}image-service/common/files_delete?systemId={systemId}&secret={secret}";
  508. return DeleteFile(url, key);
  509. }
  510. /// <summary>
  511. /// 删除文件服务器Dev目录下的数据
  512. /// </summary>
  513. /// <param name="key"></param>
  514. /// <returns></returns>
  515. public static string DeleteDevFile(string key)
  516. {
  517. string systemId = MBIConst.DevServiceId;
  518. string secret = MBIConst.DevServiceSecret;
  519. string url =
  520. $"{MBIConst.ImageServiceLocalHost}image-service/common/files_delete?systemId={systemId}&secret={secret}";
  521. return DeleteFile(url, key);
  522. }
  523. /// <summary>
  524. /// 删除图片服务器Dev目录的图片
  525. /// </summary>
  526. /// <param name="key"></param>
  527. /// <returns></returns>
  528. public static string DeleteDevImage(string key)
  529. {
  530. string systemId = MBIConst.DevServiceId;
  531. string secret = MBIConst.DevServiceSecret;
  532. string url =
  533. $"{MBIConst.ImageServiceLocalHost}image-service/common/images_delete?systemId={systemId}&secret={secret}";
  534. return DeleteFile(url, key);
  535. }
  536. #endregion
  537. #region 一级底层封装
  538. /*
  539. * 1、上传接口:URL,(传路径|传bytes) 系统及Id
  540. * 2、下载接口:URL,(传路径|返回bytes)
  541. * 3、返回文件列表:前缀,数量默认
  542. * 4、删除文件:(列表|单独文件)
  543. */
  544. public static bool UploadFile(FileServiceSetting setting, string key, Byte[] bytes)
  545. {
  546. try
  547. {
  548. string url =
  549. $"{MBIConst.ImageServiceLocalHost}image-service/common/file_upload?systemId={setting.SystemId}&secret={setting.Secret}&key={key}&overwrite=true";
  550. MemoryStream stream = new MemoryStream(bytes);
  551. var result = RequestClient.UploadFile(url, stream);
  552. JObject rj = JObject.Parse(result);
  553. if (rj["Result"].ToString() == "success")
  554. {
  555. return true;
  556. }
  557. //结果暂时不处理
  558. throw new Exception(rj["ResultMsg"].ToString());
  559. return false;
  560. }
  561. catch (Exception e)
  562. {
  563. throw;
  564. }
  565. }
  566. public static bool UploadFile(FileServiceSetting setting, string key, string filePath)
  567. {
  568. try
  569. {
  570. var bytes = File.ReadAllBytes(filePath);
  571. var result = UploadFile(setting, key, bytes);
  572. //结果暂时不处理
  573. return true;
  574. }
  575. catch (Exception)
  576. {
  577. throw;
  578. }
  579. }
  580. public static byte[] DownloadFile(FileServiceSetting setting, string key)
  581. {
  582. var url =
  583. $"{MBIConst.ImageServiceLocalHost}image-service/common/file_get/{key}?systemId={setting.SystemId}";
  584. try
  585. {
  586. return RequestClient.DownloadFile(url);
  587. }
  588. catch (Exception e)
  589. {
  590. throw;
  591. }
  592. return null;
  593. }
  594. public static bool DownloadFile(FileServiceSetting setting, string key, string filePath)
  595. {
  596. try
  597. {
  598. string parentPath = Directory.GetParent(filePath).FullName;
  599. if (!Directory.Exists(parentPath))
  600. Directory.CreateDirectory(parentPath);
  601. //if (!File.Exists(filePath))
  602. // File.Create(filePath);
  603. var reuslt = DownloadFile(setting, key);
  604. if (reuslt == null)
  605. throw new Exception("下载文件失败");
  606. File.WriteAllBytes(filePath, reuslt);
  607. return true;
  608. }
  609. catch (Exception)
  610. {
  611. throw;
  612. }
  613. }
  614. /// <summary>
  615. /// 根据指定前缀获取文件列表
  616. /// </summary>
  617. /// <param name="prefix"></param>
  618. /// <returns></returns>
  619. public static List<string> GetFiles(FileServiceSetting setting, string prefix)
  620. {
  621. List<string> files = new List<string>();
  622. string url = $"{MBIConst.ImageServiceLocalHost}image-service/common/files_list?systemId={setting.SystemId}&secret={setting.Secret}";
  623. JObject jObject = new JObject();
  624. jObject.Add("prefix", prefix);
  625. var result = RequestClient.Post(url, jObject);
  626. JObject resultJson = JObject.Parse(result);
  627. files = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(resultJson["Content"].ToString()) ?? new List<string>();
  628. return files;
  629. }
  630. /// <summary>
  631. /// 删除传入文件信息
  632. /// </summary>
  633. /// <param name="setting"></param>
  634. /// <param name="inputFiles"></param>
  635. /// <returns>成功删除的文件</returns>
  636. public static List<string> DeleteFiles(FileServiceSetting setting, List<string> inputFiles)
  637. {
  638. List<string> delFiles = new List<string>();
  639. if (inputFiles.IsNullOrEmptyExt())
  640. return delFiles;
  641. string url = $"{MBIConst.ImageServiceLocalHost}image-service/common/files_delete?systemId={setting.SystemId}&secret={setting.Secret}";
  642. JObject jObject = new JObject();
  643. JArray array = new JArray();
  644. inputFiles.ForEach(s => array.Add(s));
  645. jObject.Add("keys", array);
  646. var result = RequestClient.Post(url, jObject);
  647. JObject resultJson = JObject.Parse(result);
  648. delFiles = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(resultJson["Content"].ToString()) ?? new List<string>();
  649. return delFiles;
  650. }
  651. public static bool DeleteFile(FileServiceSetting setting, string file)
  652. {
  653. var delFile = DeleteFiles(setting, new List<string>() { file })?.FirstOrDefault();
  654. return delFile == file;
  655. }
  656. #endregion
  657. }
  658. /// <summary>
  659. /// 文件服务设置
  660. /// </summary>
  661. public class FileServiceSetting
  662. {
  663. static FileServiceSetting()
  664. {
  665. RevitSetting = new FileServiceSetting() { SystemId = MBIConst.RevitServiceId, Secret = MBIConst.RevitServiceSecret };
  666. DevSetting = new FileServiceSetting() { SystemId = MBIConst.DevServiceId, Secret = MBIConst.DevServiceSecret };
  667. }
  668. /// <summary>
  669. /// 系统ID
  670. /// </summary>
  671. public string SystemId { get; set; } = "";
  672. /// <summary>
  673. /// 系统密码
  674. /// </summary>
  675. public string Secret { get; set; } = "";
  676. public static FileServiceSetting RevitSetting { get; private set; }
  677. public static FileServiceSetting DevSetting { get; private set; }
  678. }
  679. }