| 
					
				 | 
			
			
				@@ -1,5 +1,4 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-using Aliyun.OSS; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-using Hangfire; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+using Hangfire; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 using Masuit.MyBlogs.Core.Configs; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 using Masuit.Tools; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 using Masuit.Tools.Html; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -11,7 +10,6 @@ using System; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 using System.Collections.Generic; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 using System.IO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 using System.Linq; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-using System.Net; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 using System.Net.Http; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 using System.Net.Http.Headers; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 using System.Threading; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -39,11 +37,6 @@ namespace Masuit.MyBlogs.Core.Common 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             _httpClient = httpClient; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /// <summary> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /// OSS客户端 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /// </summary> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        public static OssClient OssClient { get; set; } = new(AppConfig.AliOssConfig.EndPoint, AppConfig.AliOssConfig.AccessKeyId, AppConfig.AliOssConfig.AccessKeySecret); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// <summary> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// 上传图片 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// </summary> 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -58,13 +51,7 @@ namespace Masuit.MyBlogs.Core.Common 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             file = Path.GetFileName(file); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            var fallbackPolicy = Policy<(string url, bool success)>.Handle<Exception>().FallbackAsync(async _ => 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                await Task.CompletedTask; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return UploadOss(stream, file); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            var retryPolicy = Policy<(string url, bool success)>.Handle<Exception>().RetryAsync(3); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return await fallbackPolicy.WrapAsync(retryPolicy).ExecuteAsync(() => UploadGitlab(stream, file, cancellationToken)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return await Policy<(string url, bool success)>.Handle<Exception>().RetryAsync(3).ExecuteAsync(() => UploadGit(stream, file, cancellationToken)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         private readonly List<string> _failedList = new(); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -75,7 +62,7 @@ namespace Masuit.MyBlogs.Core.Common 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// <param name="stream"></param> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// <param name="file"></param> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// <returns></returns> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        private async Task<(string url, bool success)> UploadGitlab(Stream stream, string file, CancellationToken cancellationToken) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        private Task<(string url, bool success)> UploadGit(Stream stream, string file, CancellationToken cancellationToken) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             var gitlabs = AppConfig.GitlabConfigs.Where(c => c.FileLimitSize >= stream.Length && !_failedList.Contains(c.ApiUrl)).OrderBy(c => Guid.NewGuid()).ToList(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             if (gitlabs.Count > 0) 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -83,45 +70,18 @@ namespace Masuit.MyBlogs.Core.Common 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 var gitlab = gitlabs[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 if (gitlab.ApiUrl.Contains("gitee.com")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    return await UploadGitee(gitlab, stream, file, cancellationToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return UploadGitee(gitlab, stream, file, cancellationToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 if (gitlab.ApiUrl.Contains("api.github.com")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    return await UploadGithub(gitlab, stream, file, cancellationToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return UploadGithub(gitlab, stream, file, cancellationToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                var path = $"{DateTime.Now:yyyy\\/MM\\/dd}/{file}"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                _httpClient.DefaultRequestHeaders.Add("PRIVATE-TOKEN", gitlab.AccessToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return await _httpClient.PostAsJsonAsync(gitlab.ApiUrl.Contains("/v3/") ? gitlab.ApiUrl : gitlab.ApiUrl + HttpUtility.UrlEncode(path), new 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    file_path = path, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    branch_name = gitlab.Branch, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    branch = gitlab.Branch, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    author_email = CommonHelper.SystemSettings["ReceiveEmail"], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    author_name = SnowFlake.NewId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    encoding = "base64", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    content = Convert.ToBase64String(stream.ToArray()), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    commit_message = SnowFlake.NewId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                }, cancellationToken).ContinueWith(t => 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    if (t.IsCompletedSuccessfully) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        using var resp = t.Result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        using var content = resp.Content; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        if (resp.IsSuccessStatusCode || content.ReadAsStringAsync().Result.Contains("already exists")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            return (gitlab.RawUrl + path, true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    LogManager.Info($"图片上传到gitlab({gitlab.ApiUrl})失败。"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    _failedList.Add(gitlab.ApiUrl); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    throw t.Exception ?? new Exception(t.Result.ReasonPhrase); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return UploadGitlab(gitlab, stream, file, cancellationToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return UploadOss(stream, file); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return Task.FromResult<(string url, bool success)>((null, false)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// <summary> 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -195,25 +155,41 @@ namespace Masuit.MyBlogs.Core.Common 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// <summary> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        /// 阿里云Oss图床 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        /// github图床 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// </summary> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        /// <param name="config"></param> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// <param name="stream"></param> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// <param name="file"></param> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /// <returns></returns> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        private (string url, bool success) UploadOss(Stream stream, string file) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        private Task<(string url, bool success)> UploadGitlab(GitlabConfig config, Stream stream, string file, CancellationToken cancellationToken) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!AppConfig.AliOssConfig.Enabled) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            var path = $"{DateTime.Now:yyyy\\/MM\\/dd}/{file}"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            _httpClient.DefaultRequestHeaders.Add("PRIVATE-TOKEN", config.AccessToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return _httpClient.PostAsJsonAsync(config.ApiUrl.Contains("/v3/") ? config.ApiUrl : config.ApiUrl + HttpUtility.UrlEncode(path), new 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return (null, false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            var objectName = $"{DateTime.Now:yyyy\\/MM\\/dd}/{file}"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            var fallbackPolicy = Policy<(string url, bool success)>.Handle<Exception>().Fallback(_ => (null, false)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            var retryPolicy = Policy<(string url, bool success)>.Handle<Exception>().Retry(3); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return fallbackPolicy.Wrap(retryPolicy).Execute(() => 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                file_path = path, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                branch_name = config.Branch, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                branch = config.Branch, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                author_email = CommonHelper.SystemSettings["ReceiveEmail"], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                author_name = SnowFlake.NewId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                encoding = "base64", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                content = Convert.ToBase64String(stream.ToArray()), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                commit_message = SnowFlake.NewId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }, cancellationToken).ContinueWith(t => 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                var result = OssClient.PutObject(AppConfig.AliOssConfig.BucketName, objectName, stream); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return result.HttpStatusCode == HttpStatusCode.OK ? (AppConfig.AliOssConfig.BucketDomain + "/" + objectName, true) : throw new Exception("上传oss失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (t.IsCompletedSuccessfully) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    using var resp = t.Result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    using var content = resp.Content; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if (resp.IsSuccessStatusCode || content.ReadAsStringAsync().Result.Contains("already exists")) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        return (config.RawUrl + path, true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                LogManager.Info($"图片上传到gitlab({config.ApiUrl})失败。"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                _failedList.Add(config.ApiUrl); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw t.Exception ?? new Exception(t.Result.ReasonPhrase); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 |