コード例 #1
0
        /// <summary>
        /// 序列化记录文件的数据一致性校验
        /// Md5值;文件的名字、大小、最后修改时间;CheckSum值
        /// </summary>
        /// <param name="uploadFile"></param>
        /// <param name="uploadStream"></param>
        /// <param name="enableCheckSum"></param>
        /// <returns></returns>
        public bool IsValid(string uploadFile, Stream uploadStream, bool enableCheckSum, ResumableUploadTypeEnum uploadType)
        {
            if (this.Md5 != ComputeHash.HashCode <UploadCheckPoint>(this))
            {
                return(false);
            }

            if (uploadType == ResumableUploadTypeEnum.UploadFile)
            {
                FileInfo upload = new FileInfo(uploadFile);
                if (this.FileStatus.Size != upload.Length || this.FileStatus.LastModified != upload.LastWriteTime)
                {
                    return(false);
                }
            }
            else if (this.FileStatus.Size != uploadStream.Length - uploadStream.Position)
            {
                return(false);
            }

            if (enableCheckSum)
            {
                if (this.FileStatus.CheckSum != null)
                {
                    try
                    {
                        if (uploadType == ResumableUploadTypeEnum.UploadFile)
                        {
                            using (FileStream fileStream = new FileStream(uploadFile, FileMode.Open))
                            {
                                //校验CheckSum值--UploadFile文件的一致性
                                return(this.FileStatus.CheckSum.Equals(CommonUtil.Base64Md5(fileStream)));
                            }
                        }
                        else
                        {
                            //校验CheckSum值--UploadStream流的一致性
                            long originPosition = uploadStream.Position;
                            bool flag           = this.FileStatus.CheckSum.Equals(CommonUtil.Base64Md5(uploadStream));
                            uploadStream.Seek(originPosition, SeekOrigin.Begin);
                            return(flag);
                        }
                    }
                    catch (Exception ex)
                    {
                        ObsException e = new ObsException(ex.Message, ex.InnerException);
                        e.ErrorType = ErrorType.Sender;
                        throw e;
                    }
                }
            }
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// 获取待上传文件/数据流的状态信息
        /// </summary>
        /// <param name="uploadFile"></param>
        /// <param name="uploadStream"></param>
        /// <param name="checkSum"></param>
        /// <returns></returns>
        public static FileStatus getFileStatus(string uploadFile, Stream uploadStream, bool checkSum, ResumableUploadTypeEnum uploadType)
        {
            FileStatus fileStatus = new FileStatus();

            if (uploadType == ResumableUploadTypeEnum.UploadFile)
            {
                FileInfo fileInfo = new FileInfo(uploadFile);
                fileStatus.Size         = fileInfo.Length;
                fileStatus.LastModified = fileInfo.LastWriteTime;
            }
            else
            {
                fileStatus.Size = uploadStream.Length - uploadStream.Position;
                //数据流方式LastModified置为空
                fileStatus.LastModified = null;
            }

            //若开启文件内容校验
            if (checkSum)
            {
                try
                {
                    if (uploadType == ResumableUploadTypeEnum.UploadFile)
                    {
                        using (FileStream fileStream = new FileStream(uploadFile, FileMode.Open))
                        {
                            //计算UploadFile的hash值
                            fileStatus.CheckSum = CommonUtil.Base64Md5(fileStream);
                        }
                    }
                    else
                    {
                        //计算UploadStream的hash值
                        long originPosition = uploadStream.Position;
                        fileStatus.CheckSum = CommonUtil.Base64Md5(uploadStream);
                        uploadStream.Seek(originPosition, SeekOrigin.Begin);
                    }
                }
                catch (Exception ex)
                {
                    ObsException e = new ObsException(ex.Message, ex.InnerException);
                    e.ErrorType = ErrorType.Sender;
                    throw e;
                }
            }

            return(fileStatus);
        }
コード例 #3
0
        public static string HashCode <T>(T obj)
        {
            StringBuilder sb = new StringBuilder();

            Type type = obj.GetType();

            foreach (var property in type.GetProperties())
            {
                //UploadCheckPoint类的hash值计算不包含属性Md5和UploadStream
                if (property.Name.Equals("Md5") || property.Name.Equals("UploadStream"))
                {
                    continue;
                }

                sb.Append(property.Name + ":" + property.GetValue(obj, null));
            }

            byte[] content = Encoding.UTF8.GetBytes(sb.ToString());
            return(CommonUtil.Base64Md5(content));
        }