Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System.Collections.Generic;
10 : using System.Globalization;
11 : using System.Linq;
12 :
13 : namespace Cqrs.WebApi.Formatters.FormMultipart.Infrastructure
14 : {
15 : /// <summary>
16 : /// Provides access to all fields and files in a multi-part form-data request.
17 : /// </summary>
18 : public class FormData
19 1 : {
20 : private List<ValueFile> _files;
21 :
22 : private List<ValueString> _fields;
23 :
24 : /// <summary>
25 : /// All <see cref="ValueFile">files</see> in the request.
26 : /// </summary>
27 : public List<ValueFile> Files
28 : {
29 : get
30 : {
31 : if(_files == null)
32 : _files = new List<ValueFile>();
33 : return _files;
34 : }
35 : set
36 : {
37 : _files = value;
38 : }
39 : }
40 :
41 : /// <summary>
42 : /// All <see cref="ValueString">fields</see> in the request.
43 : /// </summary>
44 : public List<ValueString> Fields
45 : {
46 : get
47 : {
48 : if(_fields == null)
49 : _fields = new List<ValueString>();
50 : return _fields;
51 : }
52 : set
53 : {
54 : _fields = value;
55 : }
56 : }
57 :
58 : /// <summary>
59 : /// Get's all keys from <see cref="Fields"/> and <see cref="Files"/>.
60 : /// </summary>
61 1 : public List<string> GetAllKeys()
62 : {
63 : return Fields.Select(m => m.Name).Concat(Files.Select(m => m.Name)).ToList();
64 : }
65 :
66 : /// <summary>
67 : /// Adds a new <see cref="ValueString"/> to <see cref="Fields"/>.
68 : /// </summary>
69 : /// <param name="name">The <see cref="ValueString.Name"/>.</param>
70 : /// <param name="value">The <see cref="ValueString.Value"/>.</param>
71 1 : public void Add(string name, string value)
72 : {
73 : Fields.Add(new ValueString { Name = name, Value = value});
74 : }
75 :
76 : /// <summary>
77 : /// Adds a new <see cref="ValueFile"/> to <see cref="Files"/>.
78 : /// </summary>
79 : /// <param name="name">The <see cref="ValueFile.Name"/>.</param>
80 : /// <param name="value">The <see cref="ValueFile.Value"/>.</param>
81 1 : public void Add(string name, HttpFile value)
82 : {
83 : Files.Add(new ValueFile { Name = name, Value = value });
84 : }
85 :
86 : /// <summary>
87 : /// Get's the first <see cref="ValueString.Value"/> from <see cref="Fields"/> with a <see cref="ValueString.Name"/> equal to <paramref name="name"/>.
88 : /// </summary>
89 1 : public bool TryGetValue(string name, CultureInfo culture, out string value)
90 : {
91 : var field = Fields.FirstOrDefault(m => culture.CompareInfo.Compare(m.Name, name, CompareOptions.IgnoreCase) == 0);
92 : if (field != null)
93 : {
94 : value = field.Value;
95 : return true;
96 : }
97 : value = null;
98 : return false;
99 : }
100 :
101 : /// <summary>
102 : /// Get's the first <see cref="ValueFile.Value"/> from <see cref="Files"/> with a <see cref="ValueFile.Name"/> equal to <paramref name="name"/>.
103 : /// </summary>
104 1 : public bool TryGetValue(string name, CultureInfo culture, out HttpFile value)
105 : {
106 : var file = Files.FirstOrDefault(m => culture.CompareInfo.Compare(m.Name, name, CompareOptions.IgnoreCase) == 0);
107 : if (file != null)
108 : {
109 : value = file.Value;
110 : return true;
111 : }
112 : value = null;
113 : return false;
114 : }
115 :
116 : /// <summary>
117 : /// Get's all <see cref="ValueFile.Value"/> from <see cref="Files"/> with a <see cref="ValueFile.Name"/> equal to <paramref name="name"/>.
118 : /// </summary>
119 1 : public List<string> GetValues(string name, CultureInfo culture)
120 : {
121 : return Fields
122 : .Where(m => culture.CompareInfo.Compare(m.Name, name, CompareOptions.IgnoreCase) == 0)
123 : .Select(m => m.Value)
124 : .ToList();
125 : }
126 :
127 : /// <summary>
128 : /// Get's all <see cref="ValueString.Value"/> from <see cref="Fields"/> with a <see cref="ValueString.Name"/> equal to <paramref name="name"/>.
129 : /// </summary>
130 1 : public List<HttpFile> GetFiles(string name, CultureInfo culture)
131 : {
132 : return Files
133 : .Where(m => culture.CompareInfo.Compare(m.Name, name, CompareOptions.IgnoreCase) == 0)
134 : .Select(m => m.Value)
135 : .ToList();
136 : }
137 :
138 : /// <summary>
139 : /// Determines whether <see cref="Files"/> or <see cref="Fields"/> contains an item with the specific name.
140 : /// </summary>
141 : /// <param name="name">The name of the item to locate.</param>
142 : /// <param name="culture">The <see cref="CultureInfo"/> to use in key evaluation.</param>
143 : /// <returns>true if an item is found; otherwise, false.</returns>
144 1 : public bool Contains(string name, CultureInfo culture)
145 : {
146 : string val;
147 : HttpFile file;
148 :
149 : return TryGetValue(name, culture, out val) || TryGetValue(name, culture, out file);
150 : }
151 :
152 : /// <summary>
153 : /// A string value.
154 : /// </summary>
155 : public class ValueString
156 1 : {
157 : /// <summary>
158 : /// A name of the value.
159 : /// </summary>
160 : public string Name { get; set; }
161 :
162 : /// <summary>
163 : /// The value itself.
164 : /// </summary>
165 : public string Value { get; set; }
166 : }
167 :
168 : /// <summary>
169 : /// A file value.
170 : /// </summary>
171 : public class ValueFile
172 1 : {
173 : /// <summary>
174 : /// A name of the value.
175 : /// </summary>
176 : public string Name { get; set; }
177 :
178 : /// <summary>
179 : /// The file itself.
180 : /// </summary>
181 : public HttpFile Value { get; set; }
182 : }
183 : }
184 : }
|