WebFileLoader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using System;
  2. using System.Collections;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. namespace Microsoft.Samples.AppUpdater
  10. {
  11. public class WebFileLoader
  12. {
  13. internal delegate void NotifyUpdateFileInfoEventHandler(object sender, NotifyUpdateFileInfoEventArgs e);
  14. private static NotifyUpdateFileInfoEventArgs UpdateFileInfoEventArgs;
  15. internal static event NotifyUpdateFileInfoEventHandler OnNotifyUpdateFileInfo;
  16. public static bool CheckForFileUpdate(string url, string filePath)
  17. {
  18. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  19. httpWebRequest.Method = "HEAD";
  20. httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
  21. HttpWebResponse httpWebResponse;
  22. try
  23. {
  24. httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  25. }
  26. catch (WebException ex)
  27. {
  28. if (ex.Response == null)
  29. {
  30. throw;
  31. }
  32. HttpWebResponse httpWebResponse2 = (HttpWebResponse)ex.Response;
  33. if (httpWebResponse2.StatusCode != HttpStatusCode.NotModified)
  34. {
  35. ex.Response.Close();
  36. throw;
  37. }
  38. ex.Response.Close();
  39. return false;
  40. }
  41. httpWebResponse.Close();
  42. return true;
  43. }
  44. public static void UpdateFile(string url, string filePath)
  45. {
  46. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  47. httpWebRequest.Headers.Add("Translate: f");
  48. httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
  49. if (File.Exists(filePath))
  50. {
  51. httpWebRequest.IfModifiedSince = LastModFromDisk(filePath);
  52. }
  53. HttpWebResponse httpWebResponse;
  54. try
  55. {
  56. httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  57. }
  58. catch (WebException ex)
  59. {
  60. if (ex.Response == null)
  61. {
  62. throw;
  63. }
  64. HttpWebResponse httpWebResponse2 = (HttpWebResponse)ex.Response;
  65. if (httpWebResponse2.StatusCode == HttpStatusCode.NotModified)
  66. {
  67. ex.Response.Close();
  68. return;
  69. }
  70. ex.Response.Close();
  71. throw;
  72. }
  73. Stream stream = null;
  74. try
  75. {
  76. stream = httpWebResponse.GetResponseStream();
  77. CopyStreamToDisk(stream, filePath);
  78. DateTime lastWriteTime = Convert.ToDateTime(httpWebResponse.GetResponseHeader("Last-Modified"));
  79. File.SetLastWriteTime(filePath, lastWriteTime);
  80. }
  81. catch (Exception)
  82. {
  83. throw;
  84. }
  85. finally
  86. {
  87. stream?.Close();
  88. httpWebResponse?.Close();
  89. }
  90. }
  91. public static int CopyDirectory(string url, string filePath, int fileTotal, bool bReport)
  92. {
  93. int num = 0;
  94. SortedList directoryContents = GetDirectoryContents(url, deep: false);
  95. foreach (object item in directoryContents)
  96. {
  97. Resource resource = (Resource)((DictionaryEntry)item).Value;
  98. if (fileTotal != 0 && !Directory.Exists(filePath))
  99. {
  100. Directory.CreateDirectory(filePath);
  101. }
  102. if (!resource.IsFolder)
  103. {
  104. num++;
  105. string text = filePath + resource.Name;
  106. if (fileTotal != 0 && (!File.Exists(text) || resource.LastModified > LastModFromDisk(text)))
  107. {
  108. if (bReport && WebFileLoader.OnNotifyUpdateFileInfo != null)
  109. {
  110. if (UpdateFileInfoEventArgs == null)
  111. {
  112. UpdateFileInfoEventArgs = new NotifyUpdateFileInfoEventArgs();
  113. }
  114. UpdateFileInfoEventArgs.FileTotal = fileTotal;
  115. UpdateFileInfoEventArgs.FileName = filePath;
  116. UpdateFileInfoEventArgs.CurrentFile = num;
  117. WebFileLoader.OnNotifyUpdateFileInfo(null, UpdateFileInfoEventArgs);
  118. }
  119. UpdateFile(resource.Url, text);
  120. }
  121. }
  122. else
  123. {
  124. string text = filePath + resource.Name + "\\";
  125. num += CopyDirectory(resource.Url, text, fileTotal, bReport: true);
  126. }
  127. }
  128. return num;
  129. }
  130. public static SortedList GetDirectoryContents(string url, bool deep)
  131. {
  132. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  133. httpWebRequest.Headers.Add("Translate: f");
  134. httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
  135. string text = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><a:propfind xmlns:a=\"DAV:\"><a:prop><a:displayname/><a:iscollection/><a:getlastmodified/></a:prop></a:propfind>";
  136. httpWebRequest.Method = "PROPFIND";
  137. if (deep)
  138. {
  139. httpWebRequest.Headers.Add("Depth: infinity");
  140. }
  141. else
  142. {
  143. httpWebRequest.Headers.Add("Depth: 1");
  144. }
  145. httpWebRequest.ContentLength = text.Length;
  146. httpWebRequest.ContentType = "text/xml";
  147. Stream requestStream = httpWebRequest.GetRequestStream();
  148. requestStream.Write(Encoding.ASCII.GetBytes(text), 0, Encoding.ASCII.GetBytes(text).Length);
  149. requestStream.Close();
  150. StreamReader streamReader;
  151. try
  152. {
  153. HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  154. streamReader = new StreamReader(httpWebResponse.GetResponseStream());
  155. }
  156. catch (WebException ex)
  157. {
  158. throw ex;
  159. }
  160. StringBuilder stringBuilder = new StringBuilder();
  161. char[] array = new char[1024];
  162. int num = 0;
  163. for (num = streamReader.Read(array, 0, 1024); num > 0; num = streamReader.Read(array, 0, 1024))
  164. {
  165. stringBuilder.Append(array, 0, num);
  166. }
  167. streamReader.Close();
  168. XmlDocument xmlDocument = new XmlDocument();
  169. xmlDocument.LoadXml(stringBuilder.ToString());
  170. XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
  171. xmlNamespaceManager.AddNamespace("a", "DAV:");
  172. XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//a:prop/a:displayname", xmlNamespaceManager);
  173. XmlNodeList xmlNodeList2 = xmlDocument.SelectNodes("//a:prop/a:iscollection", xmlNamespaceManager);
  174. XmlNodeList xmlNodeList3 = xmlDocument.SelectNodes("//a:prop/a:getlastmodified", xmlNamespaceManager);
  175. XmlNodeList xmlNodeList4 = xmlDocument.SelectNodes("//a:href", xmlNamespaceManager);
  176. SortedList sortedList = new SortedList();
  177. for (int i = 1; i < xmlNodeList.Count; i++)
  178. {
  179. if (xmlNodeList4[i].InnerText.ToLower(new CultureInfo("en-US")).TrimEnd('/') != url.ToLower(new CultureInfo("en-US")).TrimEnd('/'))
  180. {
  181. Resource resource = new Resource();
  182. resource.Name = xmlNodeList[i].InnerText;
  183. resource.IsFolder = Convert.ToBoolean(Convert.ToInt32(xmlNodeList2[i].InnerText));
  184. resource.Url = xmlNodeList4[i].InnerText;
  185. resource.LastModified = Convert.ToDateTime(xmlNodeList3[i].InnerText);
  186. sortedList.Add(resource.Url, resource);
  187. }
  188. }
  189. return sortedList;
  190. }
  191. public static void UpdateFileList(SortedList fileList, string sourceUrl, string destPath)
  192. {
  193. if (!Directory.Exists(destPath))
  194. {
  195. Directory.CreateDirectory(destPath);
  196. }
  197. foreach (object file in fileList)
  198. {
  199. Resource resource = (Resource)((DictionaryEntry)file).Value;
  200. string url = sourceUrl + resource.Name;
  201. string filePath = destPath + resource.Name;
  202. UpdateFile(url, filePath);
  203. }
  204. }
  205. public static DateTime GetLastModTime(string url)
  206. {
  207. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  208. httpWebRequest.Method = "HEAD";
  209. HttpWebResponse httpWebResponse;
  210. try
  211. {
  212. httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  213. }
  214. catch (WebException ex)
  215. {
  216. if (ex.Response != null)
  217. {
  218. ex.Response.Close();
  219. }
  220. throw;
  221. }
  222. return Convert.ToDateTime(httpWebResponse.GetResponseHeader("Last-Modified"));
  223. }
  224. public static Stream LoadFile(string url)
  225. {
  226. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  227. HttpWebResponse httpWebResponse;
  228. try
  229. {
  230. httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  231. }
  232. catch (WebException)
  233. {
  234. throw;
  235. }
  236. return httpWebResponse.GetResponseStream();
  237. }
  238. public static bool CheckForFileUpdate(string url, DateTime lastModeTime)
  239. {
  240. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  241. httpWebRequest.Method = "HEAD";
  242. httpWebRequest.IfModifiedSince = lastModeTime;
  243. HttpWebResponse httpWebResponse;
  244. try
  245. {
  246. httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  247. }
  248. catch (WebException ex)
  249. {
  250. if (ex.Response == null)
  251. {
  252. throw;
  253. }
  254. HttpWebResponse httpWebResponse2 = (HttpWebResponse)ex.Response;
  255. if (httpWebResponse2.StatusCode != HttpStatusCode.NotModified)
  256. {
  257. ex.Response.Close();
  258. throw;
  259. }
  260. ex.Response.Close();
  261. return false;
  262. }
  263. httpWebResponse.Close();
  264. return true;
  265. }
  266. public static int GetFileCount(string filePath)
  267. {
  268. string[] directories = Directory.GetDirectories(filePath);
  269. string[] files = Directory.GetFiles(filePath);
  270. int num = 0;
  271. num = files.Length + directories.Length;
  272. string[] array = directories;
  273. foreach (string text in array)
  274. {
  275. string str = text.Remove(0, text.LastIndexOf("\\") + 1);
  276. num += GetFileCount(filePath + str + "\\");
  277. }
  278. return num;
  279. }
  280. private static void CopyStreamToDisk(Stream responseStream, string filePath)
  281. {
  282. byte[] buffer = new byte[4096];
  283. Random random = new Random();
  284. string str = Environment.GetEnvironmentVariable("temp") + "\\";
  285. str += filePath.Remove(0, filePath.LastIndexOf("\\") + 1);
  286. str = str + random.Next(10000).ToString() + ".tmp";
  287. FileStream fileStream = File.Open(str, FileMode.Create, FileAccess.ReadWrite);
  288. for (int num = responseStream.Read(buffer, 0, 4096); num > 0; num = responseStream.Read(buffer, 0, 4096))
  289. {
  290. fileStream.Write(buffer, 0, num);
  291. }
  292. fileStream.Close();
  293. if (File.Exists(filePath))
  294. {
  295. File.Delete(filePath);
  296. }
  297. File.Move(str, filePath);
  298. }
  299. public static DateTime LastModFromDisk(string filePath)
  300. {
  301. FileInfo fileInfo = new FileInfo(filePath);
  302. return fileInfo.LastWriteTime;
  303. }
  304. public static void CopyAndRename(string source, string dest)
  305. {
  306. string[] directories = Directory.GetDirectories(source);
  307. string[] files = Directory.GetFiles(source);
  308. if (!Directory.Exists(dest))
  309. {
  310. Directory.CreateDirectory(dest);
  311. }
  312. string[] array = files;
  313. foreach (string text in array)
  314. {
  315. string text2 = text.Remove(0, text.LastIndexOf("\\") + 1);
  316. MessageBox.Show(text2);
  317. if (File.Exists(dest + text2))
  318. {
  319. File.Delete(dest + text2);
  320. }
  321. File.Move(text, dest + text2);
  322. }
  323. string[] array2 = directories;
  324. foreach (string text3 in array2)
  325. {
  326. string text2 = text3.Remove(0, text3.LastIndexOf("\\") + 1);
  327. MessageBox.Show(text2);
  328. if (!Directory.Exists(dest + text2 + "\\"))
  329. {
  330. Directory.CreateDirectory(dest + text2 + "\\");
  331. }
  332. CopyAndRename(source + text2 + "\\", dest + text2 + "\\");
  333. }
  334. Directory.Delete(source, recursive: true);
  335. }
  336. public static string CreateHttpsUrl(string url)
  337. {
  338. url = url.ToLower(new CultureInfo("en-US"));
  339. if (url.StartsWith("https"))
  340. {
  341. return url;
  342. }
  343. return url.Insert(4, "s");
  344. }
  345. }
  346. }