@@ -22,14 +22,6 @@ public class ResumablePut
2222 private const int blockMashk = ( 1 << blockBits ) - 1 ;
2323 private static int BLOCKSIZE = 4 * 1024 * 1024 ;
2424
25- #region 记录总文件大小,用于计算上传百分比
26-
27- private long fsize ;
28- private float chunks ;
29- private float uploadedChunks = 0 ;
30-
31- #endregion
32-
3325 /// <summary>
3426 /// 上传完成事件
3527 /// </summary>
@@ -38,10 +30,6 @@ public class ResumablePut
3830 /// 上传Failure事件
3931 /// </summary>
4032 public event EventHandler < CallRet > PutFailure ;
41- /// <summary>
42- /// 进度提示事件
43- /// </summary>
44- public event Action < float > Progress ;
4533
4634 Settings putSetting ;
4735
@@ -72,7 +60,6 @@ public ResumablePutExtra Extra
7260 /// <param name="extra"></param>
7361 public ResumablePut ( Settings putSetting , ResumablePutExtra extra )
7462 {
75- extra . chunkSize = putSetting . ChunkSize ;
7663 this . putSetting = putSetting ;
7764 this . extra = extra ;
7865 }
@@ -95,19 +82,17 @@ public CallRet PutFile(string upToken, string localFile, string key)
9582 using ( FileStream fs = File . OpenRead ( localFile ) )
9683 {
9784 int block_cnt = block_count ( fs . Length ) ;
98- fsize = fs . Length ;
99- chunks = fsize / extra . chunkSize + 1 ;
85+ long fsize = fs . Length ;
10086 extra . Progresses = new BlkputRet [ block_cnt ] ;
101- //并行上传
10287 byte [ ] byteBuf = new byte [ BLOCKSIZE ] ;
10388 int readLen = BLOCKSIZE ;
10489 for ( int i = 0 ; i < block_cnt ; i ++ )
10590 {
106- if ( ( long ) ( i + 1 ) * BLOCKSIZE > fsize )
91+ if ( i == ( block_cnt - 1 ) ) {
10792 readLen = ( int ) ( fsize - ( long ) i * BLOCKSIZE ) ;
93+ }
10894 fs . Seek ( ( long ) i * BLOCKSIZE , SeekOrigin . Begin ) ;
10995 fs . Read ( byteBuf , 0 , readLen ) ;
110- //并行上传BLOCK
11196 BlkputRet blkRet = ResumableBlockPut ( client , byteBuf , i , readLen ) ;
11297 if ( blkRet == null )
11398 {
@@ -118,14 +103,10 @@ public CallRet PutFile(string upToken, string localFile, string key)
118103 extra . OnNotify ( new PutNotifyEvent ( i , readLen , extra . Progresses [ i ] ) ) ;
119104 }
120105 }
121- ret = Mkfile ( client , key , fs . Length ) ;
106+ ret = Mkfile ( client , key , fsize ) ;
122107 }
123108 if ( ret . OK )
124109 {
125- if ( Progress != null )
126- {
127- Progress ( 1.0f ) ;
128- }
129110 if ( PutFinished != null )
130111 {
131112 PutFinished ( this , ret ) ;
@@ -141,63 +122,43 @@ public CallRet PutFile(string upToken, string localFile, string key)
141122 return ret ;
142123 }
143124
144-
145- /// <summary>
146- /// 百分比进度提示
147- /// </summary>
148- private void progress ( )
149- {
150- uploadedChunks ++ ;
151- if ( Progress != null )
152- {
153- Progress ( ( float ) uploadedChunks / chunks ) ;
154- }
155- }
156-
157125 private BlkputRet ResumableBlockPut ( Client client , byte [ ] body , int blkIdex , int blkSize )
158126 {
159- int bodyLength ;
160- int chunkSize = extra . chunkSize ;
161127 #region Mkblock
162- if ( extra . Progresses [ blkIdex ] == null )
128+ uint crc32 = CRC32 . CheckSumBytes ( body , blkSize ) ;
129+ for ( int i = 0 ; i < putSetting . TryTimes ; i ++ )
163130 {
164- uint crc32 = CRC32 . CheckSumBytes ( body , blkSize ) ;
165- for ( int i = 0 ; i < putSetting . TryTimes ; i ++ )
131+ try
166132 {
167- try
168- {
169- extra . Progresses [ blkIdex ] = Mkblock ( client , body , blkSize ) ;
170- }
171- catch ( Exception ee )
172- {
173- if ( i == ( putSetting . TryTimes - 1 ) )
174- {
175- throw ee ;
176- }
177- System . Threading . Thread . Sleep ( 1000 ) ;
178- continue ;
179- }
180- if ( extra . Progresses [ blkIdex ] == null || crc32 != extra . Progresses [ blkIdex ] . crc32 )
133+ extra . Progresses [ blkIdex ] = Mkblock ( client , body , blkSize ) ;
134+ }
135+ catch ( Exception ee )
136+ {
137+ if ( i == ( putSetting . TryTimes - 1 ) )
181138 {
182- if ( i == ( putSetting . TryTimes - 1 ) )
183- {
184- return null ;
185- }
186- System . Threading . Thread . Sleep ( 1000 ) ;
187- continue ;
139+ throw ee ;
188140 }
189- else
141+ System . Threading . Thread . Sleep ( 1000 ) ;
142+ continue ;
143+ }
144+ if ( extra . Progresses [ blkIdex ] == null || crc32 != extra . Progresses [ blkIdex ] . crc32 )
145+ {
146+ if ( i == ( putSetting . TryTimes - 1 ) )
190147 {
191- progress ( ) ;
192- break ;
148+ return null ;
193149 }
150+ System . Threading . Thread . Sleep ( 1000 ) ;
151+ continue ;
152+ }
153+ else
154+ {
155+ break ;
194156 }
195157 }
196158 #endregion
197159
198160 return extra . Progresses [ blkIdex ] ;
199- }
200-
161+ }
201162
202163 private BlkputRet Mkblock ( Client client , byte [ ] firstChunk , int blkSize )
203164 {
0 commit comments