From e6462f9d8019cc368750268689133e5c20cb006f Mon Sep 17 00:00:00 2001 From: HarlanC Date: Thu, 8 Feb 2018 21:25:34 +0800 Subject: [PATCH] use dll by nuget;fix bug;add Zone --- .../Forms/SettingsForm.Designer.cs | 52 ++++++++++++++----- .../GreenshotQiniuPlugin.csproj | 39 +++++++------- GreenshotQiniuPlugin/QiniuConfiguration.cs | 14 ++++- GreenshotQiniuPlugin/QiniuPlugin.cs | 16 +++--- GreenshotQiniuPlugin/QiniuUtils.cs | 38 ++++++++++---- GreenshotQiniuPlugin/packages.config | 5 ++ 6 files changed, 115 insertions(+), 49 deletions(-) create mode 100644 GreenshotQiniuPlugin/packages.config diff --git a/GreenshotQiniuPlugin/Forms/SettingsForm.Designer.cs b/GreenshotQiniuPlugin/Forms/SettingsForm.Designer.cs index c7cd7917e..1e165f057 100644 --- a/GreenshotQiniuPlugin/Forms/SettingsForm.Designer.cs +++ b/GreenshotQiniuPlugin/Forms/SettingsForm.Designer.cs @@ -43,12 +43,14 @@ this.gslblDefaultDomain = new GreenshotPlugin.Controls.GreenshotLabel(); this.gstbImageNamePrefix = new GreenshotPlugin.Controls.GreenshotTextBox(); this.gslblImageNamePrefix = new GreenshotPlugin.Controls.GreenshotLabel(); + this.gslblZone = new GreenshotPlugin.Controls.GreenshotLabel(); + this.gscobZone = new GreenshotPlugin.Controls.GreenshotComboBox(); this.SuspendLayout(); // // gslblAccessKey // this.gslblAccessKey.AutoSize = true; - this.gslblAccessKey.Location = new System.Drawing.Point(18, 48); + this.gslblAccessKey.Location = new System.Drawing.Point(18, 79); this.gslblAccessKey.Name = "gslblAccessKey"; this.gslblAccessKey.Size = new System.Drawing.Size(65, 12); this.gslblAccessKey.TabIndex = 0; @@ -56,7 +58,7 @@ // // gstbAccessKey // - this.gstbAccessKey.Location = new System.Drawing.Point(137, 48); + this.gstbAccessKey.Location = new System.Drawing.Point(137, 79); this.gstbAccessKey.Name = "gstbAccessKey"; this.gstbAccessKey.PropertyName = "AccessKey"; this.gstbAccessKey.SectionName = "Qiniu"; @@ -65,7 +67,7 @@ // // gstbSecretKey // - this.gstbSecretKey.Location = new System.Drawing.Point(137, 79); + this.gstbSecretKey.Location = new System.Drawing.Point(137, 113); this.gstbSecretKey.Name = "gstbSecretKey"; this.gstbSecretKey.PropertyName = "SecretKey"; this.gstbSecretKey.SectionName = "Qiniu"; @@ -75,7 +77,7 @@ // gslblSecretKey // this.gslblSecretKey.AutoSize = true; - this.gslblSecretKey.Location = new System.Drawing.Point(18, 82); + this.gslblSecretKey.Location = new System.Drawing.Point(18, 116); this.gslblSecretKey.Name = "gslblSecretKey"; this.gslblSecretKey.Size = new System.Drawing.Size(65, 12); this.gslblSecretKey.TabIndex = 2; @@ -83,7 +85,7 @@ // // gstbScope // - this.gstbScope.Location = new System.Drawing.Point(137, 111); + this.gstbScope.Location = new System.Drawing.Point(137, 147); this.gstbScope.Name = "gstbScope"; this.gstbScope.PropertyName = "Scope"; this.gstbScope.SectionName = "Qiniu"; @@ -93,7 +95,7 @@ // gslblScope // this.gslblScope.AutoSize = true; - this.gslblScope.Location = new System.Drawing.Point(18, 114); + this.gslblScope.Location = new System.Drawing.Point(18, 150); this.gslblScope.Name = "gslblScope"; this.gslblScope.Size = new System.Drawing.Size(35, 12); this.gslblScope.TabIndex = 4; @@ -104,7 +106,7 @@ this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.buttonCancel.LanguageKey = "CANCEL"; - this.buttonCancel.Location = new System.Drawing.Point(294, 208); + this.buttonCancel.Location = new System.Drawing.Point(294, 248); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(75, 23); this.buttonCancel.TabIndex = 7; @@ -116,7 +118,7 @@ this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK; this.buttonOK.LanguageKey = "OK"; - this.buttonOK.Location = new System.Drawing.Point(213, 208); + this.buttonOK.Location = new System.Drawing.Point(213, 248); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(75, 23); this.buttonOK.TabIndex = 6; @@ -145,7 +147,7 @@ // // gstbDefaultDomain // - this.gstbDefaultDomain.Location = new System.Drawing.Point(137, 144); + this.gstbDefaultDomain.Location = new System.Drawing.Point(137, 180); this.gstbDefaultDomain.Name = "gstbDefaultDomain"; this.gstbDefaultDomain.PropertyName = "DefaultDomain"; this.gstbDefaultDomain.SectionName = "Qiniu"; @@ -155,7 +157,7 @@ // gslblDefaultDomain // this.gslblDefaultDomain.AutoSize = true; - this.gslblDefaultDomain.Location = new System.Drawing.Point(18, 147); + this.gslblDefaultDomain.Location = new System.Drawing.Point(18, 183); this.gslblDefaultDomain.Name = "gslblDefaultDomain"; this.gslblDefaultDomain.Size = new System.Drawing.Size(89, 12); this.gslblDefaultDomain.TabIndex = 16; @@ -163,7 +165,7 @@ // // gstbImageNamePrefix // - this.gstbImageNamePrefix.Location = new System.Drawing.Point(137, 178); + this.gstbImageNamePrefix.Location = new System.Drawing.Point(137, 214); this.gstbImageNamePrefix.Name = "gstbImageNamePrefix"; this.gstbImageNamePrefix.PropertyName = "ImageNamePrefix"; this.gstbImageNamePrefix.SectionName = "Qiniu"; @@ -173,17 +175,39 @@ // gslblImageNamePrefix // this.gslblImageNamePrefix.AutoSize = true; - this.gslblImageNamePrefix.Location = new System.Drawing.Point(18, 181); + this.gslblImageNamePrefix.Location = new System.Drawing.Point(18, 217); this.gslblImageNamePrefix.Name = "gslblImageNamePrefix"; this.gslblImageNamePrefix.Size = new System.Drawing.Size(107, 12); this.gslblImageNamePrefix.TabIndex = 18; this.gslblImageNamePrefix.Text = "Image Name Prefix"; // + // gslblZone + // + this.gslblZone.AutoSize = true; + this.gslblZone.Location = new System.Drawing.Point(18, 48); + this.gslblZone.Name = "gslblZone"; + this.gslblZone.Size = new System.Drawing.Size(29, 12); + this.gslblZone.TabIndex = 22; + this.gslblZone.Text = "Zone"; + // + // gscobZone + // + this.gscobZone.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.gscobZone.FormattingEnabled = true; + this.gscobZone.Location = new System.Drawing.Point(137, 48); + this.gscobZone.Name = "gscobZone"; + this.gscobZone.PropertyName = "Zone"; + this.gscobZone.SectionName = "Qiniu"; + this.gscobZone.Size = new System.Drawing.Size(241, 20); + this.gscobZone.TabIndex = 23; + // // SettingsForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(396, 243); + this.ClientSize = new System.Drawing.Size(396, 283); + this.Controls.Add(this.gscobZone); + this.Controls.Add(this.gslblZone); this.Controls.Add(this.gstbImageNamePrefix); this.Controls.Add(this.gslblImageNamePrefix); this.Controls.Add(this.gstbDefaultDomain); @@ -225,5 +249,7 @@ private GreenshotPlugin.Controls.GreenshotLabel gslblDefaultDomain; private GreenshotPlugin.Controls.GreenshotTextBox gstbImageNamePrefix; private GreenshotPlugin.Controls.GreenshotLabel gslblImageNamePrefix; + private GreenshotPlugin.Controls.GreenshotLabel gslblZone; + private GreenshotPlugin.Controls.GreenshotComboBox gscobZone; } } \ No newline at end of file diff --git a/GreenshotQiniuPlugin/GreenshotQiniuPlugin.csproj b/GreenshotQiniuPlugin/GreenshotQiniuPlugin.csproj index 2c4b04e99..b236186dc 100644 --- a/GreenshotQiniuPlugin/GreenshotQiniuPlugin.csproj +++ b/GreenshotQiniuPlugin/GreenshotQiniuPlugin.csproj @@ -28,6 +28,7 @@ TRACE prompt 4 + Off @@ -37,12 +38,11 @@ ..\Greenshot\Lib\log4net.dll - - False - ..\..\csharp-sdk\src\packages\Newtonsoft.Json.10.0.2\lib\net20\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll - - ..\..\csharp-sdk\bin\Qiniu.dll + + ..\packages\Qiniu.7.3.0\lib\Net40\Qiniu.dll @@ -92,28 +92,31 @@ + -if exist "$(ProjectDir)QiniuCredentials.private.cs" ( - rename "$(ProjectDir)QiniuCredentials.cs" "QiniuCredentials.orig.cs" - rename "$(ProjectDir)QiniuCredentials.private.cs" "QiniuCredentials.cs" -) - + - -if exist "$(ProjectDir)QiniuCredentials.orig.cs" ( - rename "$(ProjectDir)QiniuCredentials.cs" "QiniuCredentials.private.cs" - rename "$(ProjectDir)QiniuCredentials.orig.cs" "QiniuCredentials.cs" + mkdir "$(SolutionDir)Greenshot\bin\$(Configuration)\Plugins\$(ProjectName)" +"$(SolutionDir)Greenshot\bin\$(Configuration)\Plugins\$(ProjectName)\*.gsp" +del /q /s "$(SolutionDir)Greenshot\bin\$(Configuration)\Plugins\$(ProjectName)"\* + +if "$(ConfigurationName)" == "Release" ( + ..\..\..\packages\LibZ.Tool.1.2.0.0\tools\libz.exe inject-dll --assembly $(ProjectDir)bin\$(Configuration)\$(TargetFileName) --include $(ProjectDir)bin\$(Configuration)\Qiniu.dll --include $(ProjectDir)bin\$(Configuration)\Newtonsoft.Json.dll --move ) -mkdir "$(SolutionDir)Greenshot\bin\$(Configuration)\Plugins\$(ProjectName)" copy "$(ProjectDir)bin\$(Configuration)\$(TargetFileName)" "$(SolutionDir)Greenshot\bin\$(Configuration)\Plugins\$(ProjectName)\*.gsp" -copy "$(ProjectDir)bin\$(Configuration)\$(ProjectName).pdb" "$(SolutionDir)Greenshot\bin\$(Configuration)\Plugins\$(ProjectName)\" + +if "$(ConfigurationName)" == "Debug" ( + copy "$(ProjectDir)bin\$(Configuration)\Qiniu.dll" "$(SolutionDir)Greenshot\bin\$(Configuration)\Plugins\$(ProjectName)\" + copy "$(ProjectDir)bin\$(Configuration)\Newtonsoft.Json.dll" "$(SolutionDir)Greenshot\bin\$(Configuration)\Plugins\$(ProjectName)\" + copy "$(ProjectDir)bin\$(Configuration)\$(ProjectName).pdb" "$(SolutionDir)Greenshot\bin\$(Configuration)\Plugins\$(ProjectName)\" +) + mkdir "$(SolutionDir)Greenshot\bin\$(Configuration)\Languages\Plugins\$(ProjectName)" -copy "$(ProjectDir)\Languages\*.xml" "$(SolutionDir)Greenshot\bin\$(Configuration)\Languages\Plugins\$(ProjectName)\" - +copy "$(ProjectDir)\Languages\*.xml" "$(SolutionDir)Greenshot\bin\$(Configuration)\Languages\Plugins\$(ProjectName)\" \ No newline at end of file diff --git a/GreenshotQiniuPlugin/QiniuConfiguration.cs b/GreenshotQiniuPlugin/QiniuConfiguration.cs index 6ca5e0efa..781f16584 100644 --- a/GreenshotQiniuPlugin/QiniuConfiguration.cs +++ b/GreenshotQiniuPlugin/QiniuConfiguration.cs @@ -36,7 +36,19 @@ namespace GreenshotQiniuPlugin public int UploadJpegQuality { get; set; } [IniProperty("UploadReduceColors", Description = "Reduce color amount of the uploaded image to 256", DefaultValue = "False")] public bool UploadReduceColors { get; set; } - + + /*public static Zone ZONE_CN_East; + public static Zone ZONE_CN_North; + public static Zone ZONE_CN_South; + public static Zone ZONE_US_North; + */ + + public enum UploadZone + { + CN_North, CN_East, CN_South, US_North + } + [IniProperty("Zone", Description = "Zone used for uploading", DefaultValue = "CN_North")] + public UploadZone Zone { get; set; } [IniProperty("AccessKey", Description = "access key used for uploading", DefaultValue = "")] public string AccessKey { get; set; } diff --git a/GreenshotQiniuPlugin/QiniuPlugin.cs b/GreenshotQiniuPlugin/QiniuPlugin.cs index bbe3e0050..cd111f2f9 100644 --- a/GreenshotQiniuPlugin/QiniuPlugin.cs +++ b/GreenshotQiniuPlugin/QiniuPlugin.cs @@ -28,9 +28,7 @@ using Greenshot.Plugin; using GreenshotPlugin.Controls; using GreenshotPlugin.Core; -using Qiniu.Common; -using Qiniu.IO; -using Qiniu.IO.Model; + using Qiniu.Http; using Qiniu.Util; @@ -147,21 +145,23 @@ namespace GreenshotQiniuPlugin // public static void Save(ISurface surface, string fullPath, bool allowOverwrite, SurfaceOutputSettings outputSettings, bool copyPathToClipboard) // Run upload in the background - ImageOutput.Save(surfaceToUpload,fullPath,true,outputSettings,true); + MemoryStream streamoutput = new MemoryStream(); + ImageOutput.SaveToStream(surfaceToUpload, streamoutput, outputSettings); new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("qiniu", LangKey.communication_wait), delegate { - HttpResult result = QiniuUtils.UploadFile(fullPath,filename); - + HttpResult result = QiniuUtils.UploadFile(streamoutput, filename); } ); // This causes an exeption if the upload failed :) //Log.DebugFormat("Uploaded to qiniu page: " + qiniuInfo.Page); - uploadUrl = "![]("+_config.DefaultDomain + filename+")"; + uploadUrl = _config.DefaultDomain + filename; + + string markdownURL = "![]("+uploadUrl+")"; - Clipboard.SetText(uploadUrl); + Clipboard.SetText(markdownURL); diff --git a/GreenshotQiniuPlugin/QiniuUtils.cs b/GreenshotQiniuPlugin/QiniuUtils.cs index ce4564634..8c6f4b230 100644 --- a/GreenshotQiniuPlugin/QiniuUtils.cs +++ b/GreenshotQiniuPlugin/QiniuUtils.cs @@ -28,11 +28,9 @@ using Greenshot.IniFile; using Greenshot.Plugin; using GreenshotPlugin.Core; -using Qiniu.Common; -using Qiniu.IO; -using Qiniu.IO.Model; using Qiniu.Http; using Qiniu.Util; +using Qiniu.Storage; @@ -196,7 +194,7 @@ namespace GreenshotQiniuPlugin } - public static HttpResult UploadFile(string fullPath,string fileName) + public static HttpResult UploadFile(Stream filestream,string fileName) { Mac mac = new Mac(_config.AccessKey, _config.SecretKey); PutPolicy putPolicy = new PutPolicy(); @@ -204,16 +202,38 @@ namespace GreenshotQiniuPlugin putPolicy.SetExpires(3600); //putPolicy.DeleteAfterDays = 1; string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); - - ResumableUploader target = new ResumableUploader(); - HttpResult result = target.UploadFile(fullPath, fileName, token); + + Config config = new Config(); + // 设置上传区域 + switch (_config.Zone) + { + case QiniuConfiguration.UploadZone.CN_East: + config.Zone = Zone.ZONE_CN_East; + break; + case QiniuConfiguration.UploadZone.CN_North: + config.Zone = Zone.ZONE_CN_North; + break; + case QiniuConfiguration.UploadZone.CN_South: + config.Zone = Zone.ZONE_CN_South; + break; + case QiniuConfiguration.UploadZone.US_North: + config.Zone = Zone.ZONE_US_North; + break; + } + // 设置 http 或者 https 上传 + config.UseHttps = true; + config.UseCdnDomains = true; + config.ChunkSize = ChunkUnit.U512K; + + ResumableUploader target = new ResumableUploader(config); + HttpResult result = target.UploadStream(filestream, fileName, token,null); + if (result.Code != (int)HttpCode.OK) { - File.Delete(fullPath); Log.Error(result.Text); throw new Exception(result.Text); } - File.Delete(fullPath); + return result; } diff --git a/GreenshotQiniuPlugin/packages.config b/GreenshotQiniuPlugin/packages.config new file mode 100644 index 000000000..acb980f42 --- /dev/null +++ b/GreenshotQiniuPlugin/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file