FileItem.cs 6.7 KB

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