Skip to content

Commit 41d39f3

Browse files
committed
add resumble
1 parent c773572 commit 41d39f3

File tree

5 files changed

+164
-11
lines changed

5 files changed

+164
-11
lines changed

demo/Program.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using qiniu;
35
using System.Threading;
46

@@ -9,18 +11,22 @@ class MainClass
911
public static void Main (string[] args)
1012
{
1113
// 初始化qiniu配置,主要是API Keys
12-
qiniu.Config.ACCESS_KEY = "IT9iP3J9wdXXYsT1p8ns0gWD-CQOdLvIQuyE0FOi";
13-
qiniu.Config.SECRET_KEY = "zUCzekBtEqTZ4-WJPCGlBrr2PeyYxsYn98LxaivM";
14+
qiniu.Config.ACCESS_KEY = "IT9iP3J9wdXXYsT1p8ns0gWD-CQOdLvIQuyE0FOK";
15+
qiniu.Config.SECRET_KEY = "zUCzekBtEqTZ4-WJPCGlBrr2PeyYxsYn98LPaivM";
1416

1517
/**********************************************************************
1618
可以用下面的方法从配置文件中初始化
1719
qiniu.Config.InitFromAppConfig ();
1820
**********************************************************************/
1921

2022

23+
string localfile = "/Users/icattlecoder/Movies/tzd.rmvb";
24+
string bucket = "icattlecoder";
25+
string qiniukey = "tzd.rmvb";
2126
//======================================================================
2227
{
23-
QiniuFile qfile = new QiniuFile ("<input your bucket name>", "<input qiniu file key>", "<local disk file path");
28+
QiniuFile qfile = new QiniuFile (bucket, qiniukey, localfile);
29+
ResumbalePutEx puttedCtx = new ResumbalePutEx (localfile);
2430
ManualResetEvent done = new ManualResetEvent (false);
2531
qfile.UploadCompleted += (sender, e) => {
2632
Console.WriteLine (e.RawString);
@@ -29,22 +35,27 @@ public static void Main (string[] args)
2935
qfile.UploadFailed += (sender, e) => {
3036
Console.WriteLine (e.Error.ToString ());
3137
done.Set ();
38+
puttedCtx.Save();
3239
};
3340
qfile.UploadProgressChanged += (sender, e) => {
3441
int percentage = (int)(100 * e.BytesSent / e.TotalBytes);
3542
Console.Write (percentage);
3643
};
44+
qfile.UploadBlockCompleted += (sender, e) => {
45+
puttedCtx.Add(e.Index,e.Ctx);
46+
puttedCtx.Save();
47+
};
3748
// 上传为异步操作
3849
//上传本地文件到七牛云存储
39-
qfile.Upload ();
50+
qfile.Upload (puttedCtx.PuttedCtx);
4051
done.WaitOne ();
4152
}
4253

4354
//======================================================================
4455
{
4556

4657
try {
47-
QiniuFile qfile = new QiniuFile ("<input your bucket Name>", "<input qiniu file key>");
58+
QiniuFile qfile = new QiniuFile (bucket, qiniukey);
4859
QiniuFileInfo finfo = qfile.Stat ();
4960
if (finfo != null) {
5061
//删除七牛云空间的文件

demo/QiniuResumbleHelper.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Text;
3+
using System.Collections.Generic;
4+
using System.Security.Cryptography;
5+
using System.IO;
6+
using System.IO.Compression;
7+
using qiniu;
8+
namespace demo
9+
{
10+
public class ResumbalePutEx
11+
{
12+
private string puttedCtxDir;
13+
private string fileName;
14+
private string puttedCtxFileName;
15+
private Dictionary<int,BlkputRet> puttedCtx;
16+
17+
/// <summary>
18+
/// Gets the putted context.
19+
/// </summary>
20+
/// <value>The putted context.</value>
21+
public BlkputRet[] PuttedCtx {
22+
get {
23+
if (this.puttedCtx == null || this.puttedCtx.Count == 0)
24+
return null;
25+
BlkputRet[] result = new BlkputRet[this.puttedCtx.Count];
26+
for (int i = 0; i < this.puttedCtx.Count; i++) {
27+
if (this.puttedCtx.ContainsKey (i)) {
28+
result [i] = this.puttedCtx [i];
29+
} else {
30+
this.Delete ();
31+
return null;
32+
}
33+
}
34+
return result;
35+
}
36+
}
37+
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="demo.ResumbalePutEx"/> class.
40+
/// </summary>
41+
/// <param name="filename">Filename.</param>
42+
/// <param name="puttedCtxDir">Putted context dir.default set to System TempPath</param>
43+
public ResumbalePutEx (string filename, string puttedCtxDir=null){
44+
if (!File.Exists (filename)) {
45+
throw new Exception(string.Format("{0} does not exist", filename));
46+
}
47+
this.fileName = filename;
48+
if (puttedCtxDir != null) {
49+
if (Directory.Exists (puttedCtxDir)) {
50+
throw new Exception (string.Format ("{0} does not exist", puttedCtxDir));
51+
}
52+
this.puttedCtxDir = puttedCtxDir;
53+
} else {
54+
this.puttedCtxDir = Path.GetTempPath ();
55+
}
56+
string ctxfile = getFileBase64Sha1 (this.fileName);
57+
this.puttedCtxFileName = Path.Combine (this.puttedCtxDir, ctxfile);
58+
if (!File.Exists (this.puttedCtxFileName)) {
59+
File.Open (this.puttedCtxFileName, FileMode.Create);
60+
} else {
61+
this.puttedCtx = initloadPuttedCtx ();
62+
}
63+
}
64+
65+
private Dictionary<int,BlkputRet> initloadPuttedCtx(){
66+
67+
Dictionary<int,BlkputRet> result = new Dictionary<int, BlkputRet> ();
68+
string[] lines = File.ReadAllLines(this.puttedCtxFileName);
69+
foreach (string line in lines)
70+
{
71+
string[] fields = line.Split(',');
72+
BlkputRet ret = new BlkputRet();
73+
ret.offset = ulong.Parse(fields[1]);
74+
ret.ctx = fields[2];
75+
int idx = int.Parse(fields[0]);
76+
result.Add (idx, ret);
77+
}
78+
return result;
79+
}
80+
81+
/// <summary>
82+
/// Save this putted result to disk file.
83+
/// </summary>
84+
public void Save(){
85+
StringBuilder sb = new StringBuilder ();
86+
foreach (int i in this.puttedCtx.Keys) {
87+
string content = i + "," + this.puttedCtx[i].offset + "," + this.puttedCtx[i].ctx + "\n";
88+
sb.Append (content);
89+
}
90+
File.WriteAllText (this.puttedCtxFileName, sb.ToString ());
91+
}
92+
93+
/// <summary>
94+
///
95+
/// </summary>
96+
/// <param name="tempFileName"></param>
97+
/// <param name="idx"></param>
98+
/// <param name="ret"></param>
99+
public void Add(int idx, BlkputRet ret)
100+
{
101+
this.puttedCtx [idx] = ret;
102+
}
103+
104+
/// <summary>
105+
/// Adds the and sync.
106+
/// </summary>
107+
/// <param name="idx">Index.</param>
108+
/// <param name="ret">Ret.</param>
109+
public void AddAndSave(int idx,BlkputRet ret){
110+
this.Add (idx, ret);
111+
this.Save ();
112+
}
113+
114+
/// <summary>
115+
/// Delete this instance.
116+
/// </summary>
117+
public void Delete(){
118+
if (File.Exists (this.puttedCtxFileName)) {
119+
this.puttedCtx.Clear ();
120+
File.Delete (this.puttedCtxFileName);
121+
}
122+
}
123+
124+
/// <summary>
125+
/// 获取文件的SHA1值
126+
/// </summary>
127+
/// <param name="filename"></param>
128+
/// <returns>base64编码的sha1值</returns>
129+
private static string getFileBase64Sha1(string filename)
130+
{
131+
SHA1 sha1 = new SHA1CryptoServiceProvider();
132+
using (Stream reader = System.IO.File.OpenRead(filename))
133+
{
134+
byte[] result = sha1.ComputeHash(reader);
135+
return BitConverter.ToString(result);
136+
}
137+
}
138+
}
139+
}
140+
141+

demo/demo.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<ItemGroup>
3535
<Compile Include="Program.cs" />
3636
<Compile Include="Properties\AssemblyInfo.cs" />
37+
<Compile Include="QiniuResumbleHelper.cs" />
3738
</ItemGroup>
3839
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
3940
<ItemGroup>

qiniu-csharp-sdk.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ Global
2121
{8D120ACA-4BF9-42A7-81C6-8BE39FF5ED42}.Release|Any CPU.Build.0 = Release|Any CPU
2222
EndGlobalSection
2323
GlobalSection(MonoDevelopProperties) = preSolution
24-
StartupItem = qiniu.csproj
24+
StartupItem = demo\demo.csproj
2525
EndGlobalSection
2626
EndGlobal

sdk/QiniuFile.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,20 @@ private void uploadSmallFile(string token){
228228
/// Uploads the big file ( > 8MB ).
229229
/// </summary>
230230
/// <param name="token">Token.</param>
231-
private void uploadBigFile(string token,BlkputRet[] blkRets=null){
231+
private void uploadBigFile(string token,BlkputRet[] puttedBlk=null){
232232
uploading = true;
233233
Action a = () => {
234234
FileInfo finfo = new FileInfo (this.localfile);
235235
int blockcount = block_count (finfo.Length);
236-
if (blkRets == null) {
237-
blkRets = new BlkputRet[blockcount];
238-
}
236+
237+
BlkputRet []blkRets = new BlkputRet[blockcount];
239238
using (FileStream fs = File.OpenRead (this.localfile)) {
240239
long totalSent = 0;
241240
int readLen = BLOCKSIZE;
242241
byte[] buf = new byte[readLen];
243242
for (int i = 0; i < blockcount; i++) {
244-
if (blkRets [i] != null) {
243+
if (puttedBlk!=null&&i< puttedBlk.Length) {
244+
blkRets[i] = puttedBlk[i];
245245
continue;
246246
}
247247
if (i == blockcount - 1) {

0 commit comments

Comments
 (0)