Exemplo n.º 1
0
        /// <summary>
        /// 写文件块
        /// </summary>
        /// <param name="savePath"></param>
        /// <param name="section"></param>
        private void WriteSection(string savePath, SectionData section)
        {
            long startPosition = section.CurrentPageIndex * section.SectionSize;

            byte[] data  = section.Data;
            byte   value = 1;

            // 检测该块是否已经被写入 避免重复写
            using (FileStream fileWrite = new FileStream(savePath, FileMode.Open, FileAccess.Write))
            {
                fileWrite.Seek(startPosition, SeekOrigin.Begin);
                fileWrite.Write(data, 0, data.Length);

                long updatePosition = section.FileSize + section.CurrentPageIndex;
                fileWrite.Seek(updatePosition, SeekOrigin.Begin);
                fileWrite.WriteByte(value);

                fileWrite.Close();
                fileWrite.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取分块数据
        /// </summary>
        /// <param name="sectionIndex"></param>
        /// <returns></returns>
        public SectionData GetFileData(int sectionIndex)
        {
            if (sectionIndex < 0 || sectionIndex >= this.TotalSectionCount)
            {
                return(null);
            }
            SectionData section = new SectionData();

            section.FileName         = this.FileName;
            section.FileSize         = this.FileSize;
            section.SectionSize      = this.SectionSize;
            section.Pages            = this.TotalSectionCount;
            section.CurrentPageIndex = sectionIndex;
            section.Data             = this.GetSection(sectionIndex);
            section.MD5 = FileSectionUtils.GetMD5Hash(section.Data);
            if (this.Setting.DataType == SectionDataType.Base64)
            {
                section.Base64String = Convert.ToBase64String(section.Data);
                section.Data         = null;
            }
            return(section);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        public SaveResult SaveSection(SectionData section)
        {
            SaveResult result = new SaveResult();

            if (section == null || (section.Data == null && string.IsNullOrEmpty(section.Base64String)))
            {
                result.Message = "没有数据!";
                return(result);
            }
            if (string.IsNullOrEmpty(this.FileName))
            {
                this.FileName = section.FileName;
            }
            string savePath = this.Dir + System.IO.Path.DirectorySeparatorChar + this.FileName;

            //已经存在
            if (System.IO.File.Exists(savePath))
            {
                if (section.CurrentPageIndex == 0)
                {
                    System.IO.File.Delete(savePath);
                }
                else
                {
                    result.Success = true;
                    result.Message = "文件已存在!";
                    return(result);
                }
            }
            //查找特殊后缀名
            savePath += ".fstp";
            if (!System.IO.File.Exists(savePath))
            {
                //不存在则说明是首次请求进行创建, 创建不一定是 CurrentPageIndex==0 的请求
                this.CreateSaveFile(savePath, section.FileSize, section.Pages);
            }
            if (string.IsNullOrEmpty(section.MD5))
            {
                result.Success = true;
                result.Message = "MD5缺失!";
                return(result);
            }
            //验证MD5
            if (section.Data != null && section.Data.Length > 0)
            {
                string MD5 = FileSectionUtils.GetMD5Hash(section.Data);
                if (!MD5.Equals(section.MD5))
                {
                    result.Success = false;
                    result.Message = "MD5错误!";
                    return(result);
                }
                else
                {
                    this.WriteSection(savePath, section);
                }
            }
            else if (!string.IsNullOrEmpty(section.Base64String))
            {
                byte[] buffer = Convert.FromBase64String(section.Base64String);
                section.Data = buffer;
                string MD5 = FileSectionUtils.GetMD5Hash(buffer);
                if (!MD5.Equals(section.MD5))
                {
                    result.Success = false;
                    result.Message = "MD5错误!";
                    return(result);
                }
                else
                {
                    this.WriteSection(savePath, section);
                }
            }
            result.Success = true;
            //检查是否全部写完
            result.FileFinished = CheckSaveOver(savePath, section);
            return(result);
        }