mirror of
https://github.com/greenshot/greenshot
synced 2025-08-14 02:37:03 -07:00
cleanup & small fixes. Added some more IBinaryContainer implementations which are used by plugins which aren't rolled out yet. (but I didn't want to loose the code)
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2524 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
623fa983c3
commit
b7b89ade04
7 changed files with 167 additions and 38 deletions
|
@ -254,41 +254,61 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write Multipart Form Data directly to the HttpWebRequest response stream
|
||||
/// Write Multipart Form Data directly to the HttpWebRequest
|
||||
/// </summary>
|
||||
/// <param name="webRequest">HttpWebRequest to write the multipart form data to</param>
|
||||
/// <param name="postParameters">Parameters to include in the multipart form data</param>
|
||||
public static void WriteMultipartFormData(HttpWebRequest webRequest, IDictionary<string, object> postParameters) {
|
||||
string boundary = String.Format("----------{0:N}", Guid.NewGuid());
|
||||
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
|
||||
|
||||
bool needsCLRF = false;
|
||||
using (Stream formDataStream = webRequest.GetRequestStream()) {
|
||||
foreach (var param in postParameters) {
|
||||
// Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
|
||||
// Skip it on the first parameter, add it to subsequent parameters.
|
||||
if (needsCLRF) {
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, Encoding.UTF8.GetByteCount("\r\n"));
|
||||
}
|
||||
WriteMultipartFormData(formDataStream, boundary, postParameters);
|
||||
}
|
||||
}
|
||||
|
||||
needsCLRF = true;
|
||||
/// <summary>
|
||||
/// Write Multipart Form Data to the HttpListenerResponse
|
||||
/// </summary>
|
||||
/// <param name="webRequest">HttpWebRequest to write the multipart form data to</param>
|
||||
/// <param name="postParameters">Parameters to include in the multipart form data</param>
|
||||
public static void WriteMultipartFormData(HttpListenerResponse response, IDictionary<string, object> postParameters) {
|
||||
string boundary = String.Format("----------{0:N}", Guid.NewGuid());
|
||||
response.ContentType = "multipart/form-data; boundary=" + boundary;
|
||||
WriteMultipartFormData(response.OutputStream, boundary, postParameters);
|
||||
}
|
||||
|
||||
if (param.Value is IBinaryContainer) {
|
||||
IBinaryContainer binaryParameter = (IBinaryContainer)param.Value;
|
||||
binaryParameter.WriteFormDataToStream(boundary, param.Key, formDataStream);
|
||||
} else {
|
||||
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
|
||||
boundary,
|
||||
param.Key,
|
||||
param.Value);
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData));
|
||||
}
|
||||
/// <summary>
|
||||
/// Write Multipart Form Data to a Stream, content-type should be set before this!
|
||||
/// </summary>
|
||||
/// <param name="formDataStream">Stream to write the multipart form data to</param>
|
||||
/// <param name="boundary">String boundary for the multipart/form-data</param>
|
||||
/// <param name="postParameters">Parameters to include in the multipart form data</param>
|
||||
public static void WriteMultipartFormData(Stream formDataStream, string boundary, IDictionary<string, object> postParameters) {
|
||||
bool needsCLRF = false;
|
||||
foreach (var param in postParameters) {
|
||||
// Add a CRLF to allow multiple parameters to be added.
|
||||
// Skip it on the first parameter, add it to subsequent parameters.
|
||||
if (needsCLRF) {
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, Encoding.UTF8.GetByteCount("\r\n"));
|
||||
}
|
||||
|
||||
// Add the end of the request. Start with a newline
|
||||
string footer = "\r\n--" + boundary + "--\r\n";
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
|
||||
needsCLRF = true;
|
||||
|
||||
if (param.Value is IBinaryContainer) {
|
||||
IBinaryContainer binaryParameter = (IBinaryContainer)param.Value;
|
||||
binaryParameter.WriteFormDataToStream(boundary, param.Key, formDataStream);
|
||||
} else {
|
||||
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
|
||||
boundary,
|
||||
param.Key,
|
||||
param.Value);
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData));
|
||||
}
|
||||
}
|
||||
|
||||
// Add the end of the request. Start with a newline
|
||||
string footer = "\r\n--" + boundary + "--\r\n";
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -330,18 +350,18 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// A container to supply files to a Multi-part form data upload
|
||||
/// </summary>
|
||||
public class FileContainer : IBinaryContainer {
|
||||
public class ByteContainer : IBinaryContainer {
|
||||
private byte[] file;
|
||||
private string fileName;
|
||||
private string contentType;
|
||||
private int fileSize;
|
||||
public FileContainer(byte[] file) : this(file, null) {
|
||||
public ByteContainer(byte[] file) : this(file, null) {
|
||||
}
|
||||
public FileContainer(byte[] file, string filename) : this(file, filename, null) {
|
||||
public ByteContainer(byte[] file, string filename) : this(file, filename, null) {
|
||||
}
|
||||
public FileContainer(byte[] file, string filename, string contenttype) : this(file, filename, contenttype, 0) {
|
||||
public ByteContainer(byte[] file, string filename, string contenttype) : this(file, filename, contenttype, 0) {
|
||||
}
|
||||
public FileContainer(byte[] file, string filename, string contenttype, int filesize) {
|
||||
public ByteContainer(byte[] file, string filename, string contenttype, int filesize) {
|
||||
this.file = file;
|
||||
this.fileName = filename;
|
||||
this.contentType = contenttype;
|
||||
|
@ -412,6 +432,82 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// A container to supply images to a Multi-part form data upload
|
||||
/// </summary>
|
||||
public class BitmapContainer : IBinaryContainer {
|
||||
private Bitmap bitmap;
|
||||
private SurfaceOutputSettings outputSettings;
|
||||
private string fileName;
|
||||
|
||||
public BitmapContainer(Bitmap bitmap, SurfaceOutputSettings outputSettings, string filename) {
|
||||
this.bitmap = bitmap;
|
||||
this.outputSettings = outputSettings;
|
||||
this.fileName = filename;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a Base64String from the image by saving it to a memory stream and converting it.
|
||||
/// Should be avoided if possible, as this uses a lot of memory.
|
||||
/// </summary>
|
||||
/// <returns>string</returns>
|
||||
public string ToBase64String(Base64FormattingOptions formattingOptions) {
|
||||
using (MemoryStream stream = new MemoryStream()) {
|
||||
ImageOutput.SaveToStream(bitmap, null, stream, outputSettings);
|
||||
return System.Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a byte[] from the image by saving it to a memory stream.
|
||||
/// Should be avoided if possible, as this uses a lot of memory.
|
||||
/// </summary>
|
||||
/// <returns>byte[]</returns>
|
||||
public byte[] ToByteArray() {
|
||||
using (MemoryStream stream = new MemoryStream()) {
|
||||
ImageOutput.SaveToStream(bitmap, null, stream, outputSettings);
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write Multipart Form Data directly to the HttpWebRequest response stream
|
||||
/// </summary>
|
||||
/// <param name="webRequest">HttpWebRequest to write the multipart form data to</param>
|
||||
/// <param name="postParameters">Parameters to include in the multipart form data</param>
|
||||
public void WriteFormDataToStream(string boundary, string key, Stream formDataStream) {
|
||||
// Add just the first part of this param, since we will write the file data directly to the Stream
|
||||
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
|
||||
boundary,
|
||||
key,
|
||||
fileName ?? key,
|
||||
"image/" + outputSettings.Format.ToString());
|
||||
|
||||
formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header));
|
||||
ImageOutput.SaveToStream(bitmap, null, formDataStream, outputSettings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A plain "write data to stream"
|
||||
/// </summary>
|
||||
/// <param name="dataStream"></param>
|
||||
public void WriteToStream(Stream dataStream) {
|
||||
// Write the file data directly to the Stream, rather than serializing it to a string.
|
||||
ImageOutput.SaveToStream(bitmap, null, dataStream, outputSettings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upload the image to the webrequest
|
||||
/// </summary>
|
||||
/// <param name="webRequest"></param>
|
||||
public void Upload(HttpWebRequest webRequest) {
|
||||
webRequest.ContentType = "image/" + outputSettings.Format.ToString();
|
||||
using (var requestStream = webRequest.GetRequestStream()) {
|
||||
WriteToStream(requestStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A container to supply surfaces to a Multi-part form data upload
|
||||
/// </summary>
|
||||
public class SurfaceContainer : IBinaryContainer {
|
||||
private ISurface surface;
|
||||
private SurfaceOutputSettings outputSettings;
|
||||
|
@ -424,7 +520,7 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a Base64String from the image by saving it to a memory stream and converting it.
|
||||
/// Create a Base64String from the Surface by saving it to a memory stream and converting it.
|
||||
/// Should be avoided if possible, as this uses a lot of memory.
|
||||
/// </summary>
|
||||
/// <returns>string</returns>
|
||||
|
@ -474,7 +570,7 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upload the image to the webrequest
|
||||
/// Upload the Surface as image to the webrequest
|
||||
/// </summary>
|
||||
/// <param name="webRequest"></param>
|
||||
public void Upload(HttpWebRequest webRequest) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue