@@ -22,14 +22,6 @@ public class ResumablePut
22
22
private const int blockMashk = ( 1 << blockBits ) - 1 ;
23
23
private static int BLOCKSIZE = 4 * 1024 * 1024 ;
24
24
25
- #region 记录总文件大小,用于计算上传百分比
26
-
27
- private long fsize ;
28
- private float chunks ;
29
- private float uploadedChunks = 0 ;
30
-
31
- #endregion
32
-
33
25
/// <summary>
34
26
/// 上传完成事件
35
27
/// </summary>
@@ -38,10 +30,6 @@ public class ResumablePut
38
30
/// 上传Failure事件
39
31
/// </summary>
40
32
public event EventHandler < CallRet > PutFailure ;
41
- /// <summary>
42
- /// 进度提示事件
43
- /// </summary>
44
- public event Action < float > Progress ;
45
33
46
34
Settings putSetting ;
47
35
@@ -72,7 +60,6 @@ public ResumablePutExtra Extra
72
60
/// <param name="extra"></param>
73
61
public ResumablePut ( Settings putSetting , ResumablePutExtra extra )
74
62
{
75
- extra . chunkSize = putSetting . ChunkSize ;
76
63
this . putSetting = putSetting ;
77
64
this . extra = extra ;
78
65
}
@@ -95,19 +82,17 @@ public CallRet PutFile(string upToken, string localFile, string key)
95
82
using ( FileStream fs = File . OpenRead ( localFile ) )
96
83
{
97
84
int block_cnt = block_count ( fs . Length ) ;
98
- fsize = fs . Length ;
99
- chunks = fsize / extra . chunkSize + 1 ;
85
+ long fsize = fs . Length ;
100
86
extra . Progresses = new BlkputRet [ block_cnt ] ;
101
- //并行上传
102
87
byte [ ] byteBuf = new byte [ BLOCKSIZE ] ;
103
88
int readLen = BLOCKSIZE ;
104
89
for ( int i = 0 ; i < block_cnt ; i ++ )
105
90
{
106
- if ( ( long ) ( i + 1 ) * BLOCKSIZE > fsize )
91
+ if ( i == ( block_cnt - 1 ) ) {
107
92
readLen = ( int ) ( fsize - ( long ) i * BLOCKSIZE ) ;
93
+ }
108
94
fs . Seek ( ( long ) i * BLOCKSIZE , SeekOrigin . Begin ) ;
109
95
fs . Read ( byteBuf , 0 , readLen ) ;
110
- //并行上传BLOCK
111
96
BlkputRet blkRet = ResumableBlockPut ( client , byteBuf , i , readLen ) ;
112
97
if ( blkRet == null )
113
98
{
@@ -118,14 +103,10 @@ public CallRet PutFile(string upToken, string localFile, string key)
118
103
extra . OnNotify ( new PutNotifyEvent ( i , readLen , extra . Progresses [ i ] ) ) ;
119
104
}
120
105
}
121
- ret = Mkfile ( client , key , fs . Length ) ;
106
+ ret = Mkfile ( client , key , fsize ) ;
122
107
}
123
108
if ( ret . OK )
124
109
{
125
- if ( Progress != null )
126
- {
127
- Progress ( 1.0f ) ;
128
- }
129
110
if ( PutFinished != null )
130
111
{
131
112
PutFinished ( this , ret ) ;
@@ -141,63 +122,43 @@ public CallRet PutFile(string upToken, string localFile, string key)
141
122
return ret ;
142
123
}
143
124
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
-
157
125
private BlkputRet ResumableBlockPut ( Client client , byte [ ] body , int blkIdex , int blkSize )
158
126
{
159
- int bodyLength ;
160
- int chunkSize = extra . chunkSize ;
161
127
#region Mkblock
162
- if ( extra . Progresses [ blkIdex ] == null )
128
+ uint crc32 = CRC32 . CheckSumBytes ( body , blkSize ) ;
129
+ for ( int i = 0 ; i < putSetting . TryTimes ; i ++ )
163
130
{
164
- uint crc32 = CRC32 . CheckSumBytes ( body , blkSize ) ;
165
- for ( int i = 0 ; i < putSetting . TryTimes ; i ++ )
131
+ try
166
132
{
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 ) )
181
138
{
182
- if ( i == ( putSetting . TryTimes - 1 ) )
183
- {
184
- return null ;
185
- }
186
- System . Threading . Thread . Sleep ( 1000 ) ;
187
- continue ;
139
+ throw ee ;
188
140
}
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 ) )
190
147
{
191
- progress ( ) ;
192
- break ;
148
+ return null ;
193
149
}
150
+ System . Threading . Thread . Sleep ( 1000 ) ;
151
+ continue ;
152
+ }
153
+ else
154
+ {
155
+ break ;
194
156
}
195
157
}
196
158
#endregion
197
159
198
160
return extra . Progresses [ blkIdex ] ;
199
- }
200
-
161
+ }
201
162
202
163
private BlkputRet Mkblock ( Client client , byte [ ] firstChunk , int blkSize )
203
164
{
0 commit comments