-
Notifications
You must be signed in to change notification settings - Fork 573
/
Copy pathMultipartFormDataContent.cs
95 lines (84 loc) · 3.51 KB
/
MultipartFormDataContent.cs
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
namespace Stripe.Infrastructure.FormEncoding
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
/// <summary>
/// A container for content encoded using <c>multipart/form-data</c> MIME type.
/// </summary>
internal class MultipartFormDataContent : System.Net.Http.MultipartFormDataContent
{
/// <summary>
/// Initializes a new instance of the <see cref="MultipartFormDataContent"/> class.
/// </summary>
/// <param name="nameValueCollection">The collection of name/value tuples to encode.</param>
public MultipartFormDataContent(IEnumerable<KeyValuePair<string, object>> nameValueCollection)
: this(nameValueCollection, Guid.NewGuid().ToString())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MultipartFormDataContent"/> class.
/// </summary>
/// <param name="nameValueCollection">The collection of name/value tuples to encode.</param>
/// <param name="boundary">The boundary string for the multipart form data content.</param>
public MultipartFormDataContent(
IEnumerable<KeyValuePair<string, object>> nameValueCollection,
string boundary)
: base(boundary)
{
if (nameValueCollection == null)
{
throw new ArgumentNullException(nameof(nameValueCollection));
}
this.ProcessParameters(nameValueCollection);
}
private static StringContent CreateStringContent(string value)
=> new StringContent(value, System.Text.Encoding.UTF8);
private static StreamContent CreateStreamContent(Stream value, string name)
{
var fileName = "blob";
var extension = string.Empty;
FileStream fileStream = value as FileStream;
if ((fileStream != null) && (!string.IsNullOrEmpty(fileStream.Name)))
{
fileName = fileStream.Name;
extension = Path.GetExtension(fileName);
}
var content = new StreamContent(value);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = name,
FileName = fileName,
FileNameStar = fileName,
};
content.Headers.ContentType = new MediaTypeHeaderValue(MimeTypes.GetMimeType(extension));
return content;
}
private static string QuoteString(string value)
{
return "\"" + value + "\"";
}
private void ProcessParameters(IEnumerable<KeyValuePair<string, object>> nameValueCollection)
{
foreach (var kvp in nameValueCollection)
{
switch (kvp.Value)
{
case string s:
this.Add(CreateStringContent(s), QuoteString(kvp.Key));
break;
case Stream s:
this.Add(CreateStreamContent(s, QuoteString(kvp.Key)));
break;
default:
var message =
"Unexpected type in collection. Expected System.String or " +
$"System.IO.Stream, got {kvp.GetType().FullName}.";
throw new ArgumentException(message, nameof(nameValueCollection));
}
}
}
}
}