|
@@ -1,23 +1,12 @@
|
|
|
-using Aliyun.OSS;
|
|
|
|
|
-using Hangfire;
|
|
|
|
|
-using IP2Region;
|
|
|
|
|
-using Masuit.MyBlogs.Core.Configs;
|
|
|
|
|
|
|
+using IP2Region;
|
|
|
using Masuit.Tools;
|
|
using Masuit.Tools;
|
|
|
-using Masuit.Tools.Html;
|
|
|
|
|
-using Masuit.Tools.Systems;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.Net.Http.Headers;
|
|
using Microsoft.Net.Http.Headers;
|
|
|
-using Newtonsoft.Json.Linq;
|
|
|
|
|
-using Polly;
|
|
|
|
|
using System;
|
|
using System;
|
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Concurrent;
|
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
|
-using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
using System.Linq;
|
|
|
-using System.Net;
|
|
|
|
|
-using System.Net.Http;
|
|
|
|
|
-using System.Net.Http.Headers;
|
|
|
|
|
#if !DEBUG
|
|
#if !DEBUG
|
|
|
using Masuit.MyBlogs.Core.Models.ViewModel;
|
|
using Masuit.MyBlogs.Core.Models.ViewModel;
|
|
|
using Masuit.Tools.Models;
|
|
using Masuit.Tools.Models;
|
|
@@ -186,274 +175,6 @@ namespace Masuit.MyBlogs.Core.Common
|
|
|
#endif
|
|
#endif
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- #region 图床相关
|
|
|
|
|
-
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// OSS客户端
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- public static OssClient OssClient { get; set; } = new OssClient(AppConfig.AliOssConfig.EndPoint, AppConfig.AliOssConfig.AccessKeyId, AppConfig.AliOssConfig.AccessKeySecret);
|
|
|
|
|
-
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// 上传图片
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="file"></param>
|
|
|
|
|
- /// <returns></returns>
|
|
|
|
|
- public static (string url, bool success) UploadImage(string file)
|
|
|
|
|
- {
|
|
|
|
|
- if (AppConfig.GiteeConfig.Enabled)
|
|
|
|
|
- {
|
|
|
|
|
- return UploadGitee(file);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (AppConfig.GitlabConfig.Enabled)
|
|
|
|
|
- {
|
|
|
|
|
- return UploadGitlab(file);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (AppConfig.AliOssConfig.Enabled)
|
|
|
|
|
- {
|
|
|
|
|
- return UploadOss(file);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return UploadSmms(file);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// 码云图床
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="file"></param>
|
|
|
|
|
- /// <returns></returns>
|
|
|
|
|
- public static (string url, bool success) UploadGitee(string file)
|
|
|
|
|
- {
|
|
|
|
|
- if (!file.Contains(new[] { ".jpg", ".jpeg", ".gif", ".png", ".bmp" }))
|
|
|
|
|
- {
|
|
|
|
|
- return (null, false);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- using (Image image = Image.FromFile(file))
|
|
|
|
|
- {
|
|
|
|
|
- using (MemoryStream m = new MemoryStream())
|
|
|
|
|
- {
|
|
|
|
|
- image.Save(m, image.RawFormat);
|
|
|
|
|
- string base64String = Convert.ToBase64String(m.ToArray());
|
|
|
|
|
- using (HttpClient httpClient = new HttpClient())
|
|
|
|
|
- {
|
|
|
|
|
- string path = $"{DateTime.Now:yyyyMMdd}/{Path.GetFileName(file)}";
|
|
|
|
|
- using (var resp = httpClient.PostAsJsonAsync(AppConfig.GiteeConfig.ApiUrl + path, new
|
|
|
|
|
- {
|
|
|
|
|
- access_token = AppConfig.GiteeConfig.AccessToken,
|
|
|
|
|
- content = base64String,
|
|
|
|
|
- message = "上传一张图片"
|
|
|
|
|
- }).Result)
|
|
|
|
|
- {
|
|
|
|
|
- if (resp.IsSuccessStatusCode || resp.Content.ReadAsStringAsync().Result.Contains("already exists"))
|
|
|
|
|
- {
|
|
|
|
|
- return (AppConfig.GiteeConfig.RawUrl + path, true);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return UploadSmms(file);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// gitlab图床
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="file"></param>
|
|
|
|
|
- /// <returns></returns>
|
|
|
|
|
- public static (string url, bool success) UploadGitlab(string file)
|
|
|
|
|
- {
|
|
|
|
|
- if (!file.Contains(new[] { ".jpg", ".jpeg", ".gif", ".png", ".bmp" }))
|
|
|
|
|
- {
|
|
|
|
|
- return (null, false);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- using (Image image = Image.FromFile(file))
|
|
|
|
|
- {
|
|
|
|
|
- using (MemoryStream m = new MemoryStream())
|
|
|
|
|
- {
|
|
|
|
|
- image.Save(m, image.RawFormat);
|
|
|
|
|
- string base64String = Convert.ToBase64String(m.ToArray());
|
|
|
|
|
- using (HttpClient httpClient = new HttpClient())
|
|
|
|
|
- {
|
|
|
|
|
- httpClient.DefaultRequestHeaders.Add("PRIVATE-TOKEN", AppConfig.GitlabConfig.AccessToken);
|
|
|
|
|
- string path = $"{DateTime.Now:yyyyMMdd}/{Path.GetFileName(file)}";
|
|
|
|
|
- using (var resp = httpClient.PostAsJsonAsync(AppConfig.GitlabConfig.ApiUrl + path, new
|
|
|
|
|
- {
|
|
|
|
|
- branch = AppConfig.GitlabConfig.Branch,
|
|
|
|
|
- author_email = "[email protected]",
|
|
|
|
|
- author_name = "ldqk",
|
|
|
|
|
- encoding = "base64",
|
|
|
|
|
- content = base64String,
|
|
|
|
|
- commit_message = "上传一张图片"
|
|
|
|
|
- }).Result)
|
|
|
|
|
- {
|
|
|
|
|
- if (resp.IsSuccessStatusCode || resp.Content.ReadAsStringAsync().Result.Contains("already exists"))
|
|
|
|
|
- {
|
|
|
|
|
- return (AppConfig.GitlabConfig.RawUrl + path, true);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return UploadSmms(file);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// 阿里云Oss图床
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="file"></param>
|
|
|
|
|
- /// <returns></returns>
|
|
|
|
|
- public static (string url, bool success) UploadOss(string file)
|
|
|
|
|
- {
|
|
|
|
|
- var objectName = DateTime.Now.ToString("yyyyMMdd") + "/" + SnowFlake.NewId + Path.GetExtension(file);
|
|
|
|
|
- var result = Policy.Handle<Exception>().Retry(5, (e, i) =>
|
|
|
|
|
- {
|
|
|
|
|
- Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
- Console.WriteLine(e.Message);
|
|
|
|
|
- Console.ResetColor();
|
|
|
|
|
- }).Execute(() => OssClient.PutObject(AppConfig.AliOssConfig.BucketName, objectName, file));
|
|
|
|
|
- return result.HttpStatusCode == HttpStatusCode.OK ? (AppConfig.AliOssConfig.BucketDomain + "/" + objectName, true) : UploadSmms(file);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// 上传图片到sm图床
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <returns></returns>
|
|
|
|
|
- public static (string url, bool success) UploadSmms(string file)
|
|
|
|
|
- {
|
|
|
|
|
- string url = string.Empty;
|
|
|
|
|
- bool success = false;
|
|
|
|
|
- using (var fs = File.OpenRead(file))
|
|
|
|
|
- {
|
|
|
|
|
- using (HttpClient httpClient = new HttpClient())
|
|
|
|
|
- {
|
|
|
|
|
- httpClient.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue.Parse("Mozilla/5.0"));
|
|
|
|
|
- using (var bc = new StreamContent(fs))
|
|
|
|
|
- {
|
|
|
|
|
- bc.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
|
|
|
|
|
- {
|
|
|
|
|
- FileName = Path.GetFileName(file),
|
|
|
|
|
- Name = "smfile"
|
|
|
|
|
- };
|
|
|
|
|
- using (var content = new MultipartFormDataContent { bc })
|
|
|
|
|
- {
|
|
|
|
|
- var code = httpClient.PostAsync("https://sm.ms/api/upload?inajax=1&ssl=1", content).ContinueWith(t =>
|
|
|
|
|
- {
|
|
|
|
|
- if (t.IsCanceled || t.IsFaulted)
|
|
|
|
|
- {
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- var res = t.Result;
|
|
|
|
|
- if (res.IsSuccessStatusCode)
|
|
|
|
|
- {
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- string s = res.Content.ReadAsStringAsync().Result;
|
|
|
|
|
- var token = JObject.Parse(s);
|
|
|
|
|
- url = (string)token["data"]["url"];
|
|
|
|
|
- return 1;
|
|
|
|
|
- }
|
|
|
|
|
- catch
|
|
|
|
|
- {
|
|
|
|
|
- return 2;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return 2;
|
|
|
|
|
- }).Result;
|
|
|
|
|
- if (code == 1)
|
|
|
|
|
- {
|
|
|
|
|
- success = true;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return success ? (url, true) : UploadPeople(file);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// 上传图片到人民网图床
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="file"></param>
|
|
|
|
|
- /// <returns></returns>
|
|
|
|
|
- public static (string url, bool success) UploadPeople(string file)
|
|
|
|
|
- {
|
|
|
|
|
- bool success = false;
|
|
|
|
|
- using (var httpClient = new HttpClient())
|
|
|
|
|
- {
|
|
|
|
|
- httpClient.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue.Parse("Chrome/72.0.3626.96"));
|
|
|
|
|
- using (var stream = File.OpenRead(file))
|
|
|
|
|
- {
|
|
|
|
|
- using (var sc = new StreamContent(stream))
|
|
|
|
|
- {
|
|
|
|
|
- using (var mc = new MultipartFormDataContent
|
|
|
|
|
- {
|
|
|
|
|
- { sc, "Filedata", Path.GetFileName(file) },
|
|
|
|
|
- {new StringContent("."+Path.GetExtension(file)),"filetype"}
|
|
|
|
|
- })
|
|
|
|
|
- {
|
|
|
|
|
- var str = httpClient.PostAsync("http://bbs1.people.com.cn/postImageUpload.do", mc).ContinueWith(t =>
|
|
|
|
|
- {
|
|
|
|
|
- if (t.IsCompletedSuccessfully)
|
|
|
|
|
- {
|
|
|
|
|
- var res = t.Result;
|
|
|
|
|
- if (res.IsSuccessStatusCode)
|
|
|
|
|
- {
|
|
|
|
|
- string result = res.Content.ReadAsStringAsync().Result;
|
|
|
|
|
- string url = "http://bbs1.people.com.cn" + (string)JObject.Parse(result)["imageUrl"];
|
|
|
|
|
- if (url.EndsWith(Path.GetExtension(file)))
|
|
|
|
|
- {
|
|
|
|
|
- success = true;
|
|
|
|
|
- return url;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return "";
|
|
|
|
|
- }).Result;
|
|
|
|
|
- return (str, success);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- #endregion
|
|
|
|
|
- /// <summary>
|
|
|
|
|
- /// 替换img标签的src属性
|
|
|
|
|
- /// </summary>
|
|
|
|
|
- /// <param name="content"></param>
|
|
|
|
|
- /// <returns></returns>
|
|
|
|
|
- public static string ReplaceImgSrc(string content)
|
|
|
|
|
- {
|
|
|
|
|
- var srcs = content.MatchImgSrcs();
|
|
|
|
|
- foreach (string src in srcs)
|
|
|
|
|
- {
|
|
|
|
|
- if (!src.StartsWith("http"))
|
|
|
|
|
- {
|
|
|
|
|
- var path = Path.Combine(AppContext.BaseDirectory + "wwwroot", src.Replace("/", @"\").Substring(1));
|
|
|
|
|
- if (File.Exists(path))
|
|
|
|
|
- {
|
|
|
|
|
- var (url, success) = UploadImage(path);
|
|
|
|
|
- if (success)
|
|
|
|
|
- {
|
|
|
|
|
- content = content.Replace(src, url);
|
|
|
|
|
- BackgroundJob.Enqueue(() => File.Delete(path));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return content;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 是否是机器人访问
|
|
/// 是否是机器人访问
|
|
|
/// </summary>
|
|
/// </summary>
|