ProjectLogManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Autodesk.Revit.DB;
  2. using FWindSoft.Tools.SystemUtil;
  3. using SAGA.DotNetUtils;
  4. using SAGA.DotNetUtils.Extend;
  5. using SAGA.MBI.Common;
  6. using SAGA.MBI.RequestData;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. namespace SAGA.MBI
  13. {
  14. public class ProjectLogManager
  15. {
  16. #region 键值相关
  17. /// <summary>
  18. /// 创建文件键值
  19. /// </summary>
  20. /// <param name="fileName">无扩展名名称</param>
  21. /// <returns></returns>
  22. public static string CreateLogFileKey(string fileName)
  23. {
  24. return fileName;
  25. }
  26. /// <summary>
  27. /// 创建指定项目的服务器文件键值
  28. /// </summary>
  29. /// <param name="doc"></param>
  30. /// <returns></returns>
  31. public static string CreateLogFileKey(Document doc)
  32. {
  33. string pathName = doc.PathName;
  34. string fileName = pathName.GetFileName();
  35. return CreateLogFileKey(fileName??string.Empty);
  36. }
  37. #endregion
  38. #region 操作相关
  39. /// <summary>
  40. /// 删除本机,本层的日志文件。用于重新下载本层的模型文件时
  41. /// </summary>
  42. /// <param name="fileName"></param>
  43. /// <returns></returns>
  44. public static bool ClearFile(string fileName)
  45. {
  46. try
  47. {
  48. string noExtendFileName = fileName.GetFileName();
  49. string key = CreateLogFileKey(noExtendFileName);
  50. var delFiles = DeleteFiles(new List<string>() { key }) ?? new List<string>();
  51. return 1 == delFiles.Count;
  52. }
  53. catch (Exception)
  54. {
  55. throw;
  56. }
  57. }
  58. /// <summary>
  59. /// 清除指定前缀的文件
  60. /// </summary>
  61. /// <param name="prefix"></param>
  62. /// <returns></returns>
  63. public static bool ClearFiles(string prefix)
  64. {
  65. try
  66. {
  67. var files = GetFiles(prefix);
  68. if (files==null||files.IsNullOrEmptyExt())
  69. return true;
  70. var delFiles = DeleteFiles(files ?? new List<string>());
  71. return files.Count== delFiles.Count;
  72. }
  73. catch (Exception)
  74. {
  75. throw;
  76. }
  77. }
  78. #endregion
  79. #region 获取日志内容
  80. /// <summary>
  81. /// 获取日志内容,
  82. /// </summary>
  83. /// <param name="fileKey">约定为文件名称</param>
  84. /// <returns></returns>
  85. public static string GetLogInfo(string fileKey)
  86. {
  87. var log = string.Empty;
  88. #region 获取服务器日志文件
  89. if (!string.IsNullOrEmpty(fileKey))
  90. {
  91. try
  92. {
  93. var bytes = DownLoadFile(fileKey);
  94. if (bytes != null)
  95. {
  96. log = Encoding.UTF8.GetString(bytes);
  97. }
  98. }
  99. catch (Exception)
  100. {
  101. }
  102. }
  103. #endregion
  104. return log ?? string.Empty;
  105. }
  106. #endregion
  107. #region 本机文件系统维护
  108. public static string LogDirectory
  109. {
  110. get
  111. {
  112. return MBIControl.ProjectCur.LocalPath;
  113. }
  114. }
  115. /// <summary>
  116. /// 获取获取文件内容
  117. /// </summary>
  118. /// <param name="fileKey"></param>
  119. /// <returns></returns>
  120. public static byte[] DownLoadFile(string fileKey)
  121. {
  122. string path = Path.Combine(LogDirectory, fileKey);
  123. if (File.Exists(path))
  124. {
  125. return File.ReadAllBytes(path);
  126. }
  127. return null;
  128. }
  129. public static bool UpLoadFile(string fileKey, byte[] bytes)
  130. {
  131. try
  132. {
  133. string path = Path.Combine(LogDirectory, fileKey);
  134. File.WriteAllBytes(path, bytes);
  135. return true;
  136. }
  137. catch (Exception)
  138. {
  139. return false;
  140. }
  141. }
  142. public static List<string> DeleteFiles(List<string> inputFiles)
  143. {
  144. List<string> results = new List<string>();
  145. for (int i = 0; i < inputFiles.Count; i++)
  146. {
  147. var currentFile = inputFiles[i];
  148. if (File.Exists(currentFile))
  149. {
  150. try
  151. {
  152. File.Delete(currentFile);
  153. results.Add(currentFile);
  154. }
  155. catch (Exception)
  156. {
  157. }
  158. }
  159. }
  160. return results;
  161. }
  162. public static List<string> GetFiles(string prefix)
  163. {
  164. List<string> results=new List<string>();
  165. if (!Directory.Exists(LogDirectory))
  166. return results;
  167. var paths = Directory.GetFiles(LogDirectory);
  168. foreach (var path in paths)
  169. {
  170. FileInfo file=new FileInfo(path);
  171. if (file.Name.StartsWith(prefix))
  172. {
  173. results.Add(path);
  174. }
  175. }
  176. return results;
  177. }
  178. #endregion
  179. }
  180. }