From 2de30a902e218f64badfb0d92ca4db988b3e9646 Mon Sep 17 00:00:00 2001 From: harlan Date: Wed, 31 May 2017 09:36:55 +0800 Subject: [PATCH] greenshot plugin for qiniu upload part 2 --- .../Forms/SettingsForm.Designer.cs | 87 +++++++--- .../Languages/language_qiniuplugin-en-US.xml | 16 +- .../Languages/language_qiniuplugin-zh-CN.xml | 2 +- GreenshotQiniuPlugin/QiniuConfiguration.cs | 15 +- GreenshotQiniuPlugin/QiniuPlugin.cs | 49 +++--- GreenshotQiniuPlugin/QiniuUtils.cs | 161 +++++++++++++++++- 6 files changed, 255 insertions(+), 75 deletions(-) diff --git a/GreenshotQiniuPlugin/Forms/SettingsForm.Designer.cs b/GreenshotQiniuPlugin/Forms/SettingsForm.Designer.cs index 0249502d0..c7cd7917e 100644 --- a/GreenshotQiniuPlugin/Forms/SettingsForm.Designer.cs +++ b/GreenshotQiniuPlugin/Forms/SettingsForm.Designer.cs @@ -37,15 +37,18 @@ this.gslblScope = new GreenshotPlugin.Controls.GreenshotLabel(); this.buttonCancel = new GreenshotPlugin.Controls.GreenshotButton(); this.buttonOK = new GreenshotPlugin.Controls.GreenshotButton(); - this.checkbox_usepagelink = new GreenshotPlugin.Controls.GreenshotCheckBox(); this.label_upload_format = new GreenshotPlugin.Controls.GreenshotLabel(); this.combobox_uploadimageformat = new GreenshotPlugin.Controls.GreenshotComboBox(); + this.gstbDefaultDomain = new GreenshotPlugin.Controls.GreenshotTextBox(); + this.gslblDefaultDomain = new GreenshotPlugin.Controls.GreenshotLabel(); + this.gstbImageNamePrefix = new GreenshotPlugin.Controls.GreenshotTextBox(); + this.gslblImageNamePrefix = new GreenshotPlugin.Controls.GreenshotLabel(); this.SuspendLayout(); // // gslblAccessKey // this.gslblAccessKey.AutoSize = true; - this.gslblAccessKey.Location = new System.Drawing.Point(18, 85); + this.gslblAccessKey.Location = new System.Drawing.Point(18, 48); this.gslblAccessKey.Name = "gslblAccessKey"; this.gslblAccessKey.Size = new System.Drawing.Size(65, 12); this.gslblAccessKey.TabIndex = 0; @@ -53,26 +56,26 @@ // // gstbAccessKey // - this.gstbAccessKey.Location = new System.Drawing.Point(107, 85); + this.gstbAccessKey.Location = new System.Drawing.Point(137, 48); this.gstbAccessKey.Name = "gstbAccessKey"; this.gstbAccessKey.PropertyName = "AccessKey"; this.gstbAccessKey.SectionName = "Qiniu"; - this.gstbAccessKey.Size = new System.Drawing.Size(261, 21); + this.gstbAccessKey.Size = new System.Drawing.Size(241, 21); this.gstbAccessKey.TabIndex = 1; // // gstbSecretKey // - this.gstbSecretKey.Location = new System.Drawing.Point(107, 116); + this.gstbSecretKey.Location = new System.Drawing.Point(137, 79); this.gstbSecretKey.Name = "gstbSecretKey"; this.gstbSecretKey.PropertyName = "SecretKey"; this.gstbSecretKey.SectionName = "Qiniu"; - this.gstbSecretKey.Size = new System.Drawing.Size(261, 21); + this.gstbSecretKey.Size = new System.Drawing.Size(241, 21); this.gstbSecretKey.TabIndex = 3; // // gslblSecretKey // this.gslblSecretKey.AutoSize = true; - this.gslblSecretKey.Location = new System.Drawing.Point(18, 119); + this.gslblSecretKey.Location = new System.Drawing.Point(18, 82); this.gslblSecretKey.Name = "gslblSecretKey"; this.gslblSecretKey.Size = new System.Drawing.Size(65, 12); this.gslblSecretKey.TabIndex = 2; @@ -80,17 +83,17 @@ // // gstbScope // - this.gstbScope.Location = new System.Drawing.Point(107, 148); + this.gstbScope.Location = new System.Drawing.Point(137, 111); this.gstbScope.Name = "gstbScope"; this.gstbScope.PropertyName = "Scope"; this.gstbScope.SectionName = "Qiniu"; - this.gstbScope.Size = new System.Drawing.Size(261, 21); + this.gstbScope.Size = new System.Drawing.Size(241, 21); this.gstbScope.TabIndex = 5; // // gslblScope // this.gslblScope.AutoSize = true; - this.gslblScope.Location = new System.Drawing.Point(18, 151); + this.gslblScope.Location = new System.Drawing.Point(18, 114); this.gslblScope.Name = "gslblScope"; this.gslblScope.Size = new System.Drawing.Size(35, 12); this.gslblScope.TabIndex = 4; @@ -120,19 +123,6 @@ this.buttonOK.Text = "Ok"; this.buttonOK.UseVisualStyleBackColor = true; // - // checkbox_usepagelink - // - this.checkbox_usepagelink.AutoSize = true; - this.checkbox_usepagelink.LanguageKey = "Qiniu.use_page_link"; - this.checkbox_usepagelink.Location = new System.Drawing.Point(20, 48); - this.checkbox_usepagelink.Name = "checkbox_usepagelink"; - this.checkbox_usepagelink.PropertyName = "UsePageLink"; - this.checkbox_usepagelink.SectionName = "Qiniu"; - this.checkbox_usepagelink.Size = new System.Drawing.Size(312, 16); - this.checkbox_usepagelink.TabIndex = 14; - this.checkbox_usepagelink.Text = "Use page link instead of image link on clipboard"; - this.checkbox_usepagelink.UseVisualStyleBackColor = true; - // // label_upload_format // this.label_upload_format.LanguageKey = "Qiniu.label_upload_format"; @@ -146,19 +136,58 @@ // this.combobox_uploadimageformat.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.combobox_uploadimageformat.FormattingEnabled = true; - this.combobox_uploadimageformat.Location = new System.Drawing.Point(107, 12); + this.combobox_uploadimageformat.Location = new System.Drawing.Point(137, 12); this.combobox_uploadimageformat.Name = "combobox_uploadimageformat"; this.combobox_uploadimageformat.PropertyName = "UploadFormat"; this.combobox_uploadimageformat.SectionName = "Qiniu"; - this.combobox_uploadimageformat.Size = new System.Drawing.Size(261, 20); + this.combobox_uploadimageformat.Size = new System.Drawing.Size(241, 20); this.combobox_uploadimageformat.TabIndex = 13; // + // gstbDefaultDomain + // + this.gstbDefaultDomain.Location = new System.Drawing.Point(137, 144); + this.gstbDefaultDomain.Name = "gstbDefaultDomain"; + this.gstbDefaultDomain.PropertyName = "DefaultDomain"; + this.gstbDefaultDomain.SectionName = "Qiniu"; + this.gstbDefaultDomain.Size = new System.Drawing.Size(241, 21); + this.gstbDefaultDomain.TabIndex = 17; + // + // gslblDefaultDomain + // + this.gslblDefaultDomain.AutoSize = true; + this.gslblDefaultDomain.Location = new System.Drawing.Point(18, 147); + this.gslblDefaultDomain.Name = "gslblDefaultDomain"; + this.gslblDefaultDomain.Size = new System.Drawing.Size(89, 12); + this.gslblDefaultDomain.TabIndex = 16; + this.gslblDefaultDomain.Text = "Default Domain"; + // + // gstbImageNamePrefix + // + this.gstbImageNamePrefix.Location = new System.Drawing.Point(137, 178); + this.gstbImageNamePrefix.Name = "gstbImageNamePrefix"; + this.gstbImageNamePrefix.PropertyName = "ImageNamePrefix"; + this.gstbImageNamePrefix.SectionName = "Qiniu"; + this.gstbImageNamePrefix.Size = new System.Drawing.Size(241, 21); + this.gstbImageNamePrefix.TabIndex = 19; + // + // gslblImageNamePrefix + // + this.gslblImageNamePrefix.AutoSize = true; + this.gslblImageNamePrefix.Location = new System.Drawing.Point(18, 181); + this.gslblImageNamePrefix.Name = "gslblImageNamePrefix"; + this.gslblImageNamePrefix.Size = new System.Drawing.Size(107, 12); + this.gslblImageNamePrefix.TabIndex = 18; + this.gslblImageNamePrefix.Text = "Image Name Prefix"; + // // 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.Controls.Add(this.checkbox_usepagelink); + this.Controls.Add(this.gstbImageNamePrefix); + this.Controls.Add(this.gslblImageNamePrefix); + this.Controls.Add(this.gstbDefaultDomain); + this.Controls.Add(this.gslblDefaultDomain); this.Controls.Add(this.label_upload_format); this.Controls.Add(this.combobox_uploadimageformat); this.Controls.Add(this.buttonCancel); @@ -173,6 +202,7 @@ this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "SettingsForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Qiniu image upload settings"; this.ResumeLayout(false); this.PerformLayout(); @@ -189,8 +219,11 @@ private GreenshotPlugin.Controls.GreenshotLabel gslblScope; private GreenshotPlugin.Controls.GreenshotButton buttonCancel; private GreenshotPlugin.Controls.GreenshotButton buttonOK; - private GreenshotPlugin.Controls.GreenshotCheckBox checkbox_usepagelink; private GreenshotPlugin.Controls.GreenshotLabel label_upload_format; private GreenshotPlugin.Controls.GreenshotComboBox combobox_uploadimageformat; + private GreenshotPlugin.Controls.GreenshotTextBox gstbDefaultDomain; + private GreenshotPlugin.Controls.GreenshotLabel gslblDefaultDomain; + private GreenshotPlugin.Controls.GreenshotTextBox gstbImageNamePrefix; + private GreenshotPlugin.Controls.GreenshotLabel gslblImageNamePrefix; } } \ No newline at end of file diff --git a/GreenshotQiniuPlugin/Languages/language_qiniuplugin-en-US.xml b/GreenshotQiniuPlugin/Languages/language_qiniuplugin-en-US.xml index 28a0458a1..016d50edd 100644 --- a/GreenshotQiniuPlugin/Languages/language_qiniuplugin-en-US.xml +++ b/GreenshotQiniuPlugin/Languages/language_qiniuplugin-en-US.xml @@ -1,11 +1,11 @@  - + Upload to Qiniu - Imgur settings + Qiniu settings Url @@ -17,25 +17,25 @@ Cancel - Successfully uploaded image to Imgur! + Successfully uploaded image to Qiniu! - An error occured while uploading to Imgur: + An error occured while uploading to Qiniu: Image format - Communicating with Imgur. Please wait... + Communicating with Qiniu. Please wait... - Are you sure you want to delete the image {0} from Imgur? + Are you sure you want to delete the image {0} from Qiniu? - Are you sure you want to delete the local Imgur history? + Are you sure you want to delete the local Qiniu history? - Delete Imgur {0} + Delete Qiniu {0} Use anonymous access diff --git a/GreenshotQiniuPlugin/Languages/language_qiniuplugin-zh-CN.xml b/GreenshotQiniuPlugin/Languages/language_qiniuplugin-zh-CN.xml index ac382ba7e..4541dfacf 100644 --- a/GreenshotQiniuPlugin/Languages/language_qiniuplugin-zh-CN.xml +++ b/GreenshotQiniuPlugin/Languages/language_qiniuplugin-zh-CN.xml @@ -1,5 +1,5 @@  - + 使用匿名连接 取消 diff --git a/GreenshotQiniuPlugin/QiniuConfiguration.cs b/GreenshotQiniuPlugin/QiniuConfiguration.cs index b8a04cdae..6ca5e0efa 100644 --- a/GreenshotQiniuPlugin/QiniuConfiguration.cs +++ b/GreenshotQiniuPlugin/QiniuConfiguration.cs @@ -36,13 +36,8 @@ 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; } - [IniProperty("UsePageLink", Description = "Use pagelink instead of direct link on the clipboard", DefaultValue = "False")] - public bool UsePageLink { get; set; } - [IniProperty("ImgQiniuUrl", Description = "Url to Qiniu cloud.", DefaultValue = "")] - public string ImgQiniuUrl { get; set; } - [IniProperty("AccessKey", Description = "access key used for uploading", DefaultValue = "")] public string AccessKey { get; set; } @@ -51,8 +46,14 @@ namespace GreenshotQiniuPlugin [IniProperty("Scope", Description = "scope key used for uploading", DefaultValue = "")] public string Scope { get; set; } - - + + [IniProperty("DefaultDomain", Description = "default domain name of qiniu for specified scope", DefaultValue = "www.qiniu.com")] + public string DefaultDomain { get; set; } + + [IniProperty("ImageNamePrefix", Description = "set Image Name Prefix ", DefaultValue = "prefix_")] + public string ImageNamePrefix { get; set; } + + /// /// A form for username/password /// diff --git a/GreenshotQiniuPlugin/QiniuPlugin.cs b/GreenshotQiniuPlugin/QiniuPlugin.cs index 3f36367a6..f87774b81 100644 --- a/GreenshotQiniuPlugin/QiniuPlugin.cs +++ b/GreenshotQiniuPlugin/QiniuPlugin.cs @@ -87,7 +87,7 @@ namespace GreenshotQiniuPlugin // Get configuration _config = IniConfig.GetIniSection(); _resources = new ComponentResourceManager(typeof(QiniuPlugin)); - string text = Language.GetString("Qiniu", LangKey.configure); + string text = Language.GetString("qiniu", LangKey.configure); _itemPlugInConfig = new ToolStripMenuItem(text) { Tag = _host, @@ -138,10 +138,9 @@ namespace GreenshotQiniuPlugin SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(_config.UploadFormat, _config.UploadJpegQuality, _config.UploadReduceColors); try { - string filename = Path.GetFileName(FilenameHelper.GetFilename(_config.UploadFormat, captureDetails)); - QiniuInfo qiniuInfo = null; - - + string filename = _config.ImageNamePrefix + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + _config.UploadFormat.ToString().ToLower(); + Path.GetFileName(FilenameHelper.GetFilename(_config.UploadFormat, captureDetails)); + string path = Directory.GetCurrentDirectory(); string fullPath = Path.Combine(path, filename); @@ -149,39 +148,29 @@ 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); - //new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("Qiniu", LangKey.communication_wait), - // delegate - // { + new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("qiniu", LangKey.communication_wait), + delegate + { HttpResult result = QiniuUtils.UploadFile(fullPath,filename); - //} - //); + } + ); // This causes an exeption if the upload failed :) - Log.DebugFormat("Uploaded to qiniu page: " + qiniuInfo.Page); - uploadUrl = null; - try - { - if (_config.UsePageLink) - { - uploadUrl = qiniuInfo.Page; - Clipboard.SetText(qiniuInfo.Page); - } - else - { - uploadUrl = qiniuInfo.Original; - Clipboard.SetText(qiniuInfo.Original); - } - } - catch (Exception ex) - { - Log.Error("Can't write to clipboard: ", ex); - } + //Log.DebugFormat("Uploaded to qiniu page: " + qiniuInfo.Page); + + uploadUrl = _config.DefaultDomain + filename; + + + Clipboard.SetText(uploadUrl); + + + return true; } catch (Exception e) { Log.Error(e); - MessageBox.Show(Language.GetString("Qiniu", LangKey.upload_failure) + " " + e.Message); + MessageBox.Show(Language.GetString("qiniu", LangKey.upload_failure) + " " + e.Message); } uploadUrl = null; return false; diff --git a/GreenshotQiniuPlugin/QiniuUtils.cs b/GreenshotQiniuPlugin/QiniuUtils.cs index b055542f9..e2ff75240 100644 --- a/GreenshotQiniuPlugin/QiniuUtils.cs +++ b/GreenshotQiniuPlugin/QiniuUtils.cs @@ -34,6 +34,8 @@ using Qiniu.IO.Model; using Qiniu.Http; using Qiniu.Util; + + namespace GreenshotQiniuPlugin { /// @@ -42,8 +44,157 @@ namespace GreenshotQiniuPlugin public static class QiniuUtils { private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(QiniuUtils)); private static readonly QiniuConfiguration _config = IniConfig.GetIniSection(); + public enum HttpCode + { + #region _PRE_ - + /// + /// 成功 + /// + OK = 200, + + /// + /// 部分OK + /// + PARTLY_OK = 298, + + /// + /// 请求错误 + /// + BAD_REQUEST = 400, + + /// + /// 认证授权失败 + /// + AUTHENTICATION_FAILED = 401, + + /// + /// 拒绝访问 + /// + ACCESS_DENIED = 403, + + /// + /// 资源不存在 + /// + OBJECT_NOT_FOUND = 404, + + /// + /// CRC32校验失败 + /// + CRC32_CHECK_FAILEd = 406, + + /// + /// 上传文件大小超限 + /// + FILE_SIZE_EXCEED = 413, + + /// + /// 镜像回源失败 + /// + PREFETCH_FAILED = 478, + + /// + /// 错误网关 + /// + BAD_GATEWAY = 502, + + /// + /// 服务端不可用 + /// + SERVER_UNAVAILABLE = 503, + + /// + /// 服务端操作超时 + /// + SERVER_TIME_EXCEED = 504, + + /// + /// 单个资源访问频率过高 + /// + TOO_FREQUENT_ACCESS = 573, + + /// + /// 回调失败 + /// + CALLBACK_FAILED = 579, + + /// + /// 服务端操作失败 + /// + SERVER_OPERATION_FAILED = 599, + + /// + /// 资源内容被修改 + /// + CONTENT_MODIFIED = 608, + + /// + /// 文件不存在 + /// + FILE_NOT_EXIST = 612, + + /// + /// 文件已存在 + /// + FILE_EXISTS = 614, + + /// + /// 空间数量已达上限 + /// + BUCKET_COUNT_LIMIT = 630, + + /// + /// 空间或者文件不存在 + /// + BUCKET_NOT_EXIST = 631, + + /// + /// 列举资源(list)使用了非法的marker + /// + INVALID_MARKER = 640, + + /// + /// 在断点续上传过程中,后续上传接收地址不正确或ctx信息已过期。 + /// + CONTEXT_EXPIRED = 701, + + #endregion _PRE_ + + #region _USR_ + + /// + /// 自定义HTTP状态码 (默认值) + /// + USER_UNDEF = -256, + + /// + /// 自定义HTTP状态码 (用户取消) + /// + USER_CANCELED = -255, + + /// + /// 自定义HTTP状态码 (用户暂停) + /// + USER_PAUSED = -254, + + /// + /// 自定义HTTP状态码 (用户继续) + /// + USER_RESUMED = -253, + + /// + /// 自定义HTTP状态码 (需要重试) + /// + USER_NEED_RETRY = -252, + + /// + /// 自定义HTTP状态码 (异常或错误) + /// + USER_EXCEPTION = -252, + + #endregion _USR_ + + } public static HttpResult UploadFile(string fullPath,string fileName) { @@ -56,7 +207,13 @@ namespace GreenshotQiniuPlugin ResumableUploader target = new ResumableUploader(); HttpResult result = target.UploadFile(fullPath, fileName, token); - File.Delete(fullPath); + if (result.Code != (int)HttpCode.OK) + { + File.Delete(fullPath); + Log.Error(result.Text); + throw new Exception(result.Text); + } + File.Delete(fullPath); return result; }