public static int OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData) { DownLoadParam param = (DownLoadParam)extraData; FileDownloaderEx downloader = param.downloader; DownloadDataEx data = param.downloadData; if (downloader.canceled) { return(-1); } int readCount = size * nmemb; param.totalDownloaded += readCount; // save block to end of file downloader.SaveToFile(buf, readCount, data.DownloadingToStream); // send progress info if (data.IsProgressKnown) { downloader.RaiseProgressChanged(param.totalDownloaded, data.FileSize); } if (downloader.canceled) { return(-1); } else { return(size * nmemb); } }
public static DownloadTaskErrorCode GetByUrl( string url, string hostName, string dest, int timeout, EventHandler callbackComplete, DownloadProgressHandler callbackProgress, out string errMsg) { DownloadTaskErrorCode errCode = DownloadTaskErrorCode.Success; #if USE_CURL_DOWNLOADER FileDownloaderEx downloader = new FileDownloaderEx(); #else FileDownloader downloader = new FileDownloader(); #endif if (callbackComplete != null) { downloader.DownloadComplete += callbackComplete; } if (callbackProgress != null) { downloader.ProgressChanged += callbackProgress; } errMsg = ""; try { downloader.Download(url, hostName, dest, timeout); } catch (WebException webException) { errMsg = webException.Message; errCode = DownloadTaskErrorCode.NetworkError; } catch (ArgumentException argException) { errMsg = argException.Message; errCode = DownloadTaskErrorCode.UrlArgError; } catch (IOException ioException) { errMsg = ioException.Message; errCode = DownloadTaskErrorCode.IOError; } catch (Exception e) { errMsg = e.Message; errCode = DownloadTaskErrorCode.Unknown; } return(errCode); }
private static void SaveFingerPrint(String destFileName, FingerPrint fingerPrint) { String fingerPrintFileName = FileDownloaderEx.MakeFingerPrintFilePath(destFileName); SecurityElement finger_print = new SecurityElement("finger_print"); finger_print.AddAttribute("time_stamp", fingerPrint.timeStamp); finger_print.AddAttribute("file_size", fingerPrint.fileSize.ToString()); File.WriteAllText(fingerPrintFileName, finger_print.ToString()); }
private static FingerPrint LoadFingerPrint(String destFileName) { String fingerPrintFileName = FileDownloaderEx.MakeFingerPrintFilePath(destFileName); if (!File.Exists(fingerPrintFileName)) //记录文件尚未创建 { return new FingerPrint { timeStamp = "", fileSize = 0 } } ; try { SecurityElement xmlDoc = SecurityElement.FromString(File.ReadAllText(fingerPrintFileName)); String timeStamp = xmlDoc.Attributes["time_stamp"].ToString(); Int64 fileSize = Int64.Parse(xmlDoc.Attributes["file_size"].ToString()); return(new FingerPrint { timeStamp = timeStamp, fileSize = fileSize }); } catch (IOException) { return(new FingerPrint { timeStamp = "", fileSize = 0 }); } catch (System.IO.IsolatedStorage.IsolatedStorageException) { return(new FingerPrint { timeStamp = "", fileSize = 0 }); } catch (XmlSyntaxException) { return(new FingerPrint { timeStamp = "", fileSize = 0 }); } catch (FormatException) { return(new FingerPrint { timeStamp = "", fileSize = 0 }); } catch (NullReferenceException) { return(new FingerPrint { timeStamp = "", fileSize = 0 }); } }
private static void DeleteDestFile(String destFileName) { File.Delete(FileDownloaderEx.MakeFingerPrintFilePath(destFileName)); File.Delete(destFileName); }
/// <summary> /// return false when no more task /// </summary> /// <returns></returns> private Boolean WorkOnce() { Task activeTask; DownloadTaskInfo activeTaskSnapshot; lock (m_lock) { if (m_stop) //stop { return(false); } if (m_taskQueue.Count == 0) //nothing to do { return(false); } activeTask = m_taskQueue[0]; m_taskQueue.RemoveAt(0); activeTaskSnapshot = activeTask.ToDownloadTaskInfo(); activeTask.status = DownloadTaskStatus.Downloading; } if (File.Exists(activeTaskSnapshot.localPath)) //local file exist, check whether already finished { #if USE_CURL_DOWNLOADER if (!FileDownloaderEx.IsFileInProgress(activeTaskSnapshot.localPath)) #else if (!FileDownloader.IsFileInProgress(activeTaskSnapshot.localPath)) #endif { Int64 fileSize; string md5string = CalcFileMd5AndSize(activeTaskSnapshot.localPath, out fileSize); if (CompareMd5(md5string, activeTaskSnapshot.md5) == 0) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Finished; activeTask.totalSize = fileSize; activeTask.finishedSize = fileSize; RaiseTaskEndEvent(activeTask.ToDownloadTaskInfo()); return(true); } } else //file content is not correct { File.Delete(activeTaskSnapshot.localPath); //download again } } } #if USE_CURL_DOWNLOADER FileDownloaderEx downloader = new FileDownloaderEx(); #else FileDownloader downloader = new FileDownloader(); #endif Boolean bCanceled = false; downloader.ProgressChanged += (sender, arg) => { //update task status lock (m_lock) { if (!activeTask.status.CanPause()) //stopped { downloader.Cancel(); bCanceled = true; } else { activeTask.totalSize = arg.TotalFileSize; activeTask.finishedSize = arg.CurrentFileSize; } } //Thread.Sleep(1); }; downloader.DownloadComplete += (sender, arg) => { }; try { Directory.CreateDirectory(m_rootDir); downloader.Download(activeTask.url, activeTask.hostName, activeTask.localPath, DownloadMan.reqTimeOut); if (!bCanceled) { Int64 fileSize; string md5string = CalcFileMd5AndSize(activeTaskSnapshot.localPath, out fileSize); if (CompareMd5(md5string, activeTaskSnapshot.md5) == 0) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Finished; activeTask.totalSize = fileSize; activeTask.finishedSize = fileSize; } } else //md5 dismatch { File.Delete(activeTaskSnapshot.localPath); lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.Md5Dismatch; activeTask.errorMessage = activeTask.errorCode.ToString(); } } } } catch (WebException e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.NetworkError; activeTask.innerErrorCode = (Int32)e.Status; activeTask.errorMessage = e.Message; } } catch (IOException e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.IOError; activeTask.innerErrorCode = 0; activeTask.errorMessage = e.Message; } } catch (ArgumentException e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.UrlArgError; activeTask.innerErrorCode = 0; activeTask.errorMessage = e.Message; } } catch (Exception e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.Unknown; activeTask.innerErrorCode = 0; activeTask.errorMessage = e.Message; } } if (!bCanceled) { RaiseTaskEndEvent(activeTask.ToDownloadTaskInfo()); } return(true); }