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
|
/*
* Copyright (C) 2008 Martin Schaaf <mascha AT ma-scha DOT de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
using FSpot;
using FSpot.Filters;
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Specialized;
namespace DigiImagesExporter
{
public class MultipartUploader {
public static void Authenticate(string url, NameValueCollection queryString, CookieContainer cookies) {
string postdata = "?";
foreach(string key in queryString.Keys) {
postdata += key + "=" + queryString.Get(key) + "&";
}
Uri uri = new Uri(url + postdata);
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.CookieContainer = cookies;
webrequest.Method = "POST";
webrequest.UserAgent = "F-Spot";
WebResponse r = webrequest.GetResponse();
r.Close();
}
public static void GetCall(string url, NameValueCollection queryString, CookieContainer cookies) {
string postdata = "?";
foreach(string key in queryString.Keys) {
postdata += key + "=" + queryString.Get(key) + "&";
}
Uri uri = new Uri(url + postdata);
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.CookieContainer = cookies;
webrequest.Method = "GET";
webrequest.UserAgent = "F-Spot";
WebResponse r = webrequest.GetResponse();
r.Close();
}
public static HttpWebResponse FileUploader(IBrowsableItem photo, String mimeType, string url, NameValueCollection querystring,
CookieContainer cookies, uint maximumSize) {
Uri uri = new Uri(url);
HttpWebRequest webrequest = (HttpWebRequest) WebRequest.Create(uri);
webrequest.CookieContainer = cookies;
string boundary = DateTime.Now.Ticks.ToString("x");
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
webrequest.UserAgent = "F-Spot";
StringBuilder sb = new StringBuilder();
foreach(string key in querystring.Keys) {
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\nContent-Disposition: form-data; name=\"");
sb.Append(key);
sb.Append("\"\r\n\r\n");
sb.Append(querystring.Get(key));
sb.Append("\r\n\r\n");
}
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\nContent-Disposition: form-data; name=\"picture\"; filename=\"");
sb.Append(Path.GetFileName(photo.DefaultVersionUri.LocalPath));
sb.Append("\"\r\n");
sb.Append("Content-Type: ");
sb.Append(mimeType);
sb.Append("\r\n\r\n");
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString());
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n\r\n--" + boundary + "--\r\n");
FilterSet filterSet = new FilterSet();
filterSet.Add(new OrientationFilter());
if (maximumSize > 0) {
filterSet.Add(new ResizeFilter(maximumSize));
}
filterSet.Add(new JpegFilter());
FilterRequest request = new FilterRequest(photo.DefaultVersionUri);
filterSet.Convert(request);
FileStream fileStream = new FileStream(request.Current.LocalPath, FileMode.Open, FileAccess.Read);
webrequest.ContentLength = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 ) {
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
return (HttpWebResponse) webrequest.GetResponse();
}
}
}
|