THupload-Oss
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.

309 lines
13KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using log4net;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Reflection;
  9. using System.Web.UI.WebControls;
  10. using System.IO.Pipes;
  11. using static THUploadOss.Common;
  12. namespace THUploadOss
  13. {
  14. public class THDownloadFile
  15. {
  16. //是指定文件夹下的所有文件,同时还要重命名文件夹,根据订阅的消息重命名文件夹????
  17. private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  18. public string m_smbPicPath; // SMB服务器上的图片文件路径
  19. public string m_smbVideoPath; // SMB服务器上的视频文件路径
  20. public string m_localPicPath; // 本地保存路径
  21. public string m_localVideoPath; // 本地保存路径
  22. public int m_deleteTime; // 本地文件保存时间
  23. public int m_number = 0;
  24. FileStream readFileStream;
  25. FileStream fileStream;
  26. public bool InitDownloadFile()
  27. {
  28. if (!getMqttConfig())
  29. return false;
  30. return true;
  31. }
  32. public bool DownloadAllFile()
  33. {
  34. if (StsrtDownload == 1)
  35. {
  36. try
  37. {
  38. if (m_number++ > 10)
  39. {
  40. Status = (int)UPLOAD_STATUS.Fail;
  41. StsrtDownload = -1;
  42. DownloadFinsh = -1;
  43. m_number = 0;
  44. return false;
  45. }
  46. string[] picFiles = Directory.GetFiles(m_smbPicPath);
  47. long pictotalSize = 0;
  48. long picindex = 0;
  49. foreach (string file in picFiles)
  50. {
  51. FileInfo fileinfo = new FileInfo(file);
  52. pictotalSize += fileinfo.Length;
  53. }
  54. Logger.WriteInfo($"THDownloadFile - picFiles Total size: {pictotalSize} bytes");
  55. // 创建任务名称文件夹
  56. string newFolderPath = Path.Combine(m_localPicPath, RequestId);
  57. Directory.CreateDirectory(newFolderPath);
  58. foreach (string file in picFiles)
  59. {
  60. if (StsrtDownload == -1)
  61. {
  62. RemoveAllFile(m_smbPicPath);
  63. RemoveAllFile(m_smbVideoPath);
  64. m_number = 0;
  65. Logger.WriteInfo("THDownloadFile - 等待删除缓存!");
  66. System.Threading.Thread.Sleep(60000);//删除延时
  67. Status = (int)UPLOAD_STATUS.Ccancellation;
  68. Logger.WriteInfo("THDownloadFile - 删除缓存完成!");
  69. return false;
  70. }
  71. string destinationFilePath = Path.Combine(newFolderPath, Path.GetFileName(file));
  72. readFileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
  73. fileStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write);
  74. byte[] buffer = new byte[10240];
  75. int count;
  76. Progress<double> prog = new Progress<double>((theV =>
  77. {
  78. DownloadPercent = (double)theV / pictotalSize * 100;
  79. Logger.WriteInfo($@"THDownloadFile - Now the picFiles Progress:{(DownloadPercent>100?100:DownloadPercent)}%");
  80. }));
  81. while ((count = readFileStream.Read(buffer, 0, 10240)) != 0 && StsrtDownload != -1)
  82. {
  83. fileStream.Write(buffer, 0, count);
  84. ((IProgress<double>)prog).Report((double)picindex * 10240);
  85. picindex++;
  86. }
  87. fileStream.Close();
  88. readFileStream.Close();
  89. }
  90. string[] Videofiles = Directory.GetFiles(m_smbVideoPath);
  91. long videototalSize = 0;
  92. long videoindex = 0;
  93. foreach (string file in Videofiles)
  94. {
  95. FileInfo fileinfo = new FileInfo(file);
  96. videototalSize += fileinfo.Length;
  97. }
  98. Logger.WriteInfo($"THDownloadFile - Videofiles Total size: {videototalSize} bytes");
  99. // 创建任务名称文件夹
  100. newFolderPath = Path.Combine(m_localVideoPath, RequestId);
  101. Directory.CreateDirectory(newFolderPath);
  102. foreach (string file in Videofiles)
  103. {
  104. if (StsrtDownload == -1)
  105. {
  106. RemoveAllFile(m_smbPicPath);
  107. RemoveAllFile(m_smbVideoPath);
  108. m_number = 0;
  109. Logger.WriteInfo("THDownloadFile - 等待删除缓存!");
  110. System.Threading.Thread.Sleep(60000);//删除延时
  111. Status = (int)UPLOAD_STATUS.Ccancellation;
  112. Logger.WriteInfo("THDownloadFile - 删除缓存完成!");
  113. return false;
  114. }
  115. string destinationFilePath = Path.Combine(newFolderPath, Path.GetFileName(file));
  116. readFileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
  117. fileStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write);
  118. byte[] buffer = new byte[10240];
  119. int count;
  120. Progress<double> prog = new Progress<double>((theV =>
  121. {
  122. DownloadPercent = (double)theV / videototalSize * 100;
  123. Logger.WriteInfo($@"THDownloadFile - Now the Videofiles Progress:{(DownloadPercent > 100 ? 100 : DownloadPercent)}%");
  124. }));
  125. while ((count = readFileStream.Read(buffer, 0, 10240)) != 0 && StsrtDownload != -1)
  126. {
  127. //System.Threading.Thread.Sleep(100);
  128. fileStream.Write(buffer, 0, count);
  129. ((IProgress<double>)prog).Report((double)videoindex * 10240);
  130. videoindex++;
  131. }
  132. fileStream.Close();
  133. readFileStream.Close();
  134. }
  135. Logger.WriteInfo("THDownloadFile - 所有文件下载成功!");
  136. if (RemoveAllFile(m_smbVideoPath) && RemoveAllFile(m_smbPicPath))
  137. {
  138. Logger.WriteInfo("THDownloadFile - 等待删除缓存!");
  139. System.Threading.Thread.Sleep(60000);//删除延时
  140. if (StsrtDownload == -1)
  141. {
  142. m_number = 0;
  143. Status = (int)UPLOAD_STATUS.Ccancellation;
  144. return false;
  145. }
  146. DownloadPercent = 100.00;
  147. Status = (int)UPLOAD_STATUS.Uploading;
  148. StsrtDownload = 0;
  149. DownloadFinsh = 1;
  150. m_number = 0;
  151. Logger.WriteInfo("THDownloadFile - 删除缓存完成!");
  152. return true;
  153. }else
  154. return false;
  155. }
  156. catch (Exception ex)
  157. {
  158. if(fileStream !=null)
  159. fileStream.Close();
  160. if(readFileStream!=null)
  161. readFileStream.Close();
  162. Logger.WriteError("THDownloadFile - DownloadAllFile-发生异常:" + ex.Message);
  163. return false;
  164. }
  165. }
  166. else
  167. {
  168. System.Threading.Thread.Sleep(1000);
  169. CleanFile(m_localVideoPath);
  170. CleanFile(m_localPicPath);
  171. return false;
  172. }
  173. }
  174. //实现删除指定文件夹下的所有文件
  175. public bool RemoveAllFile(string path)
  176. {
  177. try {
  178. string[] files = Directory.GetFiles(path);
  179. // 逐个删除文件
  180. foreach (string file in files)
  181. {
  182. File.Delete(file);
  183. Logger.WriteInfo("THDownloadFile - RemoveFile(" + file + ")-操作完成");
  184. }
  185. Logger.WriteInfo("THDownloadFile - RemoveAllFile(" + path + ")-操作完成");
  186. return true;
  187. } catch (Exception ex)
  188. {
  189. Logger.WriteError("THDownloadFile - RemoveAllFile(" + path + ")-发生异常:" + ex.Message);
  190. return false;
  191. }
  192. }
  193. public void CleanFile(string path)
  194. {
  195. DateTime nowTime = DateTime.Now;
  196. DirectoryInfo root = new DirectoryInfo(path);
  197. if (root.Exists) {
  198. DirectoryInfo[] dics = root.GetDirectories();//获取文件夹
  199. FileAttributes attr = File.GetAttributes(path);
  200. if (attr == FileAttributes.Directory)//判断是不是文件夹
  201. {
  202. foreach (DirectoryInfo file in dics)//遍历文件夹
  203. {
  204. TimeSpan t = nowTime - file.CreationTime; //当前时间 减去 文件创建时间
  205. int day = t.Days;
  206. if (day > m_deleteTime) //保存的时间 ; 单位:天
  207. {
  208. Directory.Delete(file.FullName, true); //删除超过时间的文件夹
  209. }
  210. }
  211. }
  212. }
  213. }
  214. public bool getMqttConfig()
  215. {
  216. if (System.IO.File.Exists(ConfigPath))
  217. {
  218. try
  219. {
  220. StringBuilder smbPicPath = new StringBuilder(200);
  221. GetPrivateProfileString("SMBPATH", "smbPicPath", "配置文件存在,读取未成功!", smbPicPath, 255, ConfigPath);
  222. m_smbPicPath = smbPicPath.ToString();
  223. StringBuilder smbVideoPath = new StringBuilder(200);
  224. GetPrivateProfileString("SMBPATH", "smbVideoPath", "配置文件存在,读取未成功!", smbVideoPath, 255, ConfigPath);
  225. m_smbVideoPath = smbVideoPath.ToString();
  226. StringBuilder localPicPath = new StringBuilder(10);
  227. GetPrivateProfileString("SMBPATH", "localPicPath", "配置文件存在,读取未成功!", localPicPath, 255, ConfigPath);
  228. m_localPicPath = localPicPath.ToString();
  229. StringBuilder localVideoPath = new StringBuilder(10);
  230. GetPrivateProfileString("SMBPATH", "localVideoPath", "配置文件存在,读取未成功!", localVideoPath, 255, ConfigPath);
  231. m_localVideoPath = localVideoPath.ToString();
  232. StringBuilder deleteTime = new StringBuilder(10);
  233. GetPrivateProfileString("SMBPATH", "deleteTime", "配置文件存在,读取未成功!", deleteTime, 255, ConfigPath);
  234. m_deleteTime = int.Parse(deleteTime.ToString());
  235. return true;
  236. }
  237. catch (Exception ex)
  238. {
  239. Logger.WriteError("THDownloadFile - getMqttConfig" + ex.Message.ToString());
  240. return false;
  241. }
  242. }
  243. return false;
  244. }
  245. }
  246. }
  247. ////在指定路径下新建文件夹,并将这个路径下所有的png后缀文件移动到新建的文件夹中
  248. //public bool MoveFile(string path, string folderName, string fileType)
  249. //{
  250. // try {
  251. // // 创建新文件夹
  252. // string newFolderPath = Path.Combine(path, folderName);
  253. // Directory.CreateDirectory(newFolderPath);
  254. // // 获取指定路径下的所有png后缀文件
  255. // string[] files = Directory.GetFiles(path, fileType);
  256. // // 遍历所有png文件,将其移动到新建的文件夹中
  257. // foreach (string file in files)
  258. // {
  259. // string fileName = Path.GetFileName(file);
  260. // string newFilePath = Path.Combine(newFolderPath, fileName);
  261. // File.Move(file, newFilePath);
  262. // }
  263. // Logger.WriteInfo("THDownloadFile - MoveFile(" + path +"," + folderName+","+ fileType + ")-操作完成");
  264. // return true;
  265. // } catch (Exception ex)
  266. // {
  267. // Logger.WriteError("THDownloadFile - MoveFile(" + path +"," + folderName+","+ fileType + ")-发生异常:" + ex.Message);
  268. // return false;
  269. // }
  270. //}