Code reuse and cleanup.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2126 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-10-08 15:32:14 +00:00
commit 448f82f6c3
16 changed files with 119 additions and 107 deletions

View file

@ -264,8 +264,8 @@ namespace GreenshotPlugin.Core {
needsCLRF = true;
if (param.Value is IBinaryParameter) {
IBinaryParameter binaryParameter = (IBinaryParameter)param.Value;
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}",
@ -283,27 +283,31 @@ namespace GreenshotPlugin.Core {
}
}
public interface IBinaryParameter {
/// <summary>
/// This interface can be used to pass binary information around, like byte[] or Image
/// </summary>
public interface IBinaryContainer {
void WriteFormDataToStream(string boundary, string key, Stream formDataStream);
void WriteToStream(Stream formDataStream);
string ToBase64String();
string ToBase64String(Base64FormattingOptions formattingOptions);
byte[] ToByteArray();
}
/// <summary>
/// A container to supply files to a Multi-part form data upload
/// </summary>
public class FileParameter : IBinaryParameter {
public class FileContainer : IBinaryContainer {
private byte[] file;
private string fileName;
private string contentType;
private int fileSize;
public FileParameter(byte[] file) : this(file, null) {
public FileContainer(byte[] file) : this(file, null) {
}
public FileParameter(byte[] file, string filename) : this(file, filename, null) {
public FileContainer(byte[] file, string filename) : this(file, filename, null) {
}
public FileParameter(byte[] file, string filename, string contenttype) : this(file, filename, contenttype, 0) {
public FileContainer(byte[] file, string filename, string contenttype) : this(file, filename, contenttype, 0) {
}
public FileParameter(byte[] file, string filename, string contenttype, int filesize) {
public FileContainer(byte[] file, string filename, string contenttype, int filesize) {
this.file = file;
this.fileName = filename;
this.contentType = contenttype;
@ -314,10 +318,27 @@ namespace GreenshotPlugin.Core {
}
}
public string ToBase64String() {
return System.Convert.ToBase64String(file, 0, fileSize);
/// <summary>
/// Create a Base64String from the byte[]
/// </summary>
/// <returns>string</returns>
public string ToBase64String(Base64FormattingOptions formattingOptions) {
return System.Convert.ToBase64String(file, 0, fileSize, formattingOptions);
}
/// <summary>
/// Returns the initial byte-array which was supplied when creating the FileParameter
/// </summary>
/// <returns>byte[]</returns>
public byte[] ToByteArray() {
return file;
}
/// <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",
@ -332,6 +353,10 @@ namespace GreenshotPlugin.Core {
formDataStream.Write(file, 0, fileSize);
}
/// <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.
dataStream.Write(file, 0, fileSize);
@ -341,24 +366,46 @@ namespace GreenshotPlugin.Core {
/// <summary>
/// A container to supply images to a Multi-part form data upload
/// </summary>
public class ImageParameter : IBinaryParameter {
public class ImageContainer : IBinaryContainer {
private Image image;
private OutputSettings outputSettings;
private string fileName;
public ImageParameter(Image image, OutputSettings outputSettings, string filename) {
public ImageContainer(Image image, OutputSettings outputSettings, string filename) {
this.image = image;
this.outputSettings = outputSettings;
this.fileName = filename;
}
public string ToBase64String() {
/// <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(image, stream, outputSettings);
return System.Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length);
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(image, 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",
@ -373,6 +420,10 @@ namespace GreenshotPlugin.Core {
ImageOutput.SaveToStream(image, 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(image, dataStream, outputSettings);