|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Windows; |
| 6 | +using System.Windows.Controls; |
| 7 | +using System.Windows.Documents; |
| 8 | +using System.Windows.Input; |
| 9 | +using System.Windows.Media; |
| 10 | +using System.Windows.Media.Animation; |
| 11 | +using System.Windows.Shapes; |
| 12 | +using System.IO; |
| 13 | + |
| 14 | +namespace Uploader.Client |
| 15 | +{ |
| 16 | + public partial class ListItem : UserControl |
| 17 | + { |
| 18 | + public ListItem() |
| 19 | + { |
| 20 | + InitializeComponent(); |
| 21 | + FilePosition = 0; |
| 22 | + } |
| 23 | + |
| 24 | + FileInfo _File; |
| 25 | + public FileInfo File |
| 26 | + { |
| 27 | + get { return _File; } |
| 28 | + set |
| 29 | + { |
| 30 | + _File = value; |
| 31 | + this.uxText.Text = _File.Name; |
| 32 | + } |
| 33 | + } |
| 34 | + private long FilePosition; |
| 35 | + private long FileLength; |
| 36 | + private int CurrentStep = 0; |
| 37 | + private int Steps = 0; |
| 38 | + |
| 39 | + public void Send() |
| 40 | + { |
| 41 | + try |
| 42 | + { |
| 43 | + //send first chunk, start upload. |
| 44 | + |
| 45 | + FileReceiver.FileReceiverClient client = new Uploader.Client.FileReceiver.FileReceiverClient(); |
| 46 | + client.Endpoint.Address = new System.ServiceModel.EndpointAddress(Utility.BaseUrl + "Services/FileReceiver.svc"); |
| 47 | + client.BeginUploadCompleted += new EventHandler<Uploader.Client.FileReceiver.BeginUploadCompletedEventArgs>(client_BeginUploadCompleted); |
| 48 | + FileReceiver.ChunkUploadRequest req = new Uploader.Client.FileReceiver.ChunkUploadRequest(); |
| 49 | + using (FileStream fs = this.File.OpenRead()) |
| 50 | + { |
| 51 | + //get full hash first. |
| 52 | + this.FileLength = fs.Length; |
| 53 | + |
| 54 | + //setup progress bar. |
| 55 | + this.Steps = (int)(this.FileLength / (long)Utility.chunkSize); |
| 56 | + this.uxProgress.Minimum = 0; |
| 57 | + this.uxProgress.Maximum = this.Steps; |
| 58 | + |
| 59 | + int read = 0; |
| 60 | + byte[] buffer = null; |
| 61 | + |
| 62 | + if (fs.Length <= Utility.chunkSize) |
| 63 | + buffer = new byte[(int)fs.Length]; |
| 64 | + else |
| 65 | + buffer = new byte[Utility.chunkSize]; |
| 66 | + |
| 67 | + read = fs.Read(buffer, 0, Utility.chunkSize); |
| 68 | + |
| 69 | + this.FilePosition += read; |
| 70 | + |
| 71 | + req.Chunk = buffer; |
| 72 | + req.ChunkSize = buffer.Length; |
| 73 | + req.Hash = Utility.GetSHA256Hash(buffer); |
| 74 | + |
| 75 | + client.BeginUploadAsync(req); |
| 76 | + } |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + //show error. |
| 81 | + this.uxText.Text = ex.Message; |
| 82 | + } |
| 83 | + } |
| 84 | + void SendNextChunk(Guid token) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + FileReceiver.FileReceiverClient client = new Uploader.Client.FileReceiver.FileReceiverClient(); |
| 89 | + client.Endpoint.Address = new System.ServiceModel.EndpointAddress(Utility.BaseUrl + "Services/FileReceiver.svc"); |
| 90 | + client.ContinueUploadCompleted += new EventHandler<Uploader.Client.FileReceiver.ContinueUploadCompletedEventArgs>(client_ContinueUploadCompleted); |
| 91 | + |
| 92 | + FileReceiver.ChunkUploadRequest req = new Uploader.Client.FileReceiver.ChunkUploadRequest(); |
| 93 | + using (FileStream fs = this.File.OpenRead()) |
| 94 | + { |
| 95 | + int read = 0; |
| 96 | + byte[] buffer = null; |
| 97 | + int readSize = Utility.chunkSize; |
| 98 | + |
| 99 | + long diff = this.FileLength - this.FilePosition; |
| 100 | + if (diff < Utility.chunkSize) |
| 101 | + readSize = (int)diff; |
| 102 | + |
| 103 | + buffer = new byte[readSize]; |
| 104 | + |
| 105 | + fs.Seek(this.FilePosition, SeekOrigin.Begin); |
| 106 | + read = fs.Read(buffer, 0, readSize); |
| 107 | + |
| 108 | + this.FilePosition += read; |
| 109 | + |
| 110 | + req.ChunkSize = buffer.Length; |
| 111 | + req.Hash = Utility.GetSHA256Hash(buffer); |
| 112 | + req.Chunk = buffer; |
| 113 | + req.Token = token; |
| 114 | + |
| 115 | + client.ContinueUploadAsync(req); |
| 116 | + } |
| 117 | + } |
| 118 | + catch (Exception ex) |
| 119 | + { |
| 120 | + //show error. |
| 121 | + this.uxText.Text = ex.Message; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + void client_ContinueUploadCompleted(object sender, Uploader.Client.FileReceiver.ContinueUploadCompletedEventArgs e) |
| 126 | + { |
| 127 | + if (!e.Cancelled && e.Error == null) |
| 128 | + { |
| 129 | + if (e.Result.Status == Uploader.Client.FileReceiver.EnumsResponsStatus.Success) |
| 130 | + { |
| 131 | + UpdateProgress(); |
| 132 | + |
| 133 | + if (this.FilePosition < this.FileLength) |
| 134 | + SendNextChunk(e.Result.Token); |
| 135 | + else |
| 136 | + { |
| 137 | + //done.. |
| 138 | + FinishUpload(e.Result.Token); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + else if (e.Error != null) |
| 143 | + { |
| 144 | + this.uxText.Text = e.Error.Message; |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + this.uxText.Text = "Cancelled"; |
| 149 | + } |
| 150 | + } |
| 151 | + void client_BeginUploadCompleted(object sender, Uploader.Client.FileReceiver.BeginUploadCompletedEventArgs e) |
| 152 | + { |
| 153 | + if (!e.Cancelled && e.Error == null) |
| 154 | + { |
| 155 | + if (e.Result.Status == Uploader.Client.FileReceiver.EnumsResponsStatus.Success) |
| 156 | + { |
| 157 | + UpdateProgress(); |
| 158 | + |
| 159 | + if (this.FilePosition < this.FileLength) |
| 160 | + SendNextChunk(e.Result.Token); |
| 161 | + else |
| 162 | + { |
| 163 | + //finish upload. |
| 164 | + FinishUpload(e.Result.Token); |
| 165 | + } |
| 166 | + } |
| 167 | + else |
| 168 | + uxText.Text = e.Result.Message; |
| 169 | + } |
| 170 | + else if (e.Error != null) |
| 171 | + { |
| 172 | + this.uxText.Text = e.Error.Message; |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + this.uxText.Text = "Cancelled"; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + void FinishUpload(Guid token) |
| 181 | + { |
| 182 | + FileReceiver.FileReceiverClient client = new Uploader.Client.FileReceiver.FileReceiverClient(); |
| 183 | + client.Endpoint.Address = new System.ServiceModel.EndpointAddress(Utility.BaseUrl + "Services/FileReceiver.svc"); |
| 184 | + client.FinishUploadCompleted += new EventHandler<Uploader.Client.FileReceiver.FinishUploadCompletedEventArgs>(client_FinishUploadCompleted); |
| 185 | + |
| 186 | + FileReceiver.FinishRequest req = new Uploader.Client.FileReceiver.FinishRequest(); |
| 187 | + using (FileStream fs = this.File.OpenRead()) |
| 188 | + { |
| 189 | + req.Extension = this.File.Extension; |
| 190 | + req.FullHash = Utility.GetSHA256Hash(fs); |
| 191 | + } |
| 192 | + req.Token = token; |
| 193 | + |
| 194 | + client.FinishUploadAsync(req); |
| 195 | + } |
| 196 | + |
| 197 | + void UpdateProgress() |
| 198 | + { |
| 199 | + this.CurrentStep++; |
| 200 | + this.uxProgress.Value = this.CurrentStep; |
| 201 | + } |
| 202 | + |
| 203 | + void client_FinishUploadCompleted(object sender, Uploader.Client.FileReceiver.FinishUploadCompletedEventArgs e) |
| 204 | + { |
| 205 | + if (!e.Cancelled && e.Error == null) |
| 206 | + { |
| 207 | + if (e.Result.Status == Uploader.Client.FileReceiver.EnumsResponsStatus.Success) |
| 208 | + { |
| 209 | + uxText.Text = "Transferred successfully."; |
| 210 | + } |
| 211 | + else |
| 212 | + uxText.Text = e.Result.Message; |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + if (e.Error != null) |
| 217 | + uxText.Text = e.Error.Message; |
| 218 | + else |
| 219 | + uxText.Text = "Cancelled"; |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments