OssHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Threading;
  3. using System.IO;
  4. using Aliyun.OSS;
  5. using Library;
  6. using System.Text.RegularExpressions;
  7. namespace MySystem
  8. {
  9. public class OssHelper
  10. {
  11. public readonly static OssHelper Instance = new OssHelper();
  12. private OssHelper()
  13. {
  14. }
  15. public void Start()//启动
  16. {
  17. Thread thread = new Thread(threadStart);
  18. thread.IsBackground = true;
  19. thread.Start();
  20. }
  21. public void Add(string data)
  22. {
  23. function.WritePage("/OSSTempFiles/", function.MD532(Guid.NewGuid().ToString()) + ".txt", data);
  24. }
  25. private void threadStart()
  26. {
  27. var client = new OssClient(AppConfig.Oss.endpoint, AppConfig.Oss.key, AppConfig.Oss.secret);
  28. while (true)
  29. {
  30. string dataFilePath = function.getPath("/OSSTempFiles/");
  31. DirectoryInfo TheFolder = new DirectoryInfo(dataFilePath);
  32. if (TheFolder.Exists)
  33. {
  34. //遍历文件
  35. if (TheFolder.GetFiles().Length > 0)
  36. {
  37. try
  38. {
  39. foreach (FileInfo NextFile in TheFolder.GetFiles())
  40. {
  41. string FileFullName = dataFilePath + NextFile.Name;
  42. string data = function.ReadInstance("/OSSTempFiles/" + NextFile.Name);
  43. ScanQueue(data, FileFullName, client);
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. function.WriteLog(ex.ToString(), "OSS上传队列异常");
  49. }
  50. }
  51. }
  52. //没有任务,休息1秒钟
  53. Thread.Sleep(1000);
  54. }
  55. }
  56. //要执行的方法
  57. public void ScanQueue(string data, string FileFullName)
  58. {
  59. var client = new OssClient(AppConfig.Oss.endpoint, AppConfig.Oss.key, AppConfig.Oss.secret);
  60. ScanQueue(data, FileFullName, client);
  61. }
  62. private void ScanQueue(string data, string FileFullName, OssClient client)
  63. {
  64. // 上传文件。
  65. string localFile = function.getPath(data);
  66. string filePath = data.TrimStart('/');
  67. if (!data.StartsWith(AppConfig.Oss.PathName))
  68. {
  69. filePath = AppConfig.Oss.PathName + data;
  70. }
  71. var result = client.PutObject(AppConfig.Oss.bucketName, filePath, localFile, new ObjectMetadata()
  72. {
  73. ExpirationTime = DateTime.Parse("2050-12-31 23:59:59")
  74. });
  75. if (!string.IsNullOrEmpty(result.ETag))
  76. {
  77. if (result.ETag.Length == 32)
  78. {
  79. if (File.Exists(localFile))
  80. {
  81. File.Delete(localFile);
  82. }
  83. }
  84. }
  85. if (File.Exists(FileFullName))
  86. {
  87. File.Delete(FileFullName);
  88. }
  89. }
  90. /// <summary>
  91. /// 解析编辑器中的图片(oss)
  92. /// </summary>
  93. /// <param name="content"></param>
  94. /// <returns></returns>
  95. public string CheckOSSPic(string content)
  96. {
  97. if (string.IsNullOrEmpty(content))
  98. {
  99. content = "";
  100. }
  101. if (AppConfig.Oss.OssStatus)
  102. {
  103. MatchCollection MC = Regex.Matches(content, "<img.*?>");
  104. foreach (Match match in MC)
  105. {
  106. string imgstr = match.Value;
  107. Match submatch = Regex.Match(imgstr, "src=\".*?\"");
  108. if (submatch.Success)
  109. {
  110. string src = submatch.Value.Replace("src=\"", "").Replace("\"", "");
  111. if (!src.StartsWith("http"))
  112. {
  113. content = content.Replace(src, AppConfig.Oss.SourceHost + src);
  114. }
  115. }
  116. }
  117. }
  118. return content;
  119. }
  120. }
  121. }