OssHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 OssHelper
  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 bucketName = "kexiaoshuang";
  19. public string SourceHost = ConfigurationManager.AppSettings["OssHost"].ToString();
  20. public bool OssStatus = true;
  21. public readonly static OssHelper Instance = new OssHelper();
  22. private OssHelper()
  23. {
  24. // SystemSet set = new WebCMSEntities().SystemSet.FirstOrDefault() ?? new SystemSet();
  25. // if (set.UploadOss == 1)
  26. // {
  27. // SourceHost = "http://" + ConfigurationManager.AppSettings["OSSBucketName"].ToString() + "." + ConfigurationManager.AppSettings["OSSEndpoint"].ToString();
  28. // OssStatus = true;
  29. // }
  30. // else
  31. // {
  32. // SourceHost = "";
  33. // OssStatus = false;
  34. // }
  35. }
  36. public void Start()//启动
  37. {
  38. Thread thread = new Thread(threadStart);
  39. thread.IsBackground = true;
  40. thread.Start();
  41. }
  42. public void Add(string data)
  43. {
  44. function.WritePage("/OSSTempFiles/", function.MD532(Guid.NewGuid().ToString()) + ".txt", data);
  45. }
  46. private void threadStart()
  47. {
  48. var client = new OssClient(endpoint, key, secret);
  49. while (true)
  50. {
  51. string dataFilePath = function.getPath("/OSSTempFiles/");
  52. DirectoryInfo TheFolder = new DirectoryInfo(dataFilePath);
  53. if (TheFolder.Exists)
  54. {
  55. //遍历文件
  56. if (TheFolder.GetFiles().Length > 0)
  57. {
  58. try
  59. {
  60. foreach (FileInfo NextFile in TheFolder.GetFiles())
  61. {
  62. string FileFullName = dataFilePath + NextFile.Name;
  63. string data = function.ReadInstance("/OSSTempFiles/" + NextFile.Name);
  64. ScanQueue(data, FileFullName, client);
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. function.WriteLog(ex.ToString(), "OSS上传队列异常");
  70. }
  71. }
  72. }
  73. //没有任务,休息1秒钟
  74. Thread.Sleep(1000);
  75. }
  76. }
  77. //要执行的方法
  78. public void ScanQueue(string data, string FileFullName)
  79. {
  80. // SystemSet set = new WebCMSEntities().SystemSet.FirstOrDefault() ?? new SystemSet();
  81. // if (set.UploadOss == 1)
  82. if(OssStatus)
  83. {
  84. var client = new OssClient(endpoint, key, secret);
  85. ScanQueue(data, FileFullName, client);
  86. }
  87. }
  88. private void ScanQueue(string data, string FileFullName, OssClient client)
  89. {
  90. // 上传文件。
  91. string localFile = function.getPath(PathName + data);
  92. string filePath = data.TrimStart('/');
  93. if (!data.StartsWith(PathName))
  94. {
  95. filePath = PathName + data;
  96. }
  97. var result = client.PutObject(bucketName, filePath, localFile, new ObjectMetadata()
  98. {
  99. ExpirationTime = DateTime.Parse("2050-12-31 23:59:59")
  100. });
  101. if (!string.IsNullOrEmpty(result.ETag))
  102. {
  103. if (result.ETag.Length == 32)
  104. {
  105. if (File.Exists(localFile))
  106. {
  107. File.Delete(localFile);
  108. }
  109. }
  110. }
  111. if (File.Exists(FileFullName))
  112. {
  113. File.Delete(FileFullName);
  114. }
  115. }
  116. /// <summary>
  117. /// 解析编辑器中的图片(oss)
  118. /// </summary>
  119. /// <param name="content"></param>
  120. /// <returns></returns>
  121. public string CheckOSSPic(string content)
  122. {
  123. if (string.IsNullOrEmpty(content))
  124. {
  125. content = "";
  126. }
  127. if (OssStatus)
  128. {
  129. MatchCollection MC = Regex.Matches(content, "<img.*?>");
  130. foreach (Match match in MC)
  131. {
  132. string imgstr = match.Value;
  133. Match submatch = Regex.Match(imgstr, "src=\".*?\"");
  134. if (submatch.Success)
  135. {
  136. string src = submatch.Value.Replace("src=\"", "").Replace("\"", "");
  137. if (!src.StartsWith("http"))
  138. {
  139. content = content.Replace(src, OssHelper.Instance.SourceHost + src);
  140. }
  141. }
  142. }
  143. }
  144. return content;
  145. }
  146. }
  147. }