1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . IO ;
3
4
using System . Text ;
4
5
#if NET40
@@ -21,14 +22,6 @@ public class ResumablePut
21
22
private const int blockMashk = ( 1 << blockBits ) - 1 ;
22
23
private static int BLOCKSIZE = 4 * 1024 * 1024 ;
23
24
24
- #region 记录总文件大小,用于计算上传百分比
25
-
26
- private long fsize ;
27
- private float chunks ;
28
- private float uploadedChunks = 0 ;
29
-
30
- #endregion
31
-
32
25
/// <summary>
33
26
/// 上传完成事件
34
27
/// </summary>
@@ -37,10 +30,6 @@ public class ResumablePut
37
30
/// 上传Failure事件
38
31
/// </summary>
39
32
public event EventHandler < CallRet > PutFailure ;
40
- /// <summary>
41
- /// 进度提示事件
42
- /// </summary>
43
- public event Action < float > Progress ;
44
33
45
34
Settings putSetting ;
46
35
@@ -71,7 +60,6 @@ public ResumablePutExtra Extra
71
60
/// <param name="extra"></param>
72
61
public ResumablePut ( Settings putSetting , ResumablePutExtra extra )
73
62
{
74
- extra . chunkSize = putSetting . ChunkSize ;
75
63
this . putSetting = putSetting ;
76
64
this . extra = extra ;
77
65
}
@@ -88,36 +76,23 @@ public CallRet PutFile(string upToken, string localFile, string key)
88
76
{
89
77
throw new Exception ( string . Format ( "{0} does not exist" , localFile ) ) ;
90
78
}
79
+
91
80
PutAuthClient client = new PutAuthClient ( upToken ) ;
92
81
CallRet ret ;
93
82
using ( FileStream fs = File . OpenRead ( localFile ) )
94
83
{
95
84
int block_cnt = block_count ( fs . Length ) ;
96
- fsize = fs . Length ;
97
- chunks = fsize / extra . chunkSize + 1 ;
85
+ long fsize = fs . Length ;
98
86
extra . Progresses = new BlkputRet [ block_cnt ] ;
99
- //并行上传
100
- #if NET35 || NET20
87
+ byte [ ] byteBuf = new byte [ BLOCKSIZE ] ;
88
+ int readLen = BLOCKSIZE ;
101
89
for ( int i = 0 ; i < block_cnt ; i ++ )
102
90
{
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 ) ;
116
95
fs . Read ( byteBuf , 0 , readLen ) ;
117
- #if NET40
118
- }
119
- #endif
120
- //并行上传BLOCK
121
96
BlkputRet blkRet = ResumableBlockPut ( client , byteBuf , i , readLen ) ;
122
97
if ( blkRet == null )
123
98
{
@@ -127,19 +102,11 @@ public CallRet PutFile(string upToken, string localFile, string key)
127
102
{
128
103
extra . OnNotify ( new PutNotifyEvent ( i , readLen , extra . Progresses [ i ] ) ) ;
129
104
}
130
- #if NET35 || NET20
131
105
}
132
- #elif NET40
133
- } ) ;
134
- #endif
135
- ret = Mkfile ( client , key , fs . Length ) ;
106
+ ret = Mkfile ( client , key , fsize ) ;
136
107
}
137
108
if ( ret . OK )
138
109
{
139
- if ( Progress != null )
140
- {
141
- Progress ( 1.0f ) ;
142
- }
143
110
if ( PutFinished != null )
144
111
{
145
112
PutFinished ( this , ret ) ;
@@ -155,104 +122,56 @@ public CallRet PutFile(string upToken, string localFile, string key)
155
122
return ret ;
156
123
}
157
124
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
-
171
125
private BlkputRet ResumableBlockPut ( Client client , byte [ ] body , int blkIdex , int blkSize )
172
126
{
173
- int bodyLength ;
174
- int chunkSize = extra . chunkSize ;
175
127
#region Mkblock
176
- if ( extra . Progresses [ blkIdex ] == null )
128
+ uint crc32 = CRC32 . CheckSumBytes ( body , blkSize ) ;
129
+ for ( int i = 0 ; i < putSetting . TryTimes ; i ++ )
177
130
{
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
183
132
{
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 ) ;
198
134
}
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 )
209
136
{
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 ) )
212
138
{
213
- if ( i == ( putSetting . TryTimes - 1 ) )
214
- {
215
- return null ;
216
- }
217
- continue ;
139
+ throw ee ;
218
140
}
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 ) )
220
147
{
221
- uploadedChunks ++ ;
222
- if ( Progress != null )
223
- {
224
- Progress ( ( float ) uploadedChunks / chunks ) ;
225
- }
226
- break ;
148
+ return null ;
227
149
}
150
+ System . Threading . Thread . Sleep ( 1000 ) ;
151
+ continue ;
152
+ }
153
+ else
154
+ {
155
+ break ;
228
156
}
229
157
}
230
158
#endregion
159
+
231
160
return extra . Progresses [ blkIdex ] ;
232
- }
161
+ }
233
162
234
- private BlkputRet Mkblock ( Client client , byte [ ] firstChunk , long blkSize )
163
+ private BlkputRet Mkblock ( Client client , byte [ ] firstChunk , int blkSize )
235
164
{
236
165
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 ) ;
238
168
if ( callRet . OK )
239
169
{
240
170
return QiniuJsonHelper . ToObject < BlkputRet > ( callRet . Response ) ;
241
171
}
242
172
return null ;
243
173
}
244
174
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
-
256
175
private CallRet Mkfile ( Client client , string key , long fsize )
257
176
{
258
177
StringBuilder urlBuilder = new StringBuilder ( ) ;
0 commit comments