|
| 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 | + |
0 commit comments