AopUtils.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using Jayrock.Json.Conversion;
  7. namespace Aop.Api.Util
  8. {
  9. /// <summary>
  10. /// AOP系统工具类。
  11. /// </summary>
  12. public abstract class AopUtils
  13. {
  14. /// <summary>
  15. /// 给AOP请求签名。
  16. /// <remarks>建议使用<code>AlipaySignature.RSASign(parameters, privateKeyPem)</code>></remarks>
  17. /// </summary>
  18. /// <param name="parameters">所有字符型的AOP请求参数</param>
  19. /// <param name="privateKeyPem">签名密钥</param>
  20. /// <returns>签名</returns>
  21. public static string SignAopRequest(IDictionary<string, string> parameters, string privateKeyPem,string charset)
  22. {
  23. if (!string.IsNullOrEmpty(privateKeyPem))
  24. return AlipaySignature.RSASign(parameters, privateKeyPem, charset);
  25. else
  26. return "";
  27. }
  28. /// <summary>
  29. /// 清除字典中值为空的项。
  30. /// </summary>
  31. /// <param name="dict">待清除的字典</param>
  32. /// <returns>清除后的字典</returns>
  33. public static IDictionary<string, T> CleanupDictionary<T>(IDictionary<string, T> dict)
  34. {
  35. IDictionary<string, T> newDict = new Dictionary<string, T>(dict.Count);
  36. IEnumerator<KeyValuePair<string, T>> dem = dict.GetEnumerator();
  37. while (dem.MoveNext())
  38. {
  39. string name = dem.Current.Key;
  40. T value = dem.Current.Value;
  41. if (value != null)
  42. {
  43. newDict.Add(name, value);
  44. }
  45. }
  46. return newDict;
  47. }
  48. /// <summary>
  49. /// 获取文件的真实后缀名。目前只支持JPG, GIF, PNG, BMP四种图片文件。
  50. /// </summary>
  51. /// <param name="fileData">文件字节流</param>
  52. /// <returns>JPG, GIF, PNG or null</returns>
  53. public static string GetFileSuffix(byte[] fileData)
  54. {
  55. if (fileData == null || fileData.Length < 10)
  56. {
  57. return null;
  58. }
  59. if (fileData[0] == 'G' && fileData[1] == 'I' && fileData[2] == 'F')
  60. {
  61. return "GIF";
  62. }
  63. else if (fileData[1] == 'P' && fileData[2] == 'N' && fileData[3] == 'G')
  64. {
  65. return "PNG";
  66. }
  67. else if (fileData[6] == 'J' && fileData[7] == 'F' && fileData[8] == 'I' && fileData[9] == 'F')
  68. {
  69. return "JPG";
  70. }
  71. else if (fileData[0] == 'B' && fileData[1] == 'M')
  72. {
  73. return "BMP";
  74. }
  75. else
  76. {
  77. return null;
  78. }
  79. }
  80. /// <summary>
  81. /// 获取文件的真实媒体类型。目前只支持JPG, GIF, PNG, BMP四种图片文件。
  82. /// </summary>
  83. /// <param name="fileData">文件字节流</param>
  84. /// <returns>媒体类型</returns>
  85. public static string GetMimeType(byte[] fileData)
  86. {
  87. string suffix = GetFileSuffix(fileData);
  88. string mimeType;
  89. switch (suffix)
  90. {
  91. case "JPG": mimeType = "image/jpeg"; break;
  92. case "GIF": mimeType = "image/gif"; break;
  93. case "PNG": mimeType = "image/png"; break;
  94. case "BMP": mimeType = "image/bmp"; break;
  95. default: mimeType = "application/octet-stream"; break;
  96. }
  97. return mimeType;
  98. }
  99. /// <summary>
  100. /// 根据文件后缀名获取文件的媒体类型。
  101. /// </summary>
  102. /// <param name="fileName">带后缀的文件名或文件全名</param>
  103. /// <returns>媒体类型</returns>
  104. public static string GetMimeType(string fileName)
  105. {
  106. string mimeType;
  107. fileName = fileName.ToLower();
  108. if (fileName.EndsWith(".bmp", StringComparison.CurrentCulture))
  109. {
  110. mimeType = "image/bmp";
  111. }
  112. else if (fileName.EndsWith(".gif", StringComparison.CurrentCulture))
  113. {
  114. mimeType = "image/gif";
  115. }
  116. else if (fileName.EndsWith(".jpg", StringComparison.CurrentCulture) || fileName.EndsWith(".jpeg", StringComparison.CurrentCulture))
  117. {
  118. mimeType = "image/jpeg";
  119. }
  120. else if (fileName.EndsWith(".png", StringComparison.CurrentCulture))
  121. {
  122. mimeType = "image/png";
  123. }
  124. else
  125. {
  126. mimeType = "application/octet-stream";
  127. }
  128. return mimeType;
  129. }
  130. /// <summary>
  131. /// 根据API名称获取响应根节点名称。
  132. /// </summary>
  133. /// <param name="api">API名称</param>
  134. /// <returns></returns>
  135. public static string GetRootElement(string api)
  136. {
  137. int pos = api.IndexOf(".");
  138. if (pos != -1 && api.Length > pos)
  139. {
  140. api = api.Substring(pos + 1).Replace('.', '_');
  141. }
  142. return api + "_response";
  143. }
  144. public static IDictionary ParseJson(string body)
  145. {
  146. return JsonConvert.Import(body) as IDictionary;
  147. }
  148. }
  149. }