Exemple #1
0
        private static void SaveFingerPrint(String destFileName, FingerPrint fingerPrint)
        {
            String fingerPrintFileName = FileDownloadHelper.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());
        }
Exemple #2
0
        private static FingerPrint LoadFingerPrint(String destFileName)
        {
            String fingerPrintFileName = FileDownloadHelper.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
                });
            }
        }
Exemple #3
0
        private static void DeleteDestFile(String destFileName)
        {
            File.Delete(FileDownloadHelper.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_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 (!FileDownloadHelper.IsFileInProgress(activeTaskSnapshot.localPath))
                {
                    Int64 fileSize;
                    if (CalcFileMd5AndSize(activeTaskSnapshot.localPath, out fileSize) == activeTaskSnapshot.md5)
                    {
                        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
                    }
                }
            }

            FileDownloadHelper downloadHelper = new FileDownloadHelper();

            Boolean bCanceled = false;

            downloadHelper.ProgressChanged += (sender, arg) =>
            {
                //update task status
                lock (m_lock)
                {
                    if (!activeTask.status.CanPause())                                  //stopped
                    {
                        downloadHelper.Cancel();
                        bCanceled = true;
                    }
                    else
                    {
                        activeTask.totalSize    = arg.TotalFileSize;
                        activeTask.finishedSize = arg.CurrentFileSize;
                    }
                }
            };

            downloadHelper.DownloadComplete += (sender, arg) =>
            {
            };

            try
            {
                Directory.CreateDirectory(m_rootDir);

                downloadHelper.Download(activeTask.url, activeTask.localPath);
                if (!bCanceled)
                {
                    Int64 fileSize;
                    if (CalcFileMd5AndSize(activeTaskSnapshot.localPath, out fileSize) == activeTaskSnapshot.md5)
                    {
                        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;
                        }
                    }
                }
            }
            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;
                }
            }

            if (!bCanceled)
            {
                RaiseTaskEndEvent(activeTask.ToDownloadTaskInfo());
            }

            return(true);
        }