11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using System . Text ;
45#if NET40
@@ -21,14 +22,6 @@ public class ResumablePut
2122 private const int blockMashk = ( 1 << blockBits ) - 1 ;
2223 private static int BLOCKSIZE = 4 * 1024 * 1024 ;
2324
24- #region 记录总文件大小,用于计算上传百分比
25-
26- private long fsize ;
27- private float chunks ;
28- private float uploadedChunks = 0 ;
29-
30- #endregion
31-
3225 /// <summary>
3326 /// 上传完成事件
3427 /// </summary>
@@ -37,10 +30,6 @@ public class ResumablePut
3730 /// 上传Failure事件
3831 /// </summary>
3932 public event EventHandler < CallRet > PutFailure ;
40- /// <summary>
41- /// 进度提示事件
42- /// </summary>
43- public event Action < float > Progress ;
4433
4534 Settings putSetting ;
4635
@@ -71,7 +60,6 @@ public ResumablePutExtra Extra
7160 /// <param name="extra"></param>
7261 public ResumablePut ( Settings putSetting , ResumablePutExtra extra )
7362 {
74- extra . chunkSize = putSetting . ChunkSize ;
7563 this . putSetting = putSetting ;
7664 this . extra = extra ;
7765 }
@@ -88,36 +76,23 @@ public CallRet PutFile(string upToken, string localFile, string key)
8876 {
8977 throw new Exception ( string . Format ( "{0} does not exist" , localFile ) ) ;
9078 }
79+
9180 PutAuthClient client = new PutAuthClient ( upToken ) ;
9281 CallRet ret ;
9382 using ( FileStream fs = File . OpenRead ( localFile ) )
9483 {
9584 int block_cnt = block_count ( fs . Length ) ;
96- fsize = fs . Length ;
97- chunks = fsize / extra . chunkSize + 1 ;
85+ long fsize = fs . Length ;
9886 extra . Progresses = new BlkputRet [ block_cnt ] ;
99- //并行上传
100- #if NET35 || NET20
87+ byte [ ] byteBuf = new byte [ BLOCKSIZE ] ;
88+ int readLen = BLOCKSIZE ;
10189 for ( int i = 0 ; i < block_cnt ; i ++ )
10290 {
103- #elif NET40
104- Parallel . For ( 0 , block_cnt , ( i ) => {
105- #endif
106-
107- int readLen = BLOCKSIZE ;
108- if ( ( i + 1 ) * BLOCKSIZE > fsize )
109- readLen = ( int ) ( fsize - i * BLOCKSIZE ) ;
110- byte [ ] byteBuf = new byte [ readLen ] ;
111- #if NET40
112- lock ( fs )
113- {
114- #endif
115- fs . Seek ( i * BLOCKSIZE , SeekOrigin . Begin ) ;
91+ if ( i == block_cnt - 1 ) {
92+ readLen = ( int ) ( fsize - ( long ) i * BLOCKSIZE ) ;
93+ }
94+ fs . Seek ( ( long ) i * BLOCKSIZE , SeekOrigin . Begin ) ;
11695 fs . Read ( byteBuf , 0 , readLen ) ;
117- #if NET40
118- }
119- #endif
120- //并行上传BLOCK
12196 BlkputRet blkRet = ResumableBlockPut ( client , byteBuf , i , readLen ) ;
12297 if ( blkRet == null )
12398 {
@@ -127,19 +102,11 @@ public CallRet PutFile(string upToken, string localFile, string key)
127102 {
128103 extra . OnNotify ( new PutNotifyEvent ( i , readLen , extra . Progresses [ i ] ) ) ;
129104 }
130- #if NET35 || NET20
131105 }
132- #elif NET40
133- } ) ;
134- #endif
135- ret = Mkfile ( client , key , fs . Length ) ;
106+ ret = Mkfile ( client , key , fsize ) ;
136107 }
137108 if ( ret . OK )
138109 {
139- if ( Progress != null )
140- {
141- Progress ( 1.0f ) ;
142- }
143110 if ( PutFinished != null )
144111 {
145112 PutFinished ( this , ret ) ;
@@ -155,104 +122,56 @@ public CallRet PutFile(string upToken, string localFile, string key)
155122 return ret ;
156123 }
157124
158-
159- /// <summary>
160- /// 百分比进度提示
161- /// </summary>
162- private void progress ( )
163- {
164- uploadedChunks ++ ;
165- if ( Progress != null )
166- {
167- Progress ( ( float ) uploadedChunks / chunks ) ;
168- }
169- }
170-
171125 private BlkputRet ResumableBlockPut ( Client client , byte [ ] body , int blkIdex , int blkSize )
172126 {
173- int bodyLength ;
174- int chunkSize = extra . chunkSize ;
175127 #region Mkblock
176- if ( extra . Progresses [ blkIdex ] == null )
128+ uint crc32 = CRC32 . CheckSumBytes ( body , blkSize ) ;
129+ for ( int i = 0 ; i < putSetting . TryTimes ; i ++ )
177130 {
178- bodyLength = chunkSize < blkSize ? chunkSize : blkSize ;
179- byte [ ] firstChunk = new byte [ bodyLength ] ;
180- Array . Copy ( body , 0 , firstChunk , 0 , bodyLength ) ;
181- uint crc32 = CRC32 . CheckSumBytes ( firstChunk ) ;
182- for ( int i = 0 ; i < putSetting . TryTimes ; i ++ )
131+ try
183132 {
184- extra . Progresses [ blkIdex ] = Mkblock ( client , firstChunk , body . Length ) ;
185- if ( extra . Progresses [ blkIdex ] == null || crc32 != extra . Progresses [ blkIdex ] . crc32 )
186- {
187- if ( i == ( putSetting . TryTimes - 1 ) )
188- {
189- return null ;
190- }
191- continue ;
192- }
193- else
194- {
195- progress ( ) ;
196- break ;
197- }
133+ extra . Progresses [ blkIdex ] = Mkblock ( client , body , blkSize ) ;
198134 }
199- }
200- #endregion
201-
202- #region PutBlock
203- while ( extra . Progresses [ blkIdex ] . offset < blkSize )
204- {
205- bodyLength = ( chunkSize < ( blkSize - extra . Progresses [ blkIdex ] . offset ) ) ? chunkSize : ( int ) ( blkSize - extra . Progresses [ blkIdex ] . offset ) ;
206- byte [ ] chunk = new byte [ bodyLength ] ;
207- Array . Copy ( body , extra . Progresses [ blkIdex ] . offset , chunk , 0 , bodyLength ) ;
208- for ( int i = 0 ; i < putSetting . TryTimes ; i ++ )
135+ catch ( Exception ee )
209136 {
210- extra . Progresses [ blkIdex ] = BlockPut ( client , extra . Progresses [ blkIdex ] , new MemoryStream ( chunk ) , bodyLength ) ;
211- if ( extra . Progresses [ blkIdex ] == null )
137+ if ( i == ( putSetting . TryTimes - 1 ) )
212138 {
213- if ( i == ( putSetting . TryTimes - 1 ) )
214- {
215- return null ;
216- }
217- continue ;
139+ throw ee ;
218140 }
219- 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 ) )
220147 {
221- uploadedChunks ++ ;
222- if ( Progress != null )
223- {
224- Progress ( ( float ) uploadedChunks / chunks ) ;
225- }
226- break ;
148+ return null ;
227149 }
150+ System . Threading . Thread . Sleep ( 1000 ) ;
151+ continue ;
152+ }
153+ else
154+ {
155+ break ;
228156 }
229157 }
230158 #endregion
159+
231160 return extra . Progresses [ blkIdex ] ;
232- }
161+ }
233162
234- private BlkputRet Mkblock ( Client client , byte [ ] firstChunk , long blkSize )
163+ private BlkputRet Mkblock ( Client client , byte [ ] firstChunk , int blkSize )
235164 {
236165 string url = string . Format ( "{0}/mkblk/{1}" , Config . UP_HOST , blkSize ) ;
237- CallRet callRet = client . CallWithBinary ( url , "application/octet-stream" , new MemoryStream ( firstChunk ) , firstChunk . Length ) ;
166+
167+ CallRet callRet = client . CallWithBinary ( url , "application/octet-stream" , new MemoryStream ( firstChunk , 0 , blkSize ) , blkSize ) ;
238168 if ( callRet . OK )
239169 {
240170 return QiniuJsonHelper . ToObject < BlkputRet > ( callRet . Response ) ;
241171 }
242172 return null ;
243173 }
244174
245- private BlkputRet BlockPut ( Client client , BlkputRet ret , Stream body , long length )
246- {
247- string url = string . Format ( "{0}/bput/{1}/{2}" , Config . UP_HOST , ret . ctx , ret . offset ) ;
248- CallRet callRet = client . CallWithBinary ( url , "application/octet-stream" , body , length ) ;
249- if ( callRet . OK )
250- {
251- return QiniuJsonHelper . ToObject < BlkputRet > ( callRet . Response ) ;
252- }
253- return null ;
254- }
255-
256175 private CallRet Mkfile ( Client client , string key , long fsize )
257176 {
258177 StringBuilder urlBuilder = new StringBuilder ( ) ;
0 commit comments