OssHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.BsModels;
  10. namespace MySystem
  11. {
  12. public class OssHelper
  13. {
  14. string key = ConfigurationManager.AppSettings["OSSKey"].ToString(); //阿里云AccessKey ID
  15. string secret = ConfigurationManager.AppSettings["OSSSecret"].ToString(); //阿里云Access Key Secret
  16. public string PathName = ConfigurationManager.AppSettings["Database"].ToString();
  17. public string endpoint = ConfigurationManager.AppSettings["OSSEndpoint"].ToString(); //endpoint
  18. public string bucketName = ConfigurationManager.AppSettings["OSSBucketName"].ToString();
  19. public string SourceHost = "";
  20. public bool OssStatus = false;
  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. {
  83. var client = new OssClient(endpoint, key, secret);
  84. ScanQueue(data, FileFullName, client);
  85. }
  86. }
  87. private void ScanQueue(string data, string FileFullName, OssClient client)
  88. {
  89. // 上传文件。
  90. string localFile = function.getPath(data);
  91. string filePath = data.TrimStart('/');
  92. if (!data.StartsWith(PathName))
  93. {
  94. filePath = PathName + data;
  95. }
  96. var result = client.PutObject(bucketName, filePath, localFile, new ObjectMetadata()
  97. {
  98. ExpirationTime = DateTime.Parse("2050-12-31 23:59:59")
  99. });
  100. if (!string.IsNullOrEmpty(result.ETag))
  101. {
  102. if (result.ETag.Length == 32)
  103. {
  104. if (File.Exists(localFile))
  105. {
  106. File.Delete(localFile);
  107. }
  108. }
  109. }
  110. if (File.Exists(FileFullName))
  111. {
  112. File.Delete(FileFullName);
  113. }
  114. }
  115. /// <summary>
  116. /// 解析编辑器中的图片(oss)
  117. /// </summary>
  118. /// <param name="content"></param>
  119. /// <returns></returns>
  120. public string CheckOSSPic(string content)
  121. {
  122. if (string.IsNullOrEmpty(content))
  123. {
  124. content = "";
  125. }
  126. if (OssStatus)
  127. {
  128. MatchCollection MC = Regex.Matches(content, "<img.*?>");
  129. foreach (Match match in MC)
  130. {
  131. string imgstr = match.Value;
  132. Match submatch = Regex.Match(imgstr, "src=\".*?\"");
  133. if (submatch.Success)
  134. {
  135. string src = submatch.Value.Replace("src=\"", "").Replace("\"", "");
  136. if (!src.StartsWith("http"))
  137. {
  138. content = content.Replace(src, OssHelper.Instance.SourceHost + src);
  139. }
  140. }
  141. }
  142. }
  143. return content;
  144. }
  145. }
  146. }