OssTestHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.IO;
  5. using Aliyun.OSS;
  6. using Aliyun.OSS.Common;
  7. using Library;
  8. using System.Text.RegularExpressions;
  9. using MySystem.Models;
  10. namespace MySystem
  11. {
  12. public class OssTestHelper
  13. {
  14. string key = "LTAI5tAp9ASZtq4wYzuXfeer"; //阿里云AccessKey ID
  15. string secret = "N0o6TwowU8r4aeNWoefMnnus9Q2saW"; //阿里云Access Key Secret
  16. public string PathName = ConfigurationManager.AppSettings["Database"].ToString();
  17. // public string endpoint = "oss-cn-chengdu.aliyuncs.com"; //endpoint
  18. public string endpoint = "oss-cn-chengdu-internal.aliyuncs.com"; //endpoint
  19. public string bucketName = "kexiaoshuang";
  20. public string SourceHost = ConfigurationManager.AppSettings["OssHost"].ToString();
  21. public bool OssStatus = true;
  22. public readonly static OssTestHelper Instance = new OssTestHelper();
  23. private OssTestHelper()
  24. {
  25. }
  26. public void Start()//启动
  27. {
  28. Thread thread = new Thread(threadStart);
  29. thread.IsBackground = true;
  30. thread.Start();
  31. }
  32. public void Add(string data)
  33. {
  34. function.WritePage("/OSSTempFiles/", function.MD532(Guid.NewGuid().ToString()) + ".txt", data);
  35. }
  36. private void threadStart()
  37. {
  38. var client = new OssClient(endpoint, key, secret);
  39. while (true)
  40. {
  41. string dataFilePath = function.getPath("/OSSTempFiles/");
  42. DirectoryInfo TheFolder = new DirectoryInfo(dataFilePath);
  43. if (TheFolder.Exists)
  44. {
  45. //遍历文件
  46. if (TheFolder.GetFiles().Length > 0)
  47. {
  48. try
  49. {
  50. foreach (FileInfo NextFile in TheFolder.GetFiles())
  51. {
  52. string FileFullName = dataFilePath + NextFile.Name;
  53. string data = function.ReadInstance("/OSSTempFiles/" + NextFile.Name);
  54. ScanQueue(data, FileFullName, client);
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. function.WriteLog(ex.ToString(), "OSS上传队列异常");
  60. }
  61. }
  62. }
  63. //没有任务,休息1秒钟
  64. Thread.Sleep(1000);
  65. }
  66. }
  67. //要执行的方法
  68. public void ScanQueue(string data, string FileFullName)
  69. {
  70. // SystemSet set = new WebCMSEntities().SystemSet.FirstOrDefault() ?? new SystemSet();
  71. // if (set.UploadOss == 1)
  72. if(OssStatus)
  73. {
  74. var client = new OssClient(endpoint, key, secret);
  75. ScanQueue(data, FileFullName, client);
  76. }
  77. }
  78. private void ScanQueue(string data, string FileFullName, OssClient client)
  79. {
  80. // 上传文件。
  81. string localFile = function.getPath(PathName + data);
  82. string filePath = data.TrimStart('/');
  83. if (!data.StartsWith(PathName))
  84. {
  85. filePath = PathName + data;
  86. }
  87. var result = client.PutObject(bucketName, filePath, localFile, new ObjectMetadata()
  88. {
  89. ExpirationTime = DateTime.Parse("2050-12-31 23:59:59")
  90. });
  91. if (!string.IsNullOrEmpty(result.ETag))
  92. {
  93. if (result.ETag.Length == 32)
  94. {
  95. if (File.Exists(localFile))
  96. {
  97. File.Delete(localFile);
  98. }
  99. }
  100. }
  101. if (File.Exists(FileFullName))
  102. {
  103. File.Delete(FileFullName);
  104. }
  105. }
  106. public string GetUrlBase64(string url)
  107. {
  108. var client = new OssClient(endpoint, key, secret);
  109. var obj = client.GetObject(bucketName, url);
  110. Stream stream = obj.Content;
  111. byte[] bytes;
  112. using (var memoryStream = new MemoryStream())
  113. {
  114. stream.CopyTo(memoryStream);
  115. bytes = memoryStream.ToArray();
  116. }
  117. string base64 = Convert.ToBase64String(bytes);
  118. return base64;
  119. }
  120. /// <summary>
  121. /// 解析编辑器中的图片(oss)
  122. /// </summary>
  123. /// <param name="content"></param>
  124. /// <returns></returns>
  125. public string CheckOSSPic(string content)
  126. {
  127. if (string.IsNullOrEmpty(content))
  128. {
  129. content = "";
  130. }
  131. if (OssStatus)
  132. {
  133. MatchCollection MC = Regex.Matches(content, "<img.*?>");
  134. foreach (Match match in MC)
  135. {
  136. string imgstr = match.Value;
  137. Match submatch = Regex.Match(imgstr, "src=\".*?\"");
  138. if (submatch.Success)
  139. {
  140. string src = submatch.Value.Replace("src=\"", "").Replace("\"", "");
  141. if (!src.StartsWith("http"))
  142. {
  143. content = content.Replace(src, OssHelper.Instance.SourceHost + src);
  144. }
  145. }
  146. }
  147. }
  148. return content;
  149. }
  150. }
  151. }