1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
|
/**
* FileReference.cs
*
* Copyright 2009, Moxiecode Systems AB
* Released under GPL License.
*
* License: http://www.plupload.com/license
* Contributing: http://www.plupload.com/contributing
*/
using System;
using System.IO;
using System.Threading;
using System.Windows.Threading;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Browser;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using FluxJpeg.Core.Encoder;
using FluxJpeg.Core;
using Plupload.PngEncoder;
namespace Moxiecode.Plupload {
enum ImageType {
Jpeg,
Png
}
/// <summary>
/// Description of File.
/// </summary>
public class FileReference {
#region private fields
private string name, uploadUrl, id, targetName, mimeType;
private FileInfo info;
private SynchronizationContext syncContext;
private int chunks, chunkSize;
private bool multipart, chunking;
private long size, chunk;
private string fileDataName;
private Dictionary<string, object> multipartParams;
private Dictionary<string, object> headers;
private Stream fileStream;
private Stream imageStream;
private HttpWebRequest req;
#endregion
/// <summary>Upload complete delegate.</summary>
public delegate void UploadCompleteHandler(object sender, UploadEventArgs e);
/// <summary>Upload chunk compleate delegate.</summary>
public delegate void UploadChunkCompleteHandler(object sender, UploadEventArgs e);
/// <summary>Upload error delegate.</summary>
public delegate void ErrorHandler(object sender, ErrorEventArgs e);
/// <summary>Upload progress delegate.</summary>
public delegate void ProgressHandler(object sender, ProgressEventArgs e);
/// <summary>Upload complete event</summary>
public event UploadCompleteHandler UploadComplete;
/// <summary>Upload chunk complete event</summary>
public event UploadChunkCompleteHandler UploadChunkComplete;
/// <summary>Error event</summary>
public event ErrorHandler Error;
/// <summary>Progress event</summary>
public event ProgressHandler Progress;
/// <summary>
/// Main constructor for the file reference.
/// </summary>
/// <param name="id">Unique file id for item.</param>
/// <param name="info">FileInfo that got returned from a file selection.</param>
public FileReference(string id, FileInfo info) {
this.id = id;
this.name = info.Name;
this.info = info;
this.size = info.Length;
}
/// <summary>Unique id for the file reference.</summary>
public string Id {
get { return id; }
}
/// <summary>File name to use with upload.</summary>
public string Name {
get { return name; }
set { name = value; }
}
/// <summary>File size for the selected file.</summary>
public long Size {
get { return this.size; }
}
/// <summary>
/// Uploads the file to the specific url and using the specified chunk_size.
/// </summary>
/// <param name="upload_url">URL to upload to.</param>
/// <param name="chunk_size">Chunk size to use.</param>
/// <param name="image_width">Image width to scale to.</param>
/// <param name="image_height">Image height to scale to.</param>
/// <param name="image_quality">Image quality to store as.</param>
public void Upload(string upload_url, string json_settings) {
int chunkSize = 0, imageWidth = 0, imageHeight = 0, imageQuality = 90;
Dictionary<string, object> settings = (Dictionary<string, object>) Moxiecode.Plupload.Utils.JsonReader.ParseJson(json_settings);
chunkSize = Convert.ToInt32(settings["chunk_size"]);
imageWidth = Convert.ToInt32(settings["image_width"]);
imageHeight = Convert.ToInt32(settings["image_height"]);
imageQuality = Convert.ToInt32(settings["image_quality"]);
this.fileDataName = (string)settings["file_data_name"];
this.multipart = Convert.ToBoolean(settings["multipart"]);
this.multipartParams = (Dictionary<string, object>)settings["multipart_params"];
this.headers = (Dictionary<string, object>)settings["headers"];
this.targetName = (string) settings["name"];
this.mimeType = (string) settings["mime"];
this.chunk = 0;
this.chunking = chunkSize > 0;
this.uploadUrl = upload_url;
try {
// Is jpeg and image size is defined
if (Regex.IsMatch(this.name, @"\.(jpeg|jpg|png)$", RegexOptions.IgnoreCase) && (imageWidth != 0 || imageHeight != 0 || imageQuality != 0))
{
if (Regex.IsMatch(this.name, @"\.png$"))
this.imageStream = this.ResizeImage(this.info.OpenRead(), imageWidth, imageHeight, imageQuality, ImageType.Png);
else
this.imageStream = this.ResizeImage(this.info.OpenRead(), imageWidth, imageHeight, imageQuality, ImageType.Jpeg);
this.imageStream.Seek(0, SeekOrigin.Begin);
this.size = this.imageStream.Length;
}
} catch (Exception ex) {
syncContext.Send(delegate {
this.OnIOError(new ErrorEventArgs(ex.Message, 0, this.chunks));
}, this);
}
if (this.chunking) {
this.chunkSize = chunkSize;
this.chunks = (int) Math.Ceiling((double) this.Size / (double) chunkSize);
} else {
this.chunkSize = (int) this.Size;
this.chunks = 1;
}
this.UploadNextChunk();
}
private int ReadByteRange(byte[] buffer, long position, int offset, int count) {
int bytes = -1;
// Read from image memory stream if it's defined
if (this.imageStream != null) {
this.imageStream.Seek(position, SeekOrigin.Begin);
return this.imageStream.Read(buffer, offset, count);
}
// Open the file and read the specified part of it
if (this.fileStream == null) {
this.fileStream = this.info.OpenRead();
}
bytes = this.fileStream.Read(buffer, offset, count);
return bytes;
}
/// <summary>
/// Uploads the next chunk if there are more in queue.
/// </summary>
/// <returns>True/false if there are more chunks to be uploaded.</returns>
public bool UploadNextChunk() {
string url = this.uploadUrl;
// Is there more chunks
if (this.chunk >= this.chunks)
return false;
this.syncContext = SynchronizationContext.Current;
// Add name, chunk and chunks to query string when we don't use multipart
if (!this.multipart) {
if (url.IndexOf('?') == -1) {
url += '?';
} else {
url += '&';
}
url += "name=" + Uri.EscapeDataString(this.targetName);
if (this.chunking) {
url += "&chunk=" + this.chunk;
url += "&chunks=" + this.chunks;
}
}
this.req = WebRequest.Create(new Uri(HtmlPage.Document.DocumentUri, url)) as HttpWebRequest;
this.req.Method = "POST";
// Add custom headers
if (this.headers != null) {
foreach (string key in this.headers.Keys) {
if (this.headers[key] == null)
continue;
switch (key.ToLower())
{
// in silverlight 3, these are set by the web browser that hosts the Silverlight application.
// http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.95%29.aspx
case "connection":
case "content-length":
case "expect":
case "if-modified-since":
case "referer":
case "transfer-encoding":
case "user-agent":
break;
// in silverlight this isn't supported, can not find reference to why not
case "range":
break;
// in .NET Framework 3.5 and below, these are set by the system.
// http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=VS.90%29.aspx
case "date":
case "host":
break;
case "accept":
this.req.Accept = (string)this.headers[key];
break;
case "content-type":
this.req.ContentType = (string)this.headers[key];
break;
default:
this.req.Headers[key] = (string)this.headers[key];
break;
}
}
}
IAsyncResult asyncResult = this.req.BeginGetRequestStream(new AsyncCallback(RequestStreamCallback), this.req);
return true;
}
/// <summary>
/// Cancels uploading the current file.
/// </summary>
public void CancelUpload() {
if (this.req != null) {
this.req.Abort();
this.req = null;
DisposeStreams();
}
}
#region protected methods
protected virtual void OnUploadComplete(UploadEventArgs e) {
DisposeStreams();
if (UploadComplete != null)
UploadComplete(this, e);
}
protected virtual void OnUploadChunkComplete(UploadEventArgs e) {
if (UploadChunkComplete != null)
UploadChunkComplete(this, e);
}
protected virtual void OnIOError(ErrorEventArgs e) {
DisposeStreams();
if (Error != null)
Error(this, e);
}
protected virtual void OnProgress(ProgressEventArgs e) {
if (Progress != null)
Progress(this, e);
}
#endregion
#region private methods
private void RequestStreamCallback(IAsyncResult ar) {
HttpWebRequest request = (HttpWebRequest) ar.AsyncState;
string boundary = "----pluploadboundary" + DateTime.Now.Ticks, dashdash = "--", crlf = "\r\n";
Stream requestStream = null;
byte[] buffer = new byte[1048576], strBuff;
int bytes;
long loaded = 0, end = 0;
int percent, lastPercent = 0;
try {
requestStream = request.EndGetRequestStream(ar);
if (this.multipart) {
request.ContentType = "multipart/form-data; boundary=" + boundary;
// Add name to multipart array
this.multipartParams["name"] = this.targetName;
// Add chunking when needed
if (this.chunking) {
this.multipartParams["chunk"] = this.chunk;
this.multipartParams["chunks"] = this.chunks;
}
// Append mutlipart parameters
foreach (KeyValuePair<string, object> pair in this.multipartParams) {
strBuff = this.StrToByteArray(dashdash + boundary + crlf +
"Content-Disposition: form-data; name=\"" + pair.Key + '"' + crlf + crlf +
pair.Value + crlf
);
requestStream.Write(strBuff, 0, strBuff.Length);
}
// Append multipart file header
strBuff = this.StrToByteArray(
dashdash + boundary + crlf +
"Content-Disposition: form-data; name=\"" + this.fileDataName + "\"; filename=\"" + this.name + '"' +
crlf + "Content-Type: " + this.mimeType + crlf + crlf
);
requestStream.Write(strBuff, 0, strBuff.Length);
} else {
request.ContentType = "application/octet-stream";
}
// Move to start
loaded = this.chunk * this.chunkSize;
// Find end
end = (this.chunk + 1) * this.chunkSize;
if (end > this.Size)
end = this.Size;
while (loaded < end && (bytes = ReadByteRange(buffer, loaded, 0, (int)(end - loaded < buffer.Length ? end - loaded : buffer.Length))) != 0) {
loaded += bytes;
percent = (int) Math.Round((double) loaded / (double) this.Size * 100.0);
if (percent > lastPercent) {
syncContext.Post(delegate {
if (percent > lastPercent) {
this.OnProgress(new ProgressEventArgs(loaded, this.Size));
lastPercent = percent;
}
}, this);
}
requestStream.Write(buffer, 0, bytes);
requestStream.Flush();
}
// Append multipart file footer
if (this.multipart) {
strBuff = this.StrToByteArray(crlf + dashdash + boundary + dashdash + crlf);
requestStream.Write(strBuff, 0, strBuff.Length);
}
} catch (Exception ex) {
syncContext.Send(delegate {
this.OnIOError(new ErrorEventArgs(ex.Message, this.chunk, this.chunks));
}, this);
} finally {
try {
if (requestStream != null) {
requestStream.Close();
requestStream.Dispose();
requestStream = null;
}
} catch (Exception ex) {
syncContext.Send(delegate {
this.OnIOError(new ErrorEventArgs(ex.Message, this.chunk, this.chunks));
}, this);
}
}
try {
request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
}
catch (WebException ex)
{
if (ex.Status != WebExceptionStatus.RequestCanceled) {
syncContext.Send(delegate {
this.OnIOError(new ErrorEventArgs(ex.Message, this.chunk, this.chunks));
}, this);
}
}
catch (Exception ex) {
syncContext.Send(delegate {
this.OnIOError(new ErrorEventArgs(ex.Message, this.chunk, this.chunks));
}, this);
}
}
private void ResponseCallback(IAsyncResult ar) {
try
{
HttpWebRequest request = ar.AsyncState as HttpWebRequest;
WebResponse response = request.EndGetResponse(ar);
syncContext.Post(ExtractResponse, response);
}
catch (WebException ex) {
if (ex.Status != WebExceptionStatus.RequestCanceled) {
syncContext.Send(delegate {
this.OnIOError(new ErrorEventArgs(ex.Message, this.chunk, this.chunks));
}, this);
}
}
catch (Exception ex) {
syncContext.Send(delegate {
this.OnIOError(new ErrorEventArgs(ex.Message, this.chunk, this.chunks));
}, this);
}
}
private void ExtractResponse(object state) {
HttpWebResponse response = state as HttpWebResponse;
StreamReader respReader = null;
Stream respStream = null;
string content;
try {
respStream = response.GetResponseStream();
if (response.StatusCode == HttpStatusCode.OK) {
respReader = new StreamReader(respStream);
if (respStream != null) {
content = respReader.ReadToEnd();
} else
throw new Exception("Error could not open response stream.");
} else
throw new Exception("Error server returned status: " + ((int) response.StatusCode) + " " + response.StatusDescription);
this.chunk++;
syncContext.Send(delegate {
this.OnUploadChunkComplete(new UploadEventArgs(content, this.chunk - 1, this.chunks));
}, this);
} catch (Exception ex) {
syncContext.Send(delegate {
this.OnIOError(new ErrorEventArgs(ex.Message, chunk, chunks));
}, this);
} finally {
if (respStream != null)
respStream.Close();
if (respReader != null)
respReader.Close();
response.Close();
}
}
private void DisposeStreams() {
if (fileStream != null) {
fileStream.Dispose();
fileStream = null;
}
if (imageStream != null) {
imageStream.Dispose();
imageStream = null;
}
}
private void Debug(string msg) {
((ScriptObject) HtmlPage.Window.Eval("console")).Invoke("log", new string[] {msg});
}
private Stream ResizeImage(Stream image_stream, int width, int height, int quality, ImageType type) {
try {
// Load the image as a writeablebitmap
WriteableBitmap writableBitmap;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(image_stream);
writableBitmap = new WriteableBitmap(bitmapImage);
if (width == 0) {
width = writableBitmap.PixelWidth;
}
if (height == 0) {
height = writableBitmap.PixelHeight;
}
double scale = Math.Min((double) width / writableBitmap.PixelWidth, (double) height / writableBitmap.PixelHeight);
// No resize needed
if (scale >= 1.0 && (quality == 0 || type != ImageType.Jpeg))
return image_stream;
if (quality == 0) {
quality = 90;
}
// Setup shorter names and pixelbuffers
int w = writableBitmap.PixelWidth;
int h = writableBitmap.PixelHeight;
int[] p = writableBitmap.Pixels;
byte[][,] imageRaster = new byte[3][,]; // RGB colors
imageRaster[0] = new byte[w, h];
imageRaster[1] = new byte[w, h];
imageRaster[2] = new byte[w, h];
// Copy WriteableBitmap data into buffer for FluxJpeg
int i = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int color = p[i++];
imageRaster[0][x, y] = (byte) (color >> 16); // R
imageRaster[1][x, y] = (byte) (color >> 8); // G
imageRaster[2][x, y] = (byte) (color); // B
}
}
// Create new FluxJpeg image based on pixel data
Image jpegImage = new Image(new ColorModel {
colorspace = ColorSpace.RGB
}, imageRaster);
ImageResizer resizer = new ImageResizer(jpegImage);
Image resizedImage;
if (scale < 1.0) {
// Calc new proportional size
width = (int) Math.Round(writableBitmap.PixelWidth * scale);
height = (int) Math.Round(writableBitmap.PixelHeight * scale);
// Resize the image
resizedImage = resizer.Resize(width, height, FluxJpeg.Core.Filtering.ResamplingFilters.LowpassAntiAlias);
} else {
resizedImage = jpegImage;
}
Stream imageStream = new MemoryStream();
if (type == ImageType.Jpeg) {
// Encode the resized image as Jpeg
JpegEncoder jpegEncoder = new JpegEncoder(resizedImage, quality, imageStream);
jpegEncoder.Encode();
} else {
int[] pixelBuffer = new int[resizedImage.Height * resizedImage.Width];
byte[][,] resizedRaster = resizedImage.Raster;
// Convert FJCore raster to PixelBuffer
for (int y = 0; y < resizedImage.Height; y++) {
for (int x = 0; x < resizedImage.Width; x++) {
int color = 0;
color = color | resizedRaster[0][x, y] << 16; // R
color = color | resizedRaster[1][x, y] << 8; // G
color = color | resizedRaster[2][x, y]; // B
pixelBuffer[(y * resizedImage.Width) + x] = color;
}
}
// Encode the resized image as Png
PngEncoder pngEncoder = new PngEncoder(pixelBuffer, resizedImage.Width, resizedImage.Height, false, PngEncoder.FILTER_NONE, Deflater.BEST_COMPRESSION);
byte[] pngBuffer = pngEncoder.pngEncode();
imageStream.Write(pngBuffer, 0, pngBuffer.Length);
}
return imageStream;
} catch {
// Ignore the error and let the server resize the image
}
return image_stream;
}
private byte[] StrToByteArray(string str) {
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
#endregion
}
/// <summary>
/// Upload event arguments class.
/// </summary>
public class UploadEventArgs : EventArgs {
#region private fields
private string response;
private long chunk;
private int chunks;
#endregion
/// <summary>
/// Main constructor for the upload event.
/// </summary>
/// <param name="response">Response contents as a string.</param>
public UploadEventArgs(string response) : this(response, 0, 0) {
}
/// <summary>
/// Main constructor for the upload event.
/// </summary>
/// <param name="response">Response contents as a string.</param>
/// <param name="chunk">Current chunk number.</param>
/// <param name="chunks">Total chunks.</param>
public UploadEventArgs(string response, long chunk, int chunks) {
this.response = response;
this.chunk = chunk;
this.chunks = chunks;
}
/// <summary>Response from upload request.</summary>
public string Response {
get { return response; }
}
/// <summary>Chunk number.</summary>
public long Chunk {
get { return chunk; }
}
/// <summary>Total number of chunks.</summary>
public int Chunks {
get { return chunks; }
}
}
/// <summary>
/// Error event arguments class.
/// </summary>
public class ErrorEventArgs : EventArgs {
#region private fields
private string message;
private long chunk;
private int chunks;
#endregion
/// <summary>
/// Main constructor for the error event.
/// </summary>
/// <param name="message">Error message.</param>
public ErrorEventArgs(string message) : this(message, 0, 0) {
this.message = message;
}
/// <summary>
/// Main constructor for the error event.
/// </summary>
/// <param name="message">Error message.</param>
/// <param name="chunk">Current chunk number.</param>
/// <param name="chunks">Total chunks.</param>
public ErrorEventArgs(string message, long chunk, int chunks) {
this.message = message;
this.chunk = chunk;
this.chunks = chunks;
}
/// <summary>Chunk number.</summary>
public long Chunk {
get { return chunk; }
}
/// <summary>Total number of chunks.</summary>
public int Chunks {
get { return chunks; }
}
/// <summary>Error message.</summary>
public string Message {
get { return message; }
}
}
/// <summary>
/// Progress event arguments class.
/// </summary>
public class ProgressEventArgs : EventArgs {
#region private fields
private long loaded, total;
#endregion
/// <summary>
/// Main constructor for the progress events args.
/// </summary>
/// <param name="loaded">Number of bytes uploaded.</param>
/// <param name="total">Total bytes to upload.</param>
public ProgressEventArgs(long loaded, long total) {
this.loaded = loaded;
this.total = total;
}
/// <summary>Total bytes to upload.</summary>
public long Total {
get { return total; }
}
/// <summary>Number of bytes upload so far.</summary>
public long Loaded {
get { return loaded; }
}
}
}
|