FileServerManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Configuration;
  7. using System.Web;
  8. using System.Collections.Generic;
  9. using Bowin.Common.Utility;
  10. using Microsoft.AspNetCore.Http.Extensions;
  11. using Microsoft.Extensions.Configuration;
  12. namespace Bowin.Common.Files
  13. {
  14. internal class FileServerSettings
  15. {
  16. public string UserName { get; set; }
  17. public string Password { get; set; }
  18. public string Url { get; set; }
  19. }
  20. /// <summary>
  21. /// 处理远程附件的类,由于联达并没有建立文件服务器,需要兼容:
  22. /// 在web调用,为了减少网络流量,就直接处理本地路径的文件;
  23. /// 在服务调用,配置Url、用户名密码后,处理的是远程文件服务器的文件。
  24. /// </summary>
  25. public class FileServerManager
  26. {
  27. public string ErrorMessage;
  28. private CredentialCache myCredentialCache = null;
  29. private static FileServerSettings Settings;
  30. private static bool IsWebLocal
  31. {
  32. get
  33. {
  34. return (HttpHelper.Accessor?.HttpContext != null || string.IsNullOrEmpty(Settings.Url));
  35. }
  36. }
  37. static FileServerManager()
  38. {
  39. var configuration = Utility.Environment.GetConfig("appsettings.json");
  40. Settings = configuration.GetSection("FileServer").Get<FileServerSettings>();
  41. if (Settings == null)
  42. {
  43. Settings = new FileServerSettings();
  44. }
  45. if (!string.IsNullOrEmpty(Settings.Url))
  46. {
  47. if (Settings.Url.Substring(Settings.Url.Length - 1, 1) != "/")
  48. {
  49. Settings.Url = Settings.Url + "/";
  50. }
  51. }
  52. }
  53. public FileServerManager()
  54. {
  55. this.ErrorMessage = "";
  56. if (!string.IsNullOrEmpty(Settings.UserName))
  57. {
  58. this.myCredentialCache = new CredentialCache();
  59. this.myCredentialCache.Add(new Uri(Settings.Url), "NTLM", new NetworkCredential(Settings.UserName, Settings.Password));
  60. }
  61. }
  62. public bool DeleteFile(string filename)
  63. {
  64. this.ErrorMessage = "";
  65. if (IsWebLocal)
  66. {
  67. this.ErrorMessage = "";
  68. try
  69. {
  70. File.Delete(HttpHelper.MapPath(filename));
  71. }
  72. catch (Exception ex)
  73. {
  74. this.ErrorMessage = ex.Message;
  75. return false;
  76. }
  77. }
  78. else
  79. {
  80. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Settings.Url + filename);
  81. request.Credentials = this.myCredentialCache;
  82. request.Method = "DELETE";
  83. WebResponse response = null;
  84. try
  85. {
  86. response = request.GetResponse();
  87. }
  88. catch (WebException ex)
  89. {
  90. this.ErrorMessage = ex.Message;
  91. return false;
  92. }
  93. finally
  94. {
  95. if (response != null)
  96. {
  97. response.Close();
  98. }
  99. }
  100. }
  101. return true;
  102. }
  103. public bool IsFileExist(string filename)
  104. {
  105. this.ErrorMessage = "";
  106. if (IsWebLocal)
  107. {
  108. return File.Exists(HttpHelper.MapPath(filename));
  109. }
  110. else
  111. {
  112. string text1 = "<?xml version=\"1.0\"?><a:propfind xmlns:a=\"DAV:\"><a:prop><a:getcontentlength/></a:prop></a:propfind>";
  113. byte[] buffer1 = new ASCIIEncoding().GetBytes(text1);
  114. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Settings.Url + filename);
  115. request.Credentials = this.myCredentialCache;
  116. request.Method = "PROPFIND";
  117. request.ContentType = "text/xml";
  118. request.Headers.Add("Depth", "0");
  119. request.Headers.Add("Translate: f");
  120. request.ContentLength = buffer1.Length;
  121. Stream stream = request.GetRequestStream();
  122. stream.Write(buffer1, 0, buffer1.Length);
  123. stream.Close();
  124. WebResponse response = null;
  125. try
  126. {
  127. response = request.GetResponse();
  128. }
  129. catch (WebException ex)
  130. {
  131. if (ex.Message.IndexOf("404") == -1)
  132. {
  133. this.ErrorMessage = ex.Message;
  134. throw ex;
  135. }
  136. return false;
  137. }
  138. finally
  139. {
  140. if (response != null)
  141. {
  142. response.Close();
  143. }
  144. }
  145. }
  146. return true;
  147. }
  148. public bool UploadFile(Stream inputStream, string filename)
  149. {
  150. this.ErrorMessage = "";
  151. if (IsWebLocal)
  152. {
  153. using (var stream = System.IO.File.Create(HttpHelper.MapPath(filename)))
  154. {
  155. inputStream.CopyTo(stream);
  156. }
  157. }
  158. else
  159. {
  160. WebClient client = new WebClient();
  161. client.Credentials = this.myCredentialCache;
  162. int num1 = (int)inputStream.Length;
  163. byte[] buffer1 = new byte[num1];
  164. inputStream.Read(buffer1, 0, num1);
  165. Stream stream = client.OpenWrite(Settings.Url + filename, "PUT");
  166. stream.Write(buffer1, 0, num1);
  167. try
  168. {
  169. stream.Close();
  170. }
  171. catch (WebException ex)
  172. {
  173. this.ErrorMessage = ex.Message;
  174. return false;
  175. }
  176. }
  177. return true;
  178. }
  179. public bool UploadFile(byte[] buffer, string filename)
  180. {
  181. this.ErrorMessage = "";
  182. if (IsWebLocal)
  183. {
  184. using (var stream = System.IO.File.Create(HttpHelper.MapPath(filename)))
  185. {
  186. stream.Write(buffer);
  187. }
  188. }
  189. else
  190. {
  191. WebClient client = new WebClient();
  192. client.Credentials = this.myCredentialCache;
  193. Stream stream = client.OpenWrite(Settings.Url + filename, "PUT");
  194. stream.Write(buffer, 0, buffer.Length);
  195. try
  196. {
  197. stream.Close();
  198. }
  199. catch (WebException ex)
  200. {
  201. this.ErrorMessage = ex.Message;
  202. return false;
  203. }
  204. }
  205. return true;
  206. }
  207. public static Stream GetFileStream(string FileName)
  208. {
  209. if (IsWebLocal)
  210. {
  211. return FileHelper.GetFileStream(FileName);
  212. }
  213. else
  214. {
  215. WebClient conn = new WebClient();
  216. var targetFile = FileName;
  217. if (!FileName.Trim().StartsWith("http") && !FileName.Trim().Contains("://"))
  218. {
  219. targetFile = new Uri(Settings.Url).GetLeftPart(UriPartial.Authority);
  220. if (!FileName.StartsWith("/"))
  221. {
  222. targetFile += "/";
  223. }
  224. targetFile += FileName;
  225. }
  226. var bytes = conn.DownloadData(targetFile);
  227. return new MemoryStream(bytes);
  228. }
  229. }
  230. /// <summary>
  231. /// 下载文件服务器中的文件
  232. /// </summary>
  233. /// <param name="virtualFilePath">目标文件的虚拟路径</param>
  234. /// <param name="remoteFile">远程文件地址</param>
  235. public static void DownloadFile(string virtualFilePath, string remoteFile)
  236. {
  237. WebClient conn = new WebClient();
  238. var targetFile = remoteFile;
  239. File.Delete(HttpHelper.MapPath(virtualFilePath));
  240. if (!remoteFile.Trim().StartsWith("http") && !remoteFile.Trim().Contains("://"))
  241. {
  242. targetFile = new Uri(HttpHelper.Current.Request.GetDisplayUrl()).GetLeftPart(UriPartial.Authority);
  243. if (!remoteFile.StartsWith("/"))
  244. {
  245. targetFile += "/";
  246. }
  247. targetFile += remoteFile;
  248. }
  249. conn.DownloadFile(targetFile, HttpHelper.MapPath(virtualFilePath));
  250. }
  251. }
  252. }