Skip to content

Commit 4a9bd6f

Browse files
author
Nathan Bridgewater
committed
added the silverlight uploader. I need to refresh my memory...
1 parent 2f62050 commit 4a9bd6f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+4816
-0
lines changed

Diff for: UploaderSL/LICENSE.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Copyright (c) 2009, Nathan Bridgewater
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5+
6+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
* Neither the name of the Integrated Web Systems, LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9+
10+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Diff for: UploaderSL/src/Uploader.Client/App.xaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
x:Class="Uploader.Client.App"
4+
>
5+
<Application.Resources>
6+
7+
</Application.Resources>
8+
</Application>

Diff for: UploaderSL/src/Uploader.Client/App.xaml.cs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
13+
namespace Uploader.Client
14+
{
15+
public partial class App : Application
16+
{
17+
18+
public App()
19+
{
20+
this.Startup += this.Application_Startup;
21+
this.Exit += this.Application_Exit;
22+
this.UnhandledException += this.Application_UnhandledException;
23+
24+
InitializeComponent();
25+
}
26+
27+
private void Application_Startup(object sender, StartupEventArgs e)
28+
{
29+
this.RootVisual = new Page();
30+
}
31+
32+
private void Application_Exit(object sender, EventArgs e)
33+
{
34+
35+
}
36+
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
37+
{
38+
// If the app is running outside of the debugger then report the exception using
39+
// the browser's exception mechanism. On IE this will display it a yellow alert
40+
// icon in the status bar and Firefox will display a script error.
41+
if (!System.Diagnostics.Debugger.IsAttached)
42+
{
43+
44+
// NOTE: This will allow the application to continue running after an exception has been thrown
45+
// but not handled.
46+
// For production applications this error handling should be replaced with something that will
47+
// report the error to the website and stop the application.
48+
e.Handled = true;
49+
Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
50+
}
51+
}
52+
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
53+
{
54+
try
55+
{
56+
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
57+
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
58+
59+
System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");
60+
}
61+
catch (Exception)
62+
{
63+
}
64+
}
65+
}
66+
}

Diff for: UploaderSL/src/Uploader.Client/Items.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Net;
3+
using System.Windows;
4+
using System.Windows.Controls;
5+
using System.Windows.Documents;
6+
using System.Windows.Ink;
7+
using System.Windows.Input;
8+
using System.Windows.Media;
9+
using System.Windows.Media.Animation;
10+
using System.Windows.Shapes;
11+
12+
namespace Uploader.Client
13+
{
14+
public class Items :
15+
System.Collections.ObjectModel.ObservableCollection<string>
16+
{
17+
public Items()
18+
{
19+
Add("Item 1");
20+
Add("Item 2");
21+
Add("Item 3");
22+
Add("Item 4");
23+
Add("Item 5");
24+
}
25+
}
26+
27+
}

Diff for: UploaderSL/src/Uploader.Client/ListItem.xaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<UserControl x:Class="Uploader.Client.ListItem"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
Width="390" Height="60">
5+
<Grid x:Name="LayoutRoot" Background="White">
6+
<Border BorderBrush="Black" BorderThickness="0,0,0,0" Margin="5,5,5,24">
7+
<TextBlock x:Name="uxText"></TextBlock>
8+
</Border>
9+
<ProgressBar Margin="5,5,5,5" Height="15" VerticalAlignment="Bottom" x:Name="uxProgress"/>
10+
</Grid>
11+
</UserControl>

Diff for: UploaderSL/src/Uploader.Client/ListItem.xaml.cs

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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+
}

Diff for: UploaderSL/src/Uploader.Client/Page.xaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<UserControl x:Class="Uploader.Client.Page"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:src="clr-namespace:Uploader.Client"
5+
Width="400" Height="600" Loaded="Page_Loaded">
6+
<Grid x:Name="LayoutRoot" Background="White">
7+
<Button Name="uxSend" Content="Send" Width="50" Height="25" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="5,5,5,5" Click="uxSend_Click"/>
8+
<Button Name="uxBrowse" Content="Browse..." Width="70" Height="25" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="5,5,70,5" Click="uxBrowse_Click"/>
9+
<ListBox Name="uxFiles" Margin="0,35,0,0"/>
10+
</Grid>
11+
</UserControl>

0 commit comments

Comments
 (0)