-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathFileParameter.cs
More file actions
124 lines (109 loc) · 4.62 KB
/
FileParameter.cs
File metadata and controls
124 lines (109 loc) · 4.62 KB
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
// Copyright (c) .NET Foundation and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace RestSharp;
/// <summary>
/// Container for files to be uploaded with requests
/// </summary>
[PublicAPI]
public record FileParameter {
/// <summary>
/// Name of the parameter
/// </summary>
public string Name { get; }
/// <summary>
/// Name of the file to use when uploading
/// </summary>
public string FileName { get; }
/// <summary>
/// MIME content type of file
/// </summary>
public ContentType ContentType { get; }
/// <summary>
/// Provides raw data for file
/// </summary>
public Func<Stream> GetFile { get; }
public FileParameterOptions Options { get; }
FileParameter(string name, string fileName, Func<Stream> getFile, ContentType? contentType, FileParameterOptions options) {
Name = name;
FileName = fileName;
GetFile = getFile;
Options = options;
ContentType = contentType ?? ContentType.Binary;
}
/// <summary>
/// Creates a file parameter from an array of bytes.
/// </summary>
/// <param name="name">The parameter name to use in the request.</param>
/// <param name="data">The data to use as the file's contents.</param>
/// <param name="filename">The filename to use in the request.</param>
/// <param name="contentType">The content type to use in the request.</param>
/// <param name="options">File parameter options</param>
/// <returns>The <see cref="FileParameter" /></returns>
public static FileParameter Create(
string name,
byte[] data,
string filename,
ContentType? contentType = null,
FileParameterOptions? options = null
) {
return new FileParameter(name, filename, GetFile, contentType, options ?? new FileParameterOptions());
Stream GetFile() {
var stream = new MemoryStream();
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
}
/// <summary>
/// Creates a file parameter from an array of bytes.
/// </summary>
/// <param name="name">The parameter name to use in the request.</param>
/// <param name="getFile">Delegate that will be called with the request stream so you can write to it..</param>
/// <param name="fileName">The filename to use in the request.</param>
/// <param name="contentType">Optional: parameter content type, default is "application/g-zip"</param>
/// <param name="options">File parameter options</param>
/// <returns>The <see cref="FileParameter" /> using the default content type.</returns>
public static FileParameter Create(
string name,
Func<Stream> getFile,
string fileName,
ContentType? contentType = null,
FileParameterOptions? options = null
)
=> new(name, fileName, getFile, contentType, options ?? new FileParameterOptions());
public static FileParameter FromFile(
string fullPath,
string? name = null,
ContentType? contentType = null,
FileParameterOptions? options = null
) {
if (!File.Exists(Ensure.NotEmptyString(fullPath, nameof(fullPath)))) throw new FileNotFoundException("File not found", fullPath);
var fileName = Path.GetFileName(fullPath);
var parameterName = name ?? fileName;
return new FileParameter(parameterName, fileName, GetFile, contentType, options ?? new FileParameterOptions());
Stream GetFile() => File.OpenRead(fullPath);
}
}
[PublicAPI]
public class FileParameterOptions {
[Obsolete("Use DisableFilenameStar instead")]
[CLSCompliant(false)]
public bool DisableFileNameStar {
get => DisableFilenameStar;
set => DisableFilenameStar = value;
}
public bool DisableFilenameStar { get; set; } = true;
public bool DisableFilenameEncoding { get; set; }
}