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; }
|
||||
}
|
||||
|
||||
public void SaveToStream(Stream stream, OutputSettings outputSettings) {
|
||||
using (Image image = surface.GetImageForExport()) {
|
||||
ImageOutput.SaveToStream(image, stream, outputSettings);
|
||||
}
|
||||
}
|
||||
|
||||
public ToolStripMenuItem GetPluginMenuItem() {
|
||||
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();
|
||||
RemoteAttachment attachment = new RemoteAttachment();
|
||||
// Comment is ignored, see: http://jira.atlassian.com/browse/CONF-9395
|
||||
attachment.comment = comment;
|
||||
attachment.fileName = filename;
|
||||
attachment.contentType = mime;
|
||||
confluence.addAttachment(credentials, pageId, attachment, buffer);
|
||||
confluence.addAttachment(credentials, pageId, attachment, image.ToByteArray());
|
||||
}
|
||||
|
||||
public Page getPage(string spaceKey, string pageTitle) {
|
||||
|
|
|
@ -30,6 +30,7 @@ using Confluence;
|
|||
using Greenshot.Plugin;
|
||||
using GreenshotPlugin.Core;
|
||||
using Greenshot.IniFile;
|
||||
using GreenshotPlugin.Controls;
|
||||
|
||||
namespace GreenshotConfluencePlugin {
|
||||
/// <summary>
|
||||
|
@ -158,15 +159,13 @@ namespace GreenshotConfluencePlugin {
|
|||
|
||||
private bool upload(Image image, Page page, string filename, out string errorMessage) {
|
||||
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;
|
||||
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.");
|
||||
if (config.CopyWikiMarkupForImageToClipboard) {
|
||||
int retryCount = 2;
|
||||
|
|
|
@ -30,6 +30,7 @@ namespace GreenshotConfluencePlugin {
|
|||
CANCEL,
|
||||
upload_menu_item,
|
||||
upload_success,
|
||||
upload_failure
|
||||
upload_failure,
|
||||
communication_wait
|
||||
}
|
||||
}
|
||||
|
|
|
@ -70,5 +70,8 @@
|
|||
<resource name="loading">
|
||||
Confluence Daten werden geladen, bitte warten!
|
||||
</resource>
|
||||
<resource name="communication_wait">
|
||||
Übermittle Daten zu Confluence. Bitte warten...
|
||||
</resource>
|
||||
</resources>
|
||||
</language>
|
|
@ -73,5 +73,8 @@
|
|||
<resource name="include_person_spaces">
|
||||
Include personal spaces in search and browsing
|
||||
</resource>
|
||||
<resource name="communication_wait">
|
||||
Transferring data to Confluence, please wait...
|
||||
</resource>
|
||||
</resources>
|
||||
</language>
|
|
@ -70,5 +70,8 @@
|
|||
<resource name="loading">
|
||||
Confluence daten worden geladen, wachten A.U.B.
|
||||
</resource>
|
||||
<resource name="communication_wait">
|
||||
Gegevensoverdracht naar Confluence, wachten A.U.B...
|
||||
</resource>
|
||||
</resources>
|
||||
</language>
|
|
@ -168,7 +168,7 @@ namespace GreenshotImgurPlugin {
|
|||
if (filename != null) {
|
||||
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);
|
||||
} catch (Exception ex) {
|
||||
LOG.Error("Upload to imgur gave an exeption: ", ex);
|
||||
|
|
|
@ -101,14 +101,7 @@ namespace GreenshotJiraPlugin {
|
|||
return selectedIssue;
|
||||
}
|
||||
|
||||
public void upload(string text) {
|
||||
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) {
|
||||
public void upload(IBinaryContainer attachment) {
|
||||
config.LastUsedJira = selectedIssue.Key;
|
||||
jiraConnector.addAttachment(selectedIssue.Key, jiraFilenameBox.Text, attachment);
|
||||
if (jiraCommentBox.Text != null && jiraCommentBox.Text.Length > 0) {
|
||||
|
|
|
@ -292,16 +292,15 @@ namespace Jira {
|
|||
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();
|
||||
try {
|
||||
string base64String = Convert.ToBase64String(buffer, Base64FormattingOptions.InsertLineBreaks);
|
||||
jira.addBase64EncodedAttachmentsToIssue(credentials, issueKey, new string[] { filename }, new string[] { base64String });
|
||||
jira.addBase64EncodedAttachmentsToIssue(credentials, issueKey, new string[] { filename }, new string[] { attachment.ToBase64String(Base64FormattingOptions.InsertLineBreaks) });
|
||||
} catch (Exception ex1) {
|
||||
LOG.WarnFormat("Failed to upload by using method addBase64EncodedAttachmentsToIssue, error was {0}", ex1.Message);
|
||||
try {
|
||||
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) {
|
||||
LOG.WarnFormat("Failed to use alternative method, error was: {0}", ex2.Message);
|
||||
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) {
|
||||
RemoteComment comment = new RemoteComment();
|
||||
comment.body = commentString;
|
||||
|
|
|
@ -104,21 +104,14 @@ namespace GreenshotJiraPlugin {
|
|||
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
|
||||
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
|
||||
string filename = Path.GetFileName(FilenameHelper.GetFilename(config.UploadFormat, captureDetails));
|
||||
byte[] buffer;
|
||||
OutputSettings outputSettings = new OutputSettings(config.UploadFormat, config.UploadJpegQuality, config.UploadReduceColors);
|
||||
if (jira != null) {
|
||||
using (MemoryStream stream = new MemoryStream()) {
|
||||
using (Image image = surface.GetImageForExport()) {
|
||||
ImageOutput.SaveToStream(image, stream, outputSettings);
|
||||
}
|
||||
// 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);
|
||||
jiraPlugin.JiraConnector.addAttachment(jira.Key, filename, new ImageContainer(image, outputSettings, filename));
|
||||
}
|
||||
);
|
||||
LOG.Debug("Uploaded to Jira.");
|
||||
|
@ -127,27 +120,22 @@ namespace GreenshotJiraPlugin {
|
|||
} catch (Exception e) {
|
||||
MessageBox.Show(Language.GetString("jira", LangKey.upload_failure) + " " + e.Message);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
JiraForm jiraForm = new JiraForm(jiraPlugin.JiraConnector);
|
||||
|
||||
if (jiraPlugin.JiraConnector.isLoggedIn) {
|
||||
jiraForm.setFilename(filename);
|
||||
DialogResult result = jiraForm.ShowDialog();
|
||||
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 {
|
||||
using (Image image = surface.GetImageForExport()) {
|
||||
// Run upload in the background
|
||||
new PleaseWaitForm().ShowAndWait(Description, Language.GetString("jira", LangKey.communication_wait),
|
||||
delegate() {
|
||||
jiraForm.upload(buffer);
|
||||
jiraForm.upload(new ImageContainer(image, outputSettings, filename));
|
||||
}
|
||||
);
|
||||
}
|
||||
LOG.Debug("Uploaded to Jira.");
|
||||
exportInformation.ExportMade = true;
|
||||
exportInformation.Uri = surface.UploadURL;
|
||||
|
|
|
@ -65,7 +65,7 @@
|
|||
Beeld formaat
|
||||
</resource>
|
||||
<resource name="communication_wait">
|
||||
Data overdraging naar Jira, wachten AUB...
|
||||
Gegevensoverdracht naar Jira, wachten A.U.B...
|
||||
</resource>
|
||||
</resources>
|
||||
</language>
|
|
@ -96,7 +96,7 @@ namespace GreenshotPhotobucketPlugin {
|
|||
oAuth.Sign(HTTPMethod.POST, apiUrl, parameters);
|
||||
apiUrl = apiUrl.Replace("api.photobucket.com", config.SubDomain);
|
||||
// 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);
|
||||
} catch (Exception ex) {
|
||||
LOG.Error("Error uploading to Photobucket.", ex);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -378,7 +378,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="contentType">contenttype for the postdata</param>
|
||||
/// <param name="postData">Data to post (MemoryStream)</param>
|
||||
/// <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) {
|
||||
parameters = new Dictionary<string, object>();
|
||||
}
|
||||
|
@ -482,7 +482,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="contentType"></param>
|
||||
/// <param name="postData">IBinaryParameter</param>
|
||||
/// <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) {
|
||||
throw new ArgumentNullException("parameters");
|
||||
}
|
||||
|
@ -529,9 +529,9 @@ namespace GreenshotPlugin.Core {
|
|||
} else {
|
||||
StringBuilder form = new StringBuilder();
|
||||
foreach (string parameterKey in requestParameters.Keys) {
|
||||
if (parameters[parameterKey] is IBinaryParameter) {
|
||||
IBinaryParameter binaryParameter = parameters[parameterKey] as IBinaryParameter;
|
||||
form.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}&", UrlEncode3986(parameterKey), UrlEncode3986(binaryParameter.ToBase64String()));
|
||||
if (parameters[parameterKey] is IBinaryContainer) {
|
||||
IBinaryContainer binaryParameter = parameters[parameterKey] as IBinaryContainer;
|
||||
form.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}&", UrlEncode3986(parameterKey), UrlEncode3986(binaryParameter.ToBase64String(Base64FormattingOptions.None)));
|
||||
} else {
|
||||
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>
|
||||
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>
|
||||
/// Get the ToolStripMenuItem where plugins can place their Menu entrys
|
||||
/// </summary>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue