mirror of
https://github.com/greenshot/greenshot
synced 2025-08-19 21:13:23 -07:00
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:
parent
abe79e65e9
commit
448f82f6c3
16 changed files with 119 additions and 107 deletions
|
@ -428,12 +428,6 @@ namespace Greenshot {
|
||||||
get { return surface.CaptureDetails; }
|
get { return surface.CaptureDetails; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SaveToStream(Stream stream, OutputSettings outputSettings) {
|
|
||||||
using (Image image = surface.GetImageForExport()) {
|
|
||||||
ImageOutput.SaveToStream(image, stream, outputSettings);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ToolStripMenuItem GetPluginMenuItem() {
|
public ToolStripMenuItem GetPluginMenuItem() {
|
||||||
return pluginToolStripMenuItem;
|
return pluginToolStripMenuItem;
|
||||||
}
|
}
|
||||||
|
|
|
@ -208,14 +208,14 @@ namespace Confluence {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAttachment(long pageId, string mime, string comment, string filename, byte[] buffer) {
|
public void addAttachment(long pageId, string mime, string comment, string filename, IBinaryContainer image) {
|
||||||
checkCredentials();
|
checkCredentials();
|
||||||
RemoteAttachment attachment = new RemoteAttachment();
|
RemoteAttachment attachment = new RemoteAttachment();
|
||||||
// Comment is ignored, see: http://jira.atlassian.com/browse/CONF-9395
|
// Comment is ignored, see: http://jira.atlassian.com/browse/CONF-9395
|
||||||
attachment.comment = comment;
|
attachment.comment = comment;
|
||||||
attachment.fileName = filename;
|
attachment.fileName = filename;
|
||||||
attachment.contentType = mime;
|
attachment.contentType = mime;
|
||||||
confluence.addAttachment(credentials, pageId, attachment, buffer);
|
confluence.addAttachment(credentials, pageId, attachment, image.ToByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Page getPage(string spaceKey, string pageTitle) {
|
public Page getPage(string spaceKey, string pageTitle) {
|
||||||
|
|
|
@ -30,6 +30,7 @@ using Confluence;
|
||||||
using Greenshot.Plugin;
|
using Greenshot.Plugin;
|
||||||
using GreenshotPlugin.Core;
|
using GreenshotPlugin.Core;
|
||||||
using Greenshot.IniFile;
|
using Greenshot.IniFile;
|
||||||
|
using GreenshotPlugin.Controls;
|
||||||
|
|
||||||
namespace GreenshotConfluencePlugin {
|
namespace GreenshotConfluencePlugin {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -158,15 +159,13 @@ namespace GreenshotConfluencePlugin {
|
||||||
|
|
||||||
private bool upload(Image image, Page page, string filename, out string errorMessage) {
|
private bool upload(Image image, Page page, string filename, out string errorMessage) {
|
||||||
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
|
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
|
||||||
byte[] buffer;
|
|
||||||
using (MemoryStream stream = new MemoryStream()) {
|
|
||||||
ImageOutput.SaveToStream(image, stream, outputSettings);
|
|
||||||
// COPY buffer to array
|
|
||||||
buffer = stream.ToArray();
|
|
||||||
}
|
|
||||||
errorMessage = null;
|
errorMessage = null;
|
||||||
try {
|
try {
|
||||||
ConfluencePlugin.ConfluenceConnector.addAttachment(page.id, "image/" + config.UploadFormat.ToString().ToLower(), null, filename, buffer);
|
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("confluence", LangKey.communication_wait),
|
||||||
|
delegate() {
|
||||||
|
ConfluencePlugin.ConfluenceConnector.addAttachment(page.id, "image/" + config.UploadFormat.ToString().ToLower(), null, filename, new ImageContainer(image, outputSettings, filename));
|
||||||
|
}
|
||||||
|
);
|
||||||
LOG.Debug("Uploaded to Confluence.");
|
LOG.Debug("Uploaded to Confluence.");
|
||||||
if (config.CopyWikiMarkupForImageToClipboard) {
|
if (config.CopyWikiMarkupForImageToClipboard) {
|
||||||
int retryCount = 2;
|
int retryCount = 2;
|
||||||
|
|
|
@ -30,6 +30,7 @@ namespace GreenshotConfluencePlugin {
|
||||||
CANCEL,
|
CANCEL,
|
||||||
upload_menu_item,
|
upload_menu_item,
|
||||||
upload_success,
|
upload_success,
|
||||||
upload_failure
|
upload_failure,
|
||||||
|
communication_wait
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,5 +70,8 @@
|
||||||
<resource name="loading">
|
<resource name="loading">
|
||||||
Confluence Daten werden geladen, bitte warten!
|
Confluence Daten werden geladen, bitte warten!
|
||||||
</resource>
|
</resource>
|
||||||
|
<resource name="communication_wait">
|
||||||
|
Übermittle Daten zu Confluence. Bitte warten...
|
||||||
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
</language>
|
</language>
|
|
@ -73,5 +73,8 @@
|
||||||
<resource name="include_person_spaces">
|
<resource name="include_person_spaces">
|
||||||
Include personal spaces in search and browsing
|
Include personal spaces in search and browsing
|
||||||
</resource>
|
</resource>
|
||||||
|
<resource name="communication_wait">
|
||||||
|
Transferring data to Confluence, please wait...
|
||||||
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
</language>
|
</language>
|
|
@ -70,5 +70,8 @@
|
||||||
<resource name="loading">
|
<resource name="loading">
|
||||||
Confluence daten worden geladen, wachten A.U.B.
|
Confluence daten worden geladen, wachten A.U.B.
|
||||||
</resource>
|
</resource>
|
||||||
|
<resource name="communication_wait">
|
||||||
|
Gegevensoverdracht naar Confluence, wachten A.U.B...
|
||||||
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
</language>
|
</language>
|
|
@ -168,7 +168,7 @@ namespace GreenshotImgurPlugin {
|
||||||
if (filename != null) {
|
if (filename != null) {
|
||||||
uploadParameters.Add("name", filename);
|
uploadParameters.Add("name", filename);
|
||||||
}
|
}
|
||||||
uploadParameters.Add("image", new ImageParameter(image, outputSettings, filename));
|
uploadParameters.Add("image", new ImageContainer(image, outputSettings, filename));
|
||||||
responseString = oAuth.MakeRequest(HTTPMethod.POST, apiUrl, uploadParameters, null, null);
|
responseString = oAuth.MakeRequest(HTTPMethod.POST, apiUrl, uploadParameters, null, null);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
LOG.Error("Upload to imgur gave an exeption: ", ex);
|
LOG.Error("Upload to imgur gave an exeption: ", ex);
|
||||||
|
|
|
@ -101,14 +101,7 @@ namespace GreenshotJiraPlugin {
|
||||||
return selectedIssue;
|
return selectedIssue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void upload(string text) {
|
public void upload(IBinaryContainer attachment) {
|
||||||
jiraConnector.addAttachment(selectedIssue.Key, jiraFilenameBox.Text, text);
|
|
||||||
if (jiraCommentBox.Text != null && jiraCommentBox.Text.Length > 0) {
|
|
||||||
jiraConnector.addComment(selectedIssue.Key, jiraCommentBox.Text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void upload(byte [] attachment) {
|
|
||||||
config.LastUsedJira = selectedIssue.Key;
|
config.LastUsedJira = selectedIssue.Key;
|
||||||
jiraConnector.addAttachment(selectedIssue.Key, jiraFilenameBox.Text, attachment);
|
jiraConnector.addAttachment(selectedIssue.Key, jiraFilenameBox.Text, attachment);
|
||||||
if (jiraCommentBox.Text != null && jiraCommentBox.Text.Length > 0) {
|
if (jiraCommentBox.Text != null && jiraCommentBox.Text.Length > 0) {
|
||||||
|
|
|
@ -292,16 +292,15 @@ namespace Jira {
|
||||||
return url.Replace(DEFAULT_POSTFIX,"") + "/browse/" + issueKey;
|
return url.Replace(DEFAULT_POSTFIX,"") + "/browse/" + issueKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAttachment(string issueKey, string filename, byte [] buffer) {
|
public void addAttachment(string issueKey, string filename, IBinaryContainer attachment) {
|
||||||
checkCredentials();
|
checkCredentials();
|
||||||
try {
|
try {
|
||||||
string base64String = Convert.ToBase64String(buffer, Base64FormattingOptions.InsertLineBreaks);
|
jira.addBase64EncodedAttachmentsToIssue(credentials, issueKey, new string[] { filename }, new string[] { attachment.ToBase64String(Base64FormattingOptions.InsertLineBreaks) });
|
||||||
jira.addBase64EncodedAttachmentsToIssue(credentials, issueKey, new string[] { filename }, new string[] { base64String });
|
|
||||||
} catch (Exception ex1) {
|
} catch (Exception ex1) {
|
||||||
LOG.WarnFormat("Failed to upload by using method addBase64EncodedAttachmentsToIssue, error was {0}", ex1.Message);
|
LOG.WarnFormat("Failed to upload by using method addBase64EncodedAttachmentsToIssue, error was {0}", ex1.Message);
|
||||||
try {
|
try {
|
||||||
LOG.Warn("Trying addAttachmentsToIssue instead");
|
LOG.Warn("Trying addAttachmentsToIssue instead");
|
||||||
jira.addAttachmentsToIssue(credentials, issueKey, new string[] { filename }, (sbyte[]) (Array)buffer);
|
jira.addAttachmentsToIssue(credentials, issueKey, new string[] { filename }, (sbyte[])(Array)attachment.ToByteArray());
|
||||||
} catch (Exception ex2) {
|
} catch (Exception ex2) {
|
||||||
LOG.WarnFormat("Failed to use alternative method, error was: {0}", ex2.Message);
|
LOG.WarnFormat("Failed to use alternative method, error was: {0}", ex2.Message);
|
||||||
throw ex2;
|
throw ex2;
|
||||||
|
@ -309,21 +308,6 @@ namespace Jira {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAttachment(string issueKey, string filename, string attachmentText) {
|
|
||||||
Encoding WINDOWS1252 = Encoding.GetEncoding(1252);
|
|
||||||
byte[] attachment = WINDOWS1252.GetBytes(attachmentText.ToCharArray());
|
|
||||||
addAttachment(issueKey, filename, attachment);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addAttachment(string issueKey, string filePath) {
|
|
||||||
FileInfo fileInfo = new FileInfo(filePath);
|
|
||||||
byte[] buffer = new byte[fileInfo.Length];
|
|
||||||
using (FileStream stream = new FileStream(filePath, FileMode.Open)) {
|
|
||||||
stream.Read(buffer, 0, (int)fileInfo.Length);
|
|
||||||
}
|
|
||||||
addAttachment(issueKey, Path.GetFileName(filePath), buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addComment(string issueKey, string commentString) {
|
public void addComment(string issueKey, string commentString) {
|
||||||
RemoteComment comment = new RemoteComment();
|
RemoteComment comment = new RemoteComment();
|
||||||
comment.body = commentString;
|
comment.body = commentString;
|
||||||
|
|
|
@ -104,50 +104,38 @@ namespace GreenshotJiraPlugin {
|
||||||
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
|
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
|
||||||
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
|
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
|
||||||
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
|
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
|
||||||
byte[] buffer;
|
|
||||||
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
|
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
|
||||||
if (jira != null) {
|
if (jira != null) {
|
||||||
using (MemoryStream stream = new MemoryStream()) {
|
using (Image image = surface.GetImageForExport()) {
|
||||||
using (Image image = surface.GetImageForExport()) {
|
try {
|
||||||
ImageOutput.SaveToStream(image, stream, outputSettings);
|
// Run upload in the background
|
||||||
|
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("jira", LangKey.communication_wait),
|
||||||
|
delegate() {
|
||||||
|
jiraPlugin.JiraConnector.addAttachment(jira.Key, filename, new ImageContainer(image, outputSettings, filename));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
LOG.Debug("Uploaded to Jira.");
|
||||||
|
exportInformation.ExportMade = true;
|
||||||
|
exportInformation.Uri = surface.UploadURL;
|
||||||
|
} catch (Exception e) {
|
||||||
|
MessageBox.Show(Language.GetString("jira", LangKey.upload_failure) + " " + e.Message);
|
||||||
}
|
}
|
||||||
// COPY stream to buffer
|
|
||||||
buffer = stream.ToArray();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// Run upload in the background
|
|
||||||
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("jira", LangKey.communication_wait),
|
|
||||||
delegate() {
|
|
||||||
jiraPlugin.JiraConnector.addAttachment(jira.Key, filename, buffer);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
LOG.Debug("Uploaded to Jira.");
|
|
||||||
exportInformation.ExportMade = true;
|
|
||||||
exportInformation.Uri = surface.UploadURL;
|
|
||||||
} catch (Exception e) {
|
|
||||||
MessageBox.Show(Language.GetString("jira", LangKey.upload_failure) + " " + e.Message);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
JiraForm jiraForm = new JiraForm(jiraPlugin.JiraConnector);
|
JiraForm jiraForm = new JiraForm(jiraPlugin.JiraConnector);
|
||||||
|
|
||||||
if (jiraPlugin.JiraConnector.isLoggedIn) {
|
if (jiraPlugin.JiraConnector.isLoggedIn) {
|
||||||
jiraForm.setFilename(filename);
|
jiraForm.setFilename(filename);
|
||||||
DialogResult result = jiraForm.ShowDialog();
|
DialogResult result = jiraForm.ShowDialog();
|
||||||
if (result == DialogResult.OK) {
|
if (result == DialogResult.OK) {
|
||||||
using (MemoryStream stream = new MemoryStream()) {
|
|
||||||
using (Image image = surface.GetImageForExport()) {
|
|
||||||
ImageOutput.SaveToStream(image, stream, outputSettings);
|
|
||||||
}
|
|
||||||
// COPY stream to buffer
|
|
||||||
buffer = stream.ToArray();
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
// Run upload in the background
|
using (Image image = surface.GetImageForExport()) {
|
||||||
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("jira", LangKey.communication_wait),
|
// Run upload in the background
|
||||||
delegate() {
|
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("jira", LangKey.communication_wait),
|
||||||
jiraForm.upload(buffer);
|
delegate() {
|
||||||
}
|
jiraForm.upload(new ImageContainer(image, outputSettings, filename));
|
||||||
);
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
LOG.Debug("Uploaded to Jira.");
|
LOG.Debug("Uploaded to Jira.");
|
||||||
exportInformation.ExportMade = true;
|
exportInformation.ExportMade = true;
|
||||||
exportInformation.Uri = surface.UploadURL;
|
exportInformation.Uri = surface.UploadURL;
|
||||||
|
|
|
@ -65,7 +65,7 @@
|
||||||
Beeld formaat
|
Beeld formaat
|
||||||
</resource>
|
</resource>
|
||||||
<resource name="communication_wait">
|
<resource name="communication_wait">
|
||||||
Data overdraging naar Jira, wachten AUB...
|
Gegevensoverdracht naar Jira, wachten A.U.B...
|
||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
</language>
|
</language>
|
|
@ -96,7 +96,7 @@ namespace GreenshotPhotobucketPlugin {
|
||||||
oAuth.Sign(HTTPMethod.POST, apiUrl, parameters);
|
oAuth.Sign(HTTPMethod.POST, apiUrl, parameters);
|
||||||
apiUrl = apiUrl.Replace("api.photobucket.com", config.SubDomain);
|
apiUrl = apiUrl.Replace("api.photobucket.com", config.SubDomain);
|
||||||
// Add image
|
// Add image
|
||||||
parameters.Add("uploadfile", new ImageParameter(image, outputSettings, filename));
|
parameters.Add("uploadfile", new ImageContainer(image, outputSettings, filename));
|
||||||
responseString = oAuth.MakeRequest(HTTPMethod.POST, apiUrl, parameters, null, null);
|
responseString = oAuth.MakeRequest(HTTPMethod.POST, apiUrl, parameters, null, null);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
LOG.Error("Error uploading to Photobucket.", ex);
|
LOG.Error("Error uploading to Photobucket.", ex);
|
||||||
|
|
|
@ -264,8 +264,8 @@ namespace GreenshotPlugin.Core {
|
||||||
|
|
||||||
needsCLRF = true;
|
needsCLRF = true;
|
||||||
|
|
||||||
if (param.Value is IBinaryParameter) {
|
if (param.Value is IBinaryContainer) {
|
||||||
IBinaryParameter binaryParameter = (IBinaryParameter)param.Value;
|
IBinaryContainer binaryParameter = (IBinaryContainer)param.Value;
|
||||||
binaryParameter.WriteFormDataToStream(boundary, param.Key, formDataStream);
|
binaryParameter.WriteFormDataToStream(boundary, param.Key, formDataStream);
|
||||||
} else {
|
} else {
|
||||||
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
|
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 WriteFormDataToStream(string boundary, string key, Stream formDataStream);
|
||||||
void WriteToStream(Stream formDataStream);
|
void WriteToStream(Stream formDataStream);
|
||||||
string ToBase64String();
|
string ToBase64String(Base64FormattingOptions formattingOptions);
|
||||||
|
byte[] ToByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A container to supply files to a Multi-part form data upload
|
/// A container to supply files to a Multi-part form data upload
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class FileParameter : IBinaryParameter {
|
public class FileContainer : IBinaryContainer {
|
||||||
private byte[] file;
|
private byte[] file;
|
||||||
private string fileName;
|
private string fileName;
|
||||||
private string contentType;
|
private string contentType;
|
||||||
private int fileSize;
|
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.file = file;
|
||||||
this.fileName = filename;
|
this.fileName = filename;
|
||||||
this.contentType = contenttype;
|
this.contentType = contenttype;
|
||||||
|
@ -314,10 +318,27 @@ namespace GreenshotPlugin.Core {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ToBase64String() {
|
/// <summary>
|
||||||
return System.Convert.ToBase64String(file, 0, fileSize);
|
/// 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) {
|
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
|
// 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",
|
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);
|
formDataStream.Write(file, 0, fileSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A plain "write data to stream"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataStream"></param>
|
||||||
public void WriteToStream(Stream dataStream) {
|
public void WriteToStream(Stream dataStream) {
|
||||||
// Write the file data directly to the Stream, rather than serializing it to a string.
|
// Write the file data directly to the Stream, rather than serializing it to a string.
|
||||||
dataStream.Write(file, 0, fileSize);
|
dataStream.Write(file, 0, fileSize);
|
||||||
|
@ -341,24 +366,46 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A container to supply images to a Multi-part form data upload
|
/// A container to supply images to a Multi-part form data upload
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ImageParameter : IBinaryParameter {
|
public class ImageContainer : IBinaryContainer {
|
||||||
private Image image;
|
private Image image;
|
||||||
private OutputSettings outputSettings;
|
private OutputSettings outputSettings;
|
||||||
private string fileName;
|
private string fileName;
|
||||||
|
|
||||||
public ImageParameter(Image image, OutputSettings outputSettings, string filename) {
|
public ImageContainer(Image image, OutputSettings outputSettings, string filename) {
|
||||||
this.image = image;
|
this.image = image;
|
||||||
this.outputSettings = outputSettings;
|
this.outputSettings = outputSettings;
|
||||||
this.fileName = filename;
|
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()) {
|
using (MemoryStream stream = new MemoryStream()) {
|
||||||
ImageOutput.SaveToStream(image, stream, outputSettings);
|
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) {
|
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
|
// 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",
|
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);
|
ImageOutput.SaveToStream(image, formDataStream, outputSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A plain "write data to stream"
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataStream"></param>
|
||||||
public void WriteToStream(Stream dataStream) {
|
public void WriteToStream(Stream dataStream) {
|
||||||
// Write the file data directly to the Stream, rather than serializing it to a string.
|
// Write the file data directly to the Stream, rather than serializing it to a string.
|
||||||
ImageOutput.SaveToStream(image, dataStream, outputSettings);
|
ImageOutput.SaveToStream(image, dataStream, outputSettings);
|
||||||
|
|
|
@ -378,7 +378,7 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <param name="contentType">contenttype for the postdata</param>
|
/// <param name="contentType">contenttype for the postdata</param>
|
||||||
/// <param name="postData">Data to post (MemoryStream)</param>
|
/// <param name="postData">Data to post (MemoryStream)</param>
|
||||||
/// <returns>The web server response.</returns>
|
/// <returns>The web server response.</returns>
|
||||||
public string MakeOAuthRequest(HTTPMethod method, string requestURL, IDictionary<string, object> parameters, string contentType, IBinaryParameter postData) {
|
public string MakeOAuthRequest(HTTPMethod method, string requestURL, IDictionary<string, object> parameters, string contentType, IBinaryContainer postData) {
|
||||||
if (parameters == null) {
|
if (parameters == null) {
|
||||||
parameters = new Dictionary<string, object>();
|
parameters = new Dictionary<string, object>();
|
||||||
}
|
}
|
||||||
|
@ -482,7 +482,7 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <param name="contentType"></param>
|
/// <param name="contentType"></param>
|
||||||
/// <param name="postData">IBinaryParameter</param>
|
/// <param name="postData">IBinaryParameter</param>
|
||||||
/// <returns>Response from server</returns>
|
/// <returns>Response from server</returns>
|
||||||
public string MakeRequest(HTTPMethod method, string requestURL, IDictionary<string, object> parameters, string contentType, IBinaryParameter postData) {
|
public string MakeRequest(HTTPMethod method, string requestURL, IDictionary<string, object> parameters, string contentType, IBinaryContainer postData) {
|
||||||
if (parameters == null) {
|
if (parameters == null) {
|
||||||
throw new ArgumentNullException("parameters");
|
throw new ArgumentNullException("parameters");
|
||||||
}
|
}
|
||||||
|
@ -529,9 +529,9 @@ namespace GreenshotPlugin.Core {
|
||||||
} else {
|
} else {
|
||||||
StringBuilder form = new StringBuilder();
|
StringBuilder form = new StringBuilder();
|
||||||
foreach (string parameterKey in requestParameters.Keys) {
|
foreach (string parameterKey in requestParameters.Keys) {
|
||||||
if (parameters[parameterKey] is IBinaryParameter) {
|
if (parameters[parameterKey] is IBinaryContainer) {
|
||||||
IBinaryParameter binaryParameter = parameters[parameterKey] as IBinaryParameter;
|
IBinaryContainer binaryParameter = parameters[parameterKey] as IBinaryContainer;
|
||||||
form.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}&", UrlEncode3986(parameterKey), UrlEncode3986(binaryParameter.ToBase64String()));
|
form.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}&", UrlEncode3986(parameterKey), UrlEncode3986(binaryParameter.ToBase64String(Base64FormattingOptions.None)));
|
||||||
} else {
|
} else {
|
||||||
form.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}&", UrlEncode3986(parameterKey), UrlEncode3986(string.Format("{0}",parameters[parameterKey])));
|
form.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}&", UrlEncode3986(parameterKey), UrlEncode3986(string.Format("{0}",parameters[parameterKey])));
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,13 +45,6 @@ namespace Greenshot.Plugin {
|
||||||
/// <returns>Bitmap</returns>
|
/// <returns>Bitmap</returns>
|
||||||
Image GetImageForExport();
|
Image GetImageForExport();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Will save the current Image in the supplied format to the given stream
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="stream">The stream the image is stored on</param>
|
|
||||||
/// <param name="outputSettings">OutputSettings</param>
|
|
||||||
void SaveToStream(Stream stream, OutputSettings outputSettings);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the ToolStripMenuItem where plugins can place their Menu entrys
|
/// Get the ToolStripMenuItem where plugins can place their Menu entrys
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue