|
- using System;
- using System.Collections.Generic;
- using System.IO;
- using log4net;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- using System.Web.UI.WebControls;
- using System.IO.Pipes;
- using static THUploadOss.Common;
-
- namespace THUploadOss
- {
- public class THDownloadFile
- {
- //是指定文件夹下的所有文件,同时还要重命名文件夹,根据订阅的消息重命名文件夹????
- private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
-
- public string m_smbPicPath; // SMB服务器上的图片文件路径
- public string m_smbVideoPath; // SMB服务器上的视频文件路径
-
- public string m_localPicPath; // 本地保存路径
- public string m_localVideoPath; // 本地保存路径
- public int m_deleteTime; // 本地文件保存时间
- public int m_number = 0;
-
- FileStream readFileStream;
- FileStream fileStream;
- public bool InitDownloadFile()
- {
- if (!getMqttConfig())
- return false;
- return true;
- }
-
- public bool DownloadAllFile()
- {
- if (StsrtDownload == 1)
- {
- try
- {
- if (m_number++ > 10)
- {
- Status = (int)UPLOAD_STATUS.Fail;
- StsrtDownload = -1;
- DownloadFinsh = -1;
- m_number = 0;
- return false;
- }
-
- string[] picFiles = Directory.GetFiles(m_smbPicPath);
- long pictotalSize = 0;
- long picindex = 0;
-
- foreach (string file in picFiles)
- {
- FileInfo fileinfo = new FileInfo(file);
- pictotalSize += fileinfo.Length;
- }
- Logger.WriteInfo($"THDownloadFile - picFiles Total size: {pictotalSize} bytes");
-
- // 创建任务名称文件夹
- string newFolderPath = Path.Combine(m_localPicPath, RequestId);
- Directory.CreateDirectory(newFolderPath);
-
- foreach (string file in picFiles)
- {
- if (StsrtDownload == -1)
- {
- RemoveAllFile(m_smbPicPath);
- RemoveAllFile(m_smbVideoPath);
- m_number = 0;
- Logger.WriteInfo("THDownloadFile - 等待删除缓存!");
- System.Threading.Thread.Sleep(60000);//删除延时
- Status = (int)UPLOAD_STATUS.Ccancellation;
- Logger.WriteInfo("THDownloadFile - 删除缓存完成!");
- return false;
- }
-
- string destinationFilePath = Path.Combine(newFolderPath, Path.GetFileName(file));
-
- readFileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
- fileStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write);
-
- byte[] buffer = new byte[10240];
- int count;
-
- Progress<double> prog = new Progress<double>((theV =>
- {
- DownloadPercent = (double)theV / pictotalSize * 100;
- Logger.WriteInfo($@"THDownloadFile - Now the picFiles Progress:{(DownloadPercent>100?100:DownloadPercent)}%");
- }));
-
- while ((count = readFileStream.Read(buffer, 0, 10240)) != 0 && StsrtDownload != -1)
- {
- fileStream.Write(buffer, 0, count);
- ((IProgress<double>)prog).Report((double)picindex * 10240);
- picindex++;
- }
- fileStream.Close();
- readFileStream.Close();
- }
-
- string[] Videofiles = Directory.GetFiles(m_smbVideoPath);
- long videototalSize = 0;
- long videoindex = 0;
- foreach (string file in Videofiles)
- {
- FileInfo fileinfo = new FileInfo(file);
- videototalSize += fileinfo.Length;
- }
- Logger.WriteInfo($"THDownloadFile - Videofiles Total size: {videototalSize} bytes");
-
- // 创建任务名称文件夹
- newFolderPath = Path.Combine(m_localVideoPath, RequestId);
- Directory.CreateDirectory(newFolderPath);
-
- foreach (string file in Videofiles)
- {
- if (StsrtDownload == -1)
- {
- RemoveAllFile(m_smbPicPath);
- RemoveAllFile(m_smbVideoPath);
- m_number = 0;
- Logger.WriteInfo("THDownloadFile - 等待删除缓存!");
- System.Threading.Thread.Sleep(60000);//删除延时
- Status = (int)UPLOAD_STATUS.Ccancellation;
- Logger.WriteInfo("THDownloadFile - 删除缓存完成!");
- return false;
- }
-
- string destinationFilePath = Path.Combine(newFolderPath, Path.GetFileName(file));
-
- readFileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
- fileStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write);
-
- byte[] buffer = new byte[10240];
- int count;
-
- Progress<double> prog = new Progress<double>((theV =>
- {
- DownloadPercent = (double)theV / videototalSize * 100;
- Logger.WriteInfo($@"THDownloadFile - Now the Videofiles Progress:{(DownloadPercent > 100 ? 100 : DownloadPercent)}%");
- }));
- while ((count = readFileStream.Read(buffer, 0, 10240)) != 0 && StsrtDownload != -1)
- {
- //System.Threading.Thread.Sleep(100);
- fileStream.Write(buffer, 0, count);
- ((IProgress<double>)prog).Report((double)videoindex * 10240);
- videoindex++;
- }
- fileStream.Close();
- readFileStream.Close();
- }
-
- Logger.WriteInfo("THDownloadFile - 所有文件下载成功!");
- if (RemoveAllFile(m_smbVideoPath) && RemoveAllFile(m_smbPicPath))
- {
- Logger.WriteInfo("THDownloadFile - 等待删除缓存!");
- System.Threading.Thread.Sleep(60000);//删除延时
- if (StsrtDownload == -1)
- {
- m_number = 0;
- Status = (int)UPLOAD_STATUS.Ccancellation;
- return false;
- }
- DownloadPercent = 100.00;
- Status = (int)UPLOAD_STATUS.Uploading;
- StsrtDownload = 0;
- DownloadFinsh = 1;
- m_number = 0;
- Logger.WriteInfo("THDownloadFile - 删除缓存完成!");
- return true;
- }else
- return false;
- }
- catch (Exception ex)
- {
- if(fileStream !=null)
- fileStream.Close();
- if(readFileStream!=null)
- readFileStream.Close();
- Logger.WriteError("THDownloadFile - DownloadAllFile-发生异常:" + ex.Message);
- return false;
- }
- }
- else
- {
- System.Threading.Thread.Sleep(1000);
- CleanFile(m_localVideoPath);
- CleanFile(m_localPicPath);
- return false;
- }
- }
-
- //实现删除指定文件夹下的所有文件
- public bool RemoveAllFile(string path)
- {
- try {
- string[] files = Directory.GetFiles(path);
-
- // 逐个删除文件
- foreach (string file in files)
- {
- File.Delete(file);
- Logger.WriteInfo("THDownloadFile - RemoveFile(" + file + ")-操作完成");
- }
- Logger.WriteInfo("THDownloadFile - RemoveAllFile(" + path + ")-操作完成");
- return true;
- } catch (Exception ex)
- {
- Logger.WriteError("THDownloadFile - RemoveAllFile(" + path + ")-发生异常:" + ex.Message);
- return false;
- }
- }
-
-
-
- public void CleanFile(string path)
- {
- DateTime nowTime = DateTime.Now;
- DirectoryInfo root = new DirectoryInfo(path);
- if (root.Exists) {
- DirectoryInfo[] dics = root.GetDirectories();//获取文件夹
-
- FileAttributes attr = File.GetAttributes(path);
- if (attr == FileAttributes.Directory)//判断是不是文件夹
- {
- foreach (DirectoryInfo file in dics)//遍历文件夹
- {
- TimeSpan t = nowTime - file.CreationTime; //当前时间 减去 文件创建时间
- int day = t.Days;
- if (day > m_deleteTime) //保存的时间 ; 单位:天
- {
- Directory.Delete(file.FullName, true); //删除超过时间的文件夹
- }
- }
- }
- }
- }
- public bool getMqttConfig()
- {
- if (System.IO.File.Exists(ConfigPath))
- {
- try
- {
- StringBuilder smbPicPath = new StringBuilder(200);
- GetPrivateProfileString("SMBPATH", "smbPicPath", "配置文件存在,读取未成功!", smbPicPath, 255, ConfigPath);
- m_smbPicPath = smbPicPath.ToString();
-
- StringBuilder smbVideoPath = new StringBuilder(200);
- GetPrivateProfileString("SMBPATH", "smbVideoPath", "配置文件存在,读取未成功!", smbVideoPath, 255, ConfigPath);
- m_smbVideoPath = smbVideoPath.ToString();
-
- StringBuilder localPicPath = new StringBuilder(10);
- GetPrivateProfileString("SMBPATH", "localPicPath", "配置文件存在,读取未成功!", localPicPath, 255, ConfigPath);
- m_localPicPath = localPicPath.ToString();
-
- StringBuilder localVideoPath = new StringBuilder(10);
- GetPrivateProfileString("SMBPATH", "localVideoPath", "配置文件存在,读取未成功!", localVideoPath, 255, ConfigPath);
- m_localVideoPath = localVideoPath.ToString();
-
- StringBuilder deleteTime = new StringBuilder(10);
- GetPrivateProfileString("SMBPATH", "deleteTime", "配置文件存在,读取未成功!", deleteTime, 255, ConfigPath);
- m_deleteTime = int.Parse(deleteTime.ToString());
-
- return true;
- }
- catch (Exception ex)
- {
- Logger.WriteError("THDownloadFile - getMqttConfig" + ex.Message.ToString());
- return false;
- }
- }
- return false;
- }
-
- }
- }
-
-
- ////在指定路径下新建文件夹,并将这个路径下所有的png后缀文件移动到新建的文件夹中
- //public bool MoveFile(string path, string folderName, string fileType)
- //{
- // try {
- // // 创建新文件夹
- // string newFolderPath = Path.Combine(path, folderName);
- // Directory.CreateDirectory(newFolderPath);
-
- // // 获取指定路径下的所有png后缀文件
- // string[] files = Directory.GetFiles(path, fileType);
-
- // // 遍历所有png文件,将其移动到新建的文件夹中
- // foreach (string file in files)
- // {
- // string fileName = Path.GetFileName(file);
- // string newFilePath = Path.Combine(newFolderPath, fileName);
- // File.Move(file, newFilePath);
- // }
-
- // Logger.WriteInfo("THDownloadFile - MoveFile(" + path +"," + folderName+","+ fileType + ")-操作完成");
- // return true;
- // } catch (Exception ex)
- // {
- // Logger.WriteError("THDownloadFile - MoveFile(" + path +"," + folderName+","+ fileType + ")-发生异常:" + ex.Message);
- // return false;
- // }
- //}
|