示例#1
0
        private DownloadErrorCode?CheckDownloadedFile(ref DownloadTaskInfo taskInfo, out string errorMessage)
        {
            bool sizeCheck = taskInfo.Size <= 0L || taskInfo.Size == DownloadedSize;

            errorMessage = string.Empty;

            if (!sizeCheck)
            {
                errorMessage = Utility.Text.Format("Expected size is '{0}' while actual size is '{1}'", taskInfo.Size, DownloadedSize);
                return(DownloadErrorCode.WrongSize);
            }

            if (taskInfo.Crc32 == null)
            {
                return(null);
            }

            using (var fs = File.OpenRead(m_TempSavePath))
            {
                var actualCrc32 = Algorithm.Crc32.Sum(fs);
                if (actualCrc32 == taskInfo.Crc32.Value)
                {
                    return(null);
                }
                else
                {
                    errorMessage = Utility.Text.Format("CRC 32 inconsistency: expects '{0}' but actually is '{1}'.",
                                                       taskInfo.Crc32.Value, actualCrc32);
                    return(DownloadErrorCode.WrongChecksum);
                }
            }
        }
示例#2
0
        private void TackleDownloadingIsOver(ref DownloadTaskInfo taskInfo)
        {
            string errorMessage;
            var    errorCode = CheckDownloadedFile(ref taskInfo, out errorMessage);

            if (errorCode == null)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(taskInfo.SavePath) ?? throw new NullReferenceException());
                if (File.Exists(taskInfo.SavePath))
                {
                    File.Delete(taskInfo.SavePath);
                }

                File.Move(m_TempSavePath, taskInfo.SavePath);
                IsDone    = true;
                ErrorCode = null;
            }
            else
            {
                IsDone       = false;
                ErrorCode    = errorCode;
                ErrorMessage = errorMessage;
                File.Delete(m_TempSavePath);
            }
            m_Status = Status.Finished;
        }