1 //-----------------------------------------------------------------------
2 // <copyright file="UpdateProfileImageCommand.cs" company="Patrick 'Ricky' Smith">
3 //  This file is part of the Twitterizer library (http://www.twitterizer.net)
4 //
5 //  Copyright (c) 2010, Patrick "Ricky" Smith (ricky@digitally-born.com)
6 //  All rights reserved.
7 //
8 //  Redistribution and use in source and binary forms, with or without modification, are
9 //  permitted provided that the following conditions are met:
10 //
11 //  - Redistributions of source code must retain the above copyright notice, this list
12 //    of conditions and the following disclaimer.
13 //  - Redistributions in binary form must reproduce the above copyright notice, this list
14 //    of conditions and the following disclaimer in the documentation and/or other
15 //    materials provided with the distribution.
16 //  - Neither the name of the Twitterizer nor the names of its contributors may be
17 //    used to endorse or promote products derived from this software without specific
18 //    prior written permission.
19 //
20 //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 //  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 //  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 //  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 //  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 //  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 //  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 //  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 //  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 //  POSSIBILITY OF SUCH DAMAGE.
30 // </copyright>
31 // <author>Ricky Smith</author>
32 // <summary>The update profile image command class.</summary>
33 //-----------------------------------------------------------------------
34 
35 namespace Twitterizer.Commands
36 {
37     using System;
38     using Twitterizer.Core;
39 
40 #if !SILVERLIGHT
41     [Serializable]
42 #endif
43     internal class UpdateProfileImageCommand : TwitterCommand<TwitterUser>
44     {
45         private readonly byte[] imageData;
46 
47         /// <summary>
48         /// Initializes a new instance of the <see cref="UpdateProfileImageCommand"/> class.
49         /// </summary>
50         /// <param name="tokens">The tokens.</param>
51         /// <param name="image">The image.</param>
52         /// <param name="options">The options.</param>
UpdateProfileImageCommand(OAuthTokens tokens, byte[] image, OptionalProperties options)53         public UpdateProfileImageCommand(OAuthTokens tokens, byte[] image, OptionalProperties options)
54             : base(HTTPVerb.POST, "account/update_profile_image.json", tokens, options)
55         {
56             if (tokens == null)
57             {
58                 throw new ArgumentNullException("tokens");
59             }
60 
61             if (image == null || image.Length == 0)
62             {
63                 throw new ArithmeticException("Image data cannot be null or zero length.");
64             }
65 
66             if (image.Length > 716800)
67             {
68                 throw new ArithmeticException("Image cannot exceed 700Kb in size.");
69             }
70 
71             this.imageData = image;
72             this.Multipart = true;
73         }
74 
75         /// <summary>
76         /// Initializes the command.
77         /// </summary>
Init()78         public override void Init()
79         {
80             this.RequestParameters.Add("image", this.imageData);
81             this.RequestParameters.Add("include_entities", "true");
82         }
83     }
84 }
85