~cszikszoy/do-plugins/fix-confluence

« back to all changes in this revision

Viewing changes to ImageShack/src/ImageShackAction.cs

  • Committer: Jason Jones
  • Date: 2008-08-23 08:53:24 UTC
  • mto: This revision was merged to the branch mainline in revision 247.
  • Revision ID: jasonedwardjones@gmail.com-20080823085324-frisaleem8e6untk
Added ImageShack plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  ImageShackAction.cs
 
2
//
 
3
//  GNOME Do is the legal property of its developers, whose names are too
 
4
//  numerous to list here.  Please refer to the COPYRIGHT file distributed with
 
5
//  this source distribution.
 
6
//
 
7
//  This program is free software: you can redistribute it and/or modify it
 
8
//  under the terms of the GNU General Public License as published by the Free
 
9
//  Software Foundation, either version 3 of the License, or (at your option)
 
10
//  any later version.
 
11
//
 
12
//  This program is distributed in the hope that it will be useful, but WITHOUT
 
13
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
14
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
15
//  more details.
 
16
//
 
17
//  You should have received a copy of the GNU General Public License along with
 
18
//  this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
using System;
 
21
using System.IO;
 
22
using System.Net;
 
23
using System.Text;
 
24
using System.Text.RegularExpressions;
 
25
using System.Web;
 
26
 
 
27
using Do.Addins;
 
28
using Do.Universe;
 
29
 
 
30
using Mono.Unix;
 
31
 
 
32
namespace ImageShack
 
33
{
 
34
        public class ImageShackAction : AbstractAction
 
35
        {
 
36
                public override string Name
 
37
                {
 
38
                        get { return Catalog.GetString ("Upload to ImageShack"); }
 
39
                }
 
40
                
 
41
                public override string Description
 
42
                {
 
43
                        get { return Catalog.GetString ("Uploads the image to ImageShack."); }
 
44
                }
 
45
                
 
46
                public override string Icon 
 
47
                {
 
48
                        get { return "imageshack.png@" + GetType ().Assembly.FullName; }
 
49
                }
 
50
                
 
51
                public override Type[] SupportedItemTypes
 
52
                {
 
53
                        get {
 
54
                                return new Type[] {
 
55
                                        typeof (IFileItem)
 
56
                                };
 
57
                        }
 
58
                }
 
59
                        
 
60
                public override bool SupportsItem (IItem item)
 
61
                {       
 
62
                        if (item is FileItem) { 
 
63
                                if ((item as FileItem).MimeType.StartsWith ("image/")) {
 
64
                                        return true;
 
65
                                }
 
66
                        }
 
67
                        return false;
 
68
                }
 
69
                                
 
70
                public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
 
71
                {                                                               
 
72
                        try {
 
73
                                string url = PostToImageShack ((items[0] as FileItem).Path, (items[0] as FileItem).MimeType);
 
74
                                return new IItem[] { new TextItem (url) };
 
75
                        }
 
76
                        catch (Exception e) {
 
77
                                Console.Error.WriteLine (e.Message);
 
78
                                return new IItem[] { new TextItem ("An error occured while uploading to ImageShack.") };
 
79
                        }                                       
 
80
                }               
 
81
                        
 
82
                public static string PostToImageShack (string file, string contentType)
 
83
                {
 
84
                        string boundary = "----------" + DateTime.Now.Ticks.ToString ("x");
 
85
                        HttpWebRequest request = (HttpWebRequest) WebRequest.Create ("http://www.imageshack.us/index.php");
 
86
                        request.Method = "POST";
 
87
                        request.ContentType = "multipart/form-data ; boundary=" + boundary;
 
88
 
 
89
            StringBuilder sb = new StringBuilder ();
 
90
            sb.Append ("--");
 
91
            sb.Append (boundary);
 
92
            sb.Append ("\r\n");
 
93
            sb.Append ("Content-Disposition: form-data; name=\"");
 
94
            sb.Append ("fileupload");
 
95
            sb.Append ("\"; filename=\"");
 
96
            sb.Append (Path.GetFileName(file));
 
97
            sb.Append ("\"");
 
98
            sb.Append ("\r\n");
 
99
            sb.Append ("Content-Type: ");
 
100
            sb.Append (contentType);
 
101
            sb.Append ("\r\n");
 
102
            sb.Append ("\r\n");
 
103
 
 
104
            string header = sb.ToString ();
 
105
            byte[] headerBytes = Encoding.UTF8.GetBytes (header);
 
106
            byte[] boundaryBytes = Encoding.ASCII.GetBytes ("\r\n--" + boundary + "\r\n");
 
107
            using (FileStream fileStream = new FileStream (file, FileMode.Open, FileAccess.Read)) {
 
108
                    long length = headerBytes.Length + fileStream.Length + boundaryBytes.Length;
 
109
                    request.ContentLength = length;                             
 
110
                                        
 
111
                                using (Stream requestStream = request.GetRequestStream ()) {
 
112
                                        requestStream.Write (headerBytes, 0, headerBytes.Length);
 
113
                                        byte[] buffer = new Byte[checked((uint)Math.Min (4096, (int)fileStream.Length))];
 
114
                                        int bytesRead = 0;
 
115
                                        while ((bytesRead = fileStream.Read (buffer, 0, buffer.Length)) != 0) {
 
116
                                                requestStream.Write (buffer, 0, bytesRead);
 
117
                                        }
 
118
                                        requestStream.Write (boundaryBytes, 0, boundaryBytes.Length);
 
119
                                }
 
120
                        }
 
121
                        
 
122
                    string responseText = "";
 
123
                        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse ()) {                    
 
124
                                using (Stream responseStream = response.GetResponseStream ()) {
 
125
                                        using (StreamReader reader = new StreamReader (responseStream)) {
 
126
                                                responseText = reader.ReadToEnd ();
 
127
                                        }
 
128
                                }
 
129
                        }
 
130
                        
 
131
                        Regex directUrlPattern = new Regex ("<input type=\"text\" onClick=\"track\\('direct'\\).*? value=\"(.*?)\"/>");
 
132
                        Match directUrl = directUrlPattern.Match (responseText);
 
133
                                        
 
134
                        return directUrl.Groups[1].Value;
 
135
                }
 
136
        }
 
137
}
 
138
 
 
139