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