示例#1
0
        internal bool CreateDownload(TransferParameter parameter)
        {
            bool ret = false;

            try
            {
                Socket s = Login();
                if (s != null)
                {
                    int contentLength = GetFileSize(s, GetUnescapeString(_transferUri.AbsolutePath));
                    if (contentLength > 0)
                    {
                        DownloadFileState = new FileState(parameter.TransferUrl, parameter.LocalFile, contentLength, parameter.ChunkCount);
                        if (DownloadFileState != null)
                        {
                            ret = true;
                            AssignFileStateDelegate();
                            DownloadFileState.FireFileDownloadBeginingEvent();
                        }
                    }
                    CleanUp(s, true);
                }
            }
            catch (Exception e)
            {
                Log.Debug("- Got an Excetpion {0}", e.ToString());
                Log.Warn(e);
                ret = false;
            }

            return(ret);
        }
示例#2
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="parameter"></param>
        public HttpWebUploader(TransferParameter parameter)
        {
            Parameter  = parameter;
            _chunkSize = (parameter.ChunkSize > 0 ? parameter.ChunkSize : 2) * 1024 * 1024;

            Log.Debug("Create HttpWebUploader: url=" + parameter.TransferUrl + ",remoteFile=" + parameter.RemoteFile + ",localFile=" + parameter.LocalFile + ",chunkCount=" + _chunkSize.ToString());
        }
示例#3
0
 public BlockDownloader(TransferParameter parameter)
 {
     Parameter   = parameter;
     _chunkCount = (parameter.ChunkCount > 0 ? parameter.ChunkCount : (short)5);
     //CreateDownload(parameter);
     AssignFileStateDelegate();
     DownloadState.Parameter = parameter;
 }
示例#4
0
        private void DownloadFile(TransferParameter parameter)
        {
            if (!CreateDownload(parameter))
            {
                Log.Debug("- 无法连接服务器,请确定远程服务器是否可用");
                throw new ApplicationException("- 无法连接服务器,请确定远程服务器是否可用");
            }

            Log.Debug("- Start Thread Download");
            DownloadFileState.Start(new ThreadProcDelegate(DownloadChunkBlock));
        }
示例#5
0
 public IActionResult StartTransfer(TransferParameter doNotStop)
 {
     try
     {
         terminal.StartTransfer(doNotStop);
         return(this.Ok(new { success = true, message = "启动成功" }));
     }
     catch (TerminalException ex)
     {
         return(this.BadRequest(new { success = true, message = ex.Message }));
     }
 }
示例#6
0
        private static void UpdateHistory()
        {
            var remotefile      = DownloadState.Parameter.RemoteFile;
            var historyApply    = new Apply();
            var historyFileItem = new FileItem();
            var parameter       = new TransferParameter();

            if (DownloadHistory.Exists(remotefile, ref historyFileItem, ref historyApply, ref parameter, false))
            {
                historyFileItem.EndTime     = DateTime.Now;
                historyFileItem.IsCompleted = true;
                DownloadHistory.Update(remotefile, historyFileItem);
            }
        }
示例#7
0
 private void DownloadFile(TransferParameter parameter)
 {
     try
     {
         CreateDownload(parameter);
         Log.Info("Start download file job : {0}, {1}, {2}", parameter.TransferUrl, parameter.LocalFile, parameter.ChunkCount);
         DownloadFileState.Start(new ThreadProcDelegate(DownloadChunkBlock));
     }
     catch (WebException e)
     {
         Log.Warn("- Got an Excetpion {0}", e.ToString());
         throw;
     }
 }
