~ubuntu-branches/debian/sid/wordpress/sid

« back to all changes in this revision

Viewing changes to debian/missing-sources/plupload-1.5.4/csharp/Plupload/FileReference.cs

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2013-09-04 23:18:58 UTC
  • mfrom: (1.2.28)
  • Revision ID: package-import@ubuntu.com-20130904231858-nljmn1buzswh63jk
Tags: 3.6+dfsg-1
* New upstream release.
* Improve wp-settings to verify that $_SERVER['HTTP_X_FORWARDED_PROTO']
  exists before accessing it (avoids a PHP notice).
  Thanks to Paul Dreik <slask@pauldreik.se> for the report and the patch.
* Document in README.Debian the need to login to /wp-admin/ to complete
  an upgrade.
* Drop useless debian/README.source
* Drop 008CVE2008-2392.patch since upstream now disables unfiltered
  uploads by default. See http://core.trac.wordpress.org/ticket/10692
* Drop 009CVE2008-6767.patch since the backto parameter is validated
  against a whitelist, and externally triggered upgrades are not a
  security problem as long as they work.
* Update debian/missing-sources with latest versions.
* Update upstream l10n.

Show diffs side-by-side

added added

removed removed

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