Skip to content

Commit 87df4a3

Browse files
committed
fix
1 parent 924818b commit 87df4a3

File tree

4 files changed

+32
-104
lines changed

4 files changed

+32
-104
lines changed

Docs/README.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -426,16 +426,6 @@ public static void PutFile(string bucket, string key, string fname)
426426
string upToken = policy.Token();
427427
PutExtra extra = new PutExtra();
428428
IOClient client = new IOClient();
429-
client.PutFinished += new EventHandler<PutRet>((o, ret) => {
430-
if (ret.OK)
431-
{
432-
Console.WriteLine("Hash: " + ret.Hash);
433-
}
434-
else
435-
{
436-
Console.WriteLine("Failed to PutFile");
437-
}
438-
});
439429
client.PutFile(upToken, key, fname, extra);
440430
}
441431
```
@@ -457,20 +447,6 @@ public static void ResumablePutFile(string bucket, string key, string fname)
457447
ResumablePutExtra extra = new ResumablePutExtra();
458448
extra.Bucket = bucket;
459449
ResumablePut client = new ResumablePut(setting, extra);
460-
client.Progress += new Action<float>((p) => {
461-
Console.WriteLine("当前进度:{0}%", p * 100);
462-
463-
});
464-
client.PutFinished += new EventHandler<CallRet>((o, ret) => {
465-
if (ret.OK)
466-
{
467-
Console.WriteLine("上传成功:{0}",ret.Response);
468-
}
469-
else
470-
{
471-
Console.WriteLine("上传失败:{0}", ret.Response);
472-
}
473-
});
474450
client.PutFile(upToken, fname, Guid.NewGuid().ToString());
475451
}
476452
```

Qiniu.Test/IO/Resumable/ResumablePutTest.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public void ResumablePutFileTest()
3434
ResumablePut target = new ResumablePut(putSetting, extra); // TODO: 初始化为适当的值
3535
Console.WriteLine ("extra.Bucket:"+Bucket);
3636
string upToken = new PutPolicy(Bucket).Token(new Qiniu.Auth.digest.Mac());
37-
target.Progress += new Action<float>(target_Progress);
3837
TmpFIle file=new TmpFIle(1024*1024*4);
3938
target.PutFinished += new EventHandler<CallRet> ((o,e) => {
4039
file.Del ();
@@ -63,12 +62,5 @@ void extra_Notify(object sender, PutNotifyEvent e)
6362
PrintLn(e.BlkSize.ToString());
6463
PrintLn(e.Ret.offset.ToString());
6564
}
66-
void target_Progress(float obj)
67-
{
68-
if (obj > 0.999999)
69-
{
70-
PrintLn((obj * 100).ToString() + "%");
71-
}
72-
}
7365
}
7466
}

Qiniu/IO/Resumable/ResumablePut.cs

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

Qiniu/IO/Resumable/Settings.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ namespace Qiniu.IO.Resumable
66
/// </summary>
77
public class Settings
88
{
9-
int chunkSize;
10-
119
/// <summary>
1210
/// chunk大小,默认为4MB;
11+
/// 兼容保留
1312
/// </summary>
1413
public int ChunkSize {
15-
get { return chunkSize; }
16-
set { chunkSize = value; }
14+
get;
15+
set;
1716
}
1817

1918
int tryTimes;
@@ -33,8 +32,8 @@ public int TryTimes {
3332
/// <param name="tryTimes">失败重试次数,默认为3</param>
3433
public Settings (int chunkSize=1 << 22, int tryTimes=3)
3534
{
36-
//取消chunk
37-
this.chunkSize = 1 << 22;
35+
//chunkSize 已经删除,兼容保留
36+
3837
this.tryTimes = tryTimes;
3938
}
4039
}

0 commit comments

Comments
 (0)