/// <summary> /// 将UploadCheckPoint中的字段数据写入CheckPointFile文件 /// </summary> /// 多个线程都需要调用该方法,需保证线程安全性 public void Record(string checkPointFile) { this.Md5 = ComputeHash.HashCode <UploadCheckPoint>(this); XmlSerializer serializer = new XmlSerializer(this.GetType()); using (XmlTextWriter fs = new XmlTextWriter(checkPointFile, Encoding.UTF8)) { fs.Formatting = Formatting.Indented; serializer.Serialize(fs, this); } }
/// <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); }
/// <summary> /// 将DownloadCheckPoint中的字段数据写入CheckPointFile文件 /// </summary> /// 多个线程都需要调用该方法,需保证线程安全性 /// <param name="checkPointFile"></param> public void Record(string checkPointFile) { this.Md5 = ComputeHash.HashCode <DownloadCheckPoint>(this); try { XmlSerializer serializer = new XmlSerializer(this.GetType()); using (XmlTextWriter fs = new XmlTextWriter(checkPointFile, Encoding.UTF8)) { serializer.Serialize(fs, this); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 序列化记录文件的数据一致性校验 /// Md5值;临时文件的名字、大小;对象的大小、最后修改时间、Etag值 /// </summary> /// <param name="tmpFilePath"></param> /// <param name="obsClient"></param> /// <returns></returns> public bool IsValid(string tmpFilePath, ObsClient obsClient) { if (this.Md5 != ComputeHash.HashCode <DownloadCheckPoint>(this)) { return(false); } FileInfo fileInfo = new FileInfo(tmpFilePath); if (!this.TmpFileStatus.TmpFilePath.Equals(tmpFilePath) || this.TmpFileStatus.Size != fileInfo.Length) { return(false); } GetObjectMetadataResponse response = obsClient.GetObjectMetadata(BucketName, ObjectKey, VersionId); if (!this.ObjectStatus.Etag.Equals(response.ETag) || this.ObjectStatus.Size != response.ContentLength || this.ObjectStatus.LastModified != response.LastModified) { return(false); } return(true); }