示例#8
0
        public static bool AcceptRanges(TransferParameter parameter, out long iFileSize, out DateTime oDateTime)
        {
            Log.Debug("AcceptRanges正在获取HttpWebRequest对象.");
            HttpWebRequest request = GetWebRequest(parameter);

            request.Accept            = "*/*";
            request.AllowAutoRedirect = true;
            request.Method            = "HEAD";

            var sb = new StringBuilder();

            sb.Append(string.Format(" GET {0} HTTP/{1}", parameter.TransferUrl, request.ProtocolVersion) + "\r\n");
            sb.Append(string.Format(" Host: {0}", request.Address.Host) + "\r\n");
            sb.Append(string.Format(" Accept: {0}", request.Accept) + "\r\n");
            sb.Append(string.Format(" User-Agent: {0}", request.UserAgent) + "\r\n");
            sb.Append(string.Format(" Referer: {0}", request.Referer) + "\r\n");
            sb.Append("AcceptRanges正在获取网络回应.");
            Log.Debug(sb.ToString());
            var response = (HttpWebResponse)request.GetResponse();

            if (response.ContentLength == -1 || response.ContentLength > 0x7fffffff)
            {
                iFileSize = 1024 * 1024 * 5;
            }
            else
            {
                iFileSize = response.ContentLength;
            }

            bool allowRangs = false;

            oDateTime = DateTime.MinValue;
            sb        = new StringBuilder();
            for (int i = 0; i < response.Headers.Count; ++i)
            {
                if (String.Compare(response.Headers.Keys[i], "Accept-Ranges", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    allowRangs = true;
                }
                if (String.Compare(response.Headers.Keys[i], "Last-Modified", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    oDateTime = response.LastModified;
                }
                sb.Append(response.Headers.Keys[i] + ":" + response.Headers[response.Headers.Keys[i]] + "\r\n");
            }
            response.Close();
            request.Abort();
            Log.Debug(sb.ToString());
            return(allowRangs);
        }
示例#9
0
        private void UploadFile(TransferParameter parameter)
        {
            FtpClient ftp = null;

            try
            {
                ftp = new FtpClient();
                var uri = new Uri(parameter.TransferUrl);
                ftp.Server          = uri.Host;
                ftp.Port            = uri.Port;
                ftp.Begining       += new BeginingEventHandler(FtpBegining);
                ftp.Completed      += new CompletedEventHandler(FtpCompleted);
                ftp.ExceptionError += new ExceptionEventHandle(FtpExceptionError);
                ftp.Progress       += new ProgressEventHandle(FtpProgress);
                var env = parameter.Environment;
                if (env != null)
                {
                    if (env.Username != "")
                    {
                        ftp.Username = env.Username;
                    }
                    if (env.Password != "")
                    {
                        ftp.Password = env.Password;
                    }
                }
                ftp.Login();
                ftp.Upload(parameter.LocalFile, parameter.RemoteFile);
            }
            catch (Exception e)
            {
                OnExcetipion(e.Message);
            }
            finally
            {
                if (ftp != null)
                {
                    ftp.Close();
                }
            }
        }
示例#10
0
        private static int GetMaxChunks(TransferParameter parameter, long iFileSize)
        {
            const int k1 = 1024;

            int maxChunks = parameter.ChunkCount;

            if (iFileSize <= 100 * k1)
            {
                //小于100K
                maxChunks = 1;
            }
            else if (iFileSize <= 500 * k1)
            {
                //小于500K
                if (maxChunks >= 5)
                {
                    maxChunks = 5;
                }
            }
            return(maxChunks);
        }
示例#11
0
        public static ArrayList GetBlocks(TransferParameter parameter, ref Apply oApply)
        {
            var  blockList    = new ArrayList();
            long fileSize     = 0;
            var  lastModified = DateTime.MinValue;
            bool allowRanges  = AcceptRanges(parameter, out fileSize, out lastModified);

            oApply.FileSize       = fileSize;
            oApply.LastModified   = lastModified;
            oApply.AllowRanges    = allowRanges;
            oApply.BlockSize      = (int)fileSize;
            oApply.ActuallyChunks = 1;
            int maxChunks = GetMaxChunks(parameter, fileSize);

            if (allowRanges && maxChunks > 1)
            {
                var  blocksize = (int)(fileSize / maxChunks + 1);
                long sizeCount = fileSize;
                int  index     = 0;
                while (sizeCount > 0)
                {
                    int actuallySize = 0;
                    actuallySize = (int)(sizeCount > blocksize ? blocksize : sizeCount);

                    var blcok = new Blocks(GetWebRequest(parameter), parameter.RemoteFile, index, index * blocksize, actuallySize);
                    blockList.Add(blcok);

                    sizeCount -= actuallySize;
                    index++;
                }
                oApply.BlockSize      = blocksize;
                oApply.ActuallyChunks = index;
            }
            else
            {
                var blcok = new Blocks(GetWebRequest(parameter), parameter.RemoteFile, 0, 0, (int)fileSize);
                blockList.Add(blcok);
            }
            return(blockList);
        }
示例#12
0
        public static HttpWebRequest GetWebRequest(TransferParameter parameter)
        {
            var uri = new Uri(parameter.TransferUrl);

            return(GetWebRequest(uri, parameter.Environment));
        }
示例#13
0
        private void CreateDownload(TransferParameter parameter)
        {
            try
            {
                bool allowRanges = false;
                int  contentLength;


                _request = DownloadRequst.GetWebRequest(parameter);
                _request.PreAuthenticate = true;

                _request.Accept            = "*/*";
                _request.AllowAutoRedirect = true;
                _request.Method            = "HEAD";

                Log.Debug("^ GET {0} HTTP/{1}", parameter.TransferUrl, _request.ProtocolVersion);
                Log.Debug("^ Host: {0}", _request.Address.Host);
                Log.Debug("^ Accept: {0}", _request.Accept);
                Log.Debug("^ User-Agent: {0}", _request.UserAgent);
                Log.Debug("^ Referer: {0}", _request.Referer);

                var response = (HttpWebResponse)_request.GetResponse();

                if (response.ContentLength == -1 || response.ContentLength > 0x7fffffff)
                {
                    contentLength = 1024 * 1024 * 5;
                }
                else
                {
                    contentLength = (int)response.ContentLength;
                }

                Log.Debug("v HTTP/{0} {1} OK", response.ProtocolVersion, response.StatusCode);
                for (int i = 0; i < response.Headers.Count; ++i)
                {
                    if (String.Compare(response.Headers.Keys[i], "Accept-Ranges", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        allowRanges = true;
                    }

                    Log.Debug("v {0}: {1}", response.Headers.Keys[i], response.Headers[i]);
                }

                response.Close();
                lock (this)
                {
                    FileUtil.CreateFile(parameter.LocalFile, contentLength);
                }
                DownloadFileState = new FileState(parameter.TransferUrl, parameter.LocalFile, contentLength, (allowRanges ? _chunkCount : (short)1));
                AssignFileStateDelegate();
                DownloadFileState.FireFileDownloadBeginingEvent();
            }
            catch (WebException e)
            {
                Log.Warn("- Got an Excetpion {0} in Create WebException", e.ToString());
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    string a = ((HttpWebResponse)e.Response).StatusCode.ToString();
                    a += ((HttpWebResponse)e.Response).StatusDescription;
                    Log.Warn(a);
                }

                throw new WebException(e.Message);
            }
            catch (Exception e)
            {
                Log.Warn("- Got an Excetpion {0}  in Create Exception", e.ToString());
                throw new Exception(e.Message);
            }
        }
示例#14
0
 public HttpDownloader(TransferParameter parameter)
 {
     Parameter         = parameter;
     DownloadFileState = null;
     _chunkCount       = (parameter.ChunkCount > 0 ? parameter.ChunkCount : (short)5);
 }
示例#15
0
        public static bool Exists(string remotefile, ref FileItem oFileItem, ref Apply oApply, ref TransferParameter oInputParam, bool bHasFinished)
        {
            var filepath = GetFilePath(remotefile);

            if (!File.Exists(filepath))
            {
                return(false);
            }
            bool isExist  = false;
            var  document = new XmlDocument();

            document.Load(filepath);
            XmlNode rootNode = document.DocumentElement;

            if (rootNode == null)
            {
                return(false);
            }
            foreach (XmlNode fileItem in rootNode.ChildNodes)
            {
                if (fileItem.Attributes == null)
                {
                    continue;
                }
                oFileItem.Guid        = fileItem.Attributes["Guid"].Value;
                oFileItem.StartTime   = TypeConvert.ToDateTime(fileItem.Attributes["StartTime"].Value, DateTime.MinValue);
                oFileItem.EndTime     = TypeConvert.ToDateTime(fileItem.Attributes["EndTime"].Value, DateTime.MinValue);
                oFileItem.IsCompleted = TypeConvert.ToBool(fileItem.Attributes["IsCompleted"].Value, false);
                oFileItem.HasBreaked  = TypeConvert.ToBool(fileItem.Attributes["HasBreaked"].Value, false);
                oFileItem.BreakInfo   = fileItem.Attributes["BreakInfo"].Value;

                foreach (XmlNode item in fileItem.ChildNodes)
                {
                    if (item.Attributes == null)
                    {
                        continue;
                    }
                    if (item.Name == "Apply")
                    {
                        oApply.LastModified   = TypeConvert.ToDateTime(item.Attributes["LastModified"].Value, DateTime.MinValue);
                        oApply.FileSize       = TypeConvert.ToLong(item.Attributes["FileSize"].Value, 0);
                        oApply.AllowRanges    = TypeConvert.ToBool(item.Attributes["AllowRanges"].Value, false);
                        oApply.BlockSize      = TypeConvert.ToInt(item.Attributes["BlockSize"].Value, 0);
                        oApply.ActuallyChunks = TypeConvert.ToInt(item.Attributes["ActuallyChunks"].Value, 0);
                    }
                    else
                    {
                        oInputParam.TransferUrl = item.Attributes["TransferUrl"].Value;
                        oInputParam.LocalFile   = item.Attributes["LocalFile"].Value;
                        oInputParam.ChunkCount  = (short)TypeConvert.ToInt(item.Attributes["ChunkCount"].Value, 0);

                        if (DownloadState.Parameter.Equals(oInputParam) && !oFileItem.IsCompleted && !bHasFinished)
                        {
                            isExist = true;
                            break;
                        }
                        if (DownloadState.Parameter.Equals(oInputParam) && oFileItem.IsCompleted && bHasFinished)
                        {
                            isExist = true;
                            break;
                        }
                    }
                }
                if (isExist)
                {
                    break;
                }
            }
            return(isExist);
        }
示例#16
0
        private void DownloadFile(TransferParameter parameter)
        {
            try
            {
                string logstr = "下载任务开始,正在创建临时目录.";
                Log.Debug(logstr);
                string[]      tempList = Directory.GetDirectories(Logger.TemporaryDirectory);
                DirectoryInfo di       = null;
                if (tempList.Length > 0)
                {
                    foreach (string item in tempList)
                    {
                        di = new DirectoryInfo(item);
                        if (di.LastWriteTime <= DateTime.Now.AddMonths(-6))
                        {
                            FileUtil.DeleteDirectory(item);
                        }
                    }
                }

                logstr = "正在创建本地保存目录.";
                Log.Debug(logstr);
                string localDir = Path.GetDirectoryName(parameter.LocalFile);
                if (!string.IsNullOrEmpty(localDir))
                {
                    di = new DirectoryInfo(localDir);
                    if (!di.Exists)
                    {
                        di.Create();
                    }
                }

                logstr = "正在初始化...";
                Log.Debug(logstr + "\r\n" + parameter.ToString());
                DownloadState.BlockList.Clear();
                var currentApply = new Apply();
                DownloadState.BlockList = DownloadRequst.GetBlocks(parameter, ref currentApply);
                Log.Debug(currentApply.ToString());

                DownloadState.AllowAppend                  = false;
                DownloadState.StopDownload                 = false;
                DownloadState.HasFinished                  = false;
                DownloadState.IsCompleted                  = false;
                DownloadState.OutputDebugLog               = true;
                BlockEvents.CompletedBytesCount            = 0;
                BlockEvents.TotalBytes                     = currentApply.FileSize;
                ServicePointManager.DefaultConnectionLimit = 100;


                var historyApply     = new Apply();
                var historyFileItem  = new FileItem();
                var historyparameter = new TransferParameter();
                if (DownloadHistory.Exists(parameter.RemoteFile, ref historyFileItem, ref historyApply, ref historyparameter, false) &&
                    DownloadState.CurrentApply.Equals(historyApply) &&
                    !File.Exists(parameter.LocalFile))
                {
                    logstr = "已恢复下载.";
                    Log.Debug(logstr);
                    DownloadState.AllowAppend  = true;
                    historyFileItem.HasBreaked = true;
                    if (string.IsNullOrEmpty(historyFileItem.BreakInfo))
                    {
                        historyFileItem.BreakInfo = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    else
                    {
                        historyFileItem.BreakInfo += ";" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    DownloadHistory.Update(parameter.RemoteFile, historyFileItem);
                }
                else if (DownloadHistory.Exists(parameter.RemoteFile, ref historyFileItem, ref historyApply, ref historyparameter, true) &&
                         parameter.Equals(historyparameter) &&
                         File.Exists(historyparameter.LocalFile))
                {
                    var fileInfo = new FileInfo(parameter.LocalFile);
                    if (fileInfo.Length == DownloadState.CurrentApply.FileSize &&
                        fileInfo.LastWriteTime == DownloadState.CurrentApply.LastModified)
                    {
                        logstr = parameter.TransferUrl + "此前已完成下载.";
                        Log.Debug(logstr);
                        DownloadState.HasFinished       = true;
                        BlockEvents.CompletedBytesCount = fileInfo.Length;
                        BlockEvents.OnProgress(100, 100);
                        BlockEvents.OnCompleted();
                        return;
                    }
                }

                if (!DownloadState.AllowAppend && !DownloadState.HasFinished)
                {
                    DownloadHistory.Insert(parameter.RemoteFile);

                    logstr = "已将本次下载任务追加到历史.";
                    Log.Debug(logstr);
                }

                if (BlockEvents.Begining != null)
                {
                    BlockEvents.Begining(parameter.LocalFile);
                }

                logstr = "开始处理分块";
                Log.Warn(logstr);
                foreach (Blocks item in DownloadState.BlockList)
                {
                    item.ReStore();
                    item.Download();
                    logstr += item.ToString() + "\r\n";
                    Log.Debug(logstr);
                }
                Log.Info("文件下载完毕");
            }
            catch (Exception err)
            {
                BlockEvents.OnException(err);
            }
        }
示例#17
0
        public FtpWebUploader(TransferParameter parameter)
        {
            Parameter = parameter;

            Log.Debug("Create FtpWebUploader: url=" + parameter.TransferUrl + ",remoteFile=" + parameter.RemoteFile + ",localFile=" + parameter.LocalFile + ",chunkCount=" + parameter.ChunkSize.ToString());
        }
示例#18
0
 public FtpDownloader(TransferParameter parameter)
 {
     Parameter         = parameter;
     DownloadFileState = null;
     _transferUri      = new Uri(parameter.TransferUrl);
 }