TopFileItem.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. namespace Topsdk.Util
  2. {
  3. /// <summary>
  4. /// 文件元数据。
  5. /// 可以使用以下几种构造方法:
  6. /// 本地路径:new FileItem("C:/temp.jpg");
  7. /// 本地文件:new FileItem(new FileInfo("C:/temp.jpg"));
  8. /// 字节数组:new FileItem("abc.jpg", bytes);
  9. /// 输入流:new FileItem("abc.jpg", stream);
  10. /// </summary>
  11. public class TopFileItem
  12. {
  13. private Contract contract;
  14. /// <summary>
  15. /// 基于本地文件的构造器。
  16. /// </summary>
  17. /// <param name="fileInfo">本地文件</param>
  18. public TopFileItem(FileInfo fileInfo)
  19. {
  20. this.contract = new LocalContract(fileInfo);
  21. }
  22. /// <summary>
  23. /// 基于本地文件全路径的构造器。
  24. /// </summary>
  25. /// <param name="filePath">本地文件全路径</param>
  26. public TopFileItem(string filePath) : this(new FileInfo(filePath))
  27. {
  28. }
  29. /// <summary>
  30. /// 基于文件名和字节数组的构造器。
  31. /// </summary>
  32. /// <param name="fileName">文件名称(服务端持久化字节数组到磁盘时的文件名)</param>
  33. /// <param name="content">文件字节数组</param>
  34. public TopFileItem(string fileName, byte[] content) : this(fileName, content, null)
  35. {
  36. }
  37. /// <summary>
  38. /// 基于文件名、字节数组和媒体类型的构造器。
  39. /// </summary>
  40. /// <param name="fileName">文件名(服务端持久化字节数组到磁盘时的文件名)</param>
  41. /// <param name="content">文件字字节数组</param>
  42. /// <param name="mimeType">媒体类型</param>
  43. public TopFileItem(string fileName, byte[] content, string mimeType)
  44. {
  45. this.contract = new ByteArrayContract(fileName, content, mimeType);
  46. }
  47. /// <summary>
  48. /// 基于文件名和输入流的构造器。
  49. /// </summary>
  50. /// <param name="fileName">文件名称(服务端持久化输入流到磁盘时的文件名)</param>
  51. /// <param name="content">文件输入流</param>
  52. public TopFileItem(string fileName, Stream stream) : this(fileName, stream, null)
  53. {
  54. }
  55. /// <summary>
  56. /// 基于文件名、输入流和媒体类型的构造器。
  57. /// </summary>
  58. /// <param name="fileName">文件名(服务端持久化输入流到磁盘时的文件名)</param>
  59. /// <param name="content">文件输入流</param>
  60. /// <param name="mimeType">媒体类型</param>
  61. public TopFileItem(string fileName, Stream stream, string mimeType)
  62. {
  63. this.contract = new StreamContract(fileName, stream, mimeType);
  64. }
  65. public bool IsValid()
  66. {
  67. return this.contract.IsValid();
  68. }
  69. public long GetFileLength()
  70. {
  71. return this.contract.GetFileLength();
  72. }
  73. public string GetFileName()
  74. {
  75. return this.contract.GetFileName();
  76. }
  77. public string GetMimeType()
  78. {
  79. return this.contract.GetMimeType();
  80. }
  81. public void Write(Stream output)
  82. {
  83. this.contract.Write(output);
  84. }
  85. public Stream GetStream()
  86. {
  87. return this.contract.GetStream();
  88. }
  89. }
  90. internal interface Contract
  91. {
  92. bool IsValid();
  93. string GetFileName();
  94. string GetMimeType();
  95. long GetFileLength();
  96. void Write(Stream output);
  97. Stream GetStream();
  98. }
  99. internal class LocalContract : Contract
  100. {
  101. private FileInfo fileInfo;
  102. public LocalContract(FileInfo fileInfo)
  103. {
  104. this.fileInfo = fileInfo;
  105. }
  106. public long GetFileLength()
  107. {
  108. return this.fileInfo.Length;
  109. }
  110. public string GetFileName()
  111. {
  112. return this.fileInfo.Name;
  113. }
  114. public string GetMimeType()
  115. {
  116. return Constants.CTYPE_DEFAULT;
  117. }
  118. public bool IsValid()
  119. {
  120. return this.fileInfo != null && this.fileInfo.Exists;
  121. }
  122. public void Write(Stream output)
  123. {
  124. using (BufferedStream bfs = new BufferedStream(this.fileInfo.OpenRead()))
  125. {
  126. int n = 0;
  127. byte[] buffer = new byte[Constants.READ_BUFFER_SIZE];
  128. while ((n = bfs.Read(buffer, 0, buffer.Length)) > 0)
  129. {
  130. output.Write(buffer, 0, n);
  131. }
  132. }
  133. }
  134. public Stream GetStream()
  135. {
  136. return new BufferedStream(this.fileInfo.OpenRead());
  137. }
  138. }
  139. internal class ByteArrayContract : Contract
  140. {
  141. private string fileName;
  142. private byte[] content;
  143. private string mimeType;
  144. public ByteArrayContract(string fileName, byte[] content, string mimeType)
  145. {
  146. this.fileName = fileName;
  147. this.content = content;
  148. this.mimeType = mimeType;
  149. }
  150. public bool IsValid()
  151. {
  152. return this.content != null && this.fileName != null;
  153. }
  154. public long GetFileLength()
  155. {
  156. return this.content.Length;
  157. }
  158. public string GetFileName()
  159. {
  160. return this.fileName;
  161. }
  162. public string GetMimeType()
  163. {
  164. if (string.IsNullOrEmpty(this.mimeType))
  165. {
  166. return Constants.CTYPE_DEFAULT;
  167. }
  168. else
  169. {
  170. return this.mimeType;
  171. }
  172. }
  173. public void Write(Stream output)
  174. {
  175. output.Write(this.content, 0, this.content.Length);
  176. }
  177. public Stream GetStream()
  178. {
  179. return new MemoryStream(content);
  180. }
  181. }
  182. internal class StreamContract : Contract
  183. {
  184. private string fileName;
  185. private Stream stream;
  186. private string mimeType;
  187. public StreamContract(string fileName, Stream stream, string mimeType)
  188. {
  189. this.fileName = fileName;
  190. this.stream = stream;
  191. this.mimeType = mimeType;
  192. }
  193. public long GetFileLength()
  194. {
  195. return 0L;
  196. }
  197. public string GetFileName()
  198. {
  199. return this.fileName;
  200. }
  201. public string GetMimeType()
  202. {
  203. if (string.IsNullOrEmpty(mimeType))
  204. {
  205. return Constants.CTYPE_DEFAULT;
  206. }
  207. else
  208. {
  209. return this.mimeType;
  210. }
  211. }
  212. public bool IsValid()
  213. {
  214. return this.stream != null && this.fileName != null;
  215. }
  216. public void Write(Stream output)
  217. {
  218. using (this.stream)
  219. {
  220. int n = 0;
  221. byte[] buffer = new byte[Constants.READ_BUFFER_SIZE];
  222. while ((n = this.stream.Read(buffer, 0, buffer.Length)) > 0)
  223. {
  224. output.Write(buffer, 0, n);
  225. }
  226. }
  227. }
  228. public Stream GetStream()
  229. {
  230. return stream;
  231. }
  232. }
  233. }