Skip to content

Commit f7c9474

Browse files
committed
提高MulitpartFile的扩展性
1 parent fe8f44e commit f7c9474

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

WebApiClient/Parameterables/MulitpartFile.cs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,7 @@ public class MulitpartFile : IApiParameterable
1616
/// <summary>
1717
/// 数据流
1818
/// </summary>
19-
private readonly Stream stream;
20-
21-
/// <summary>
22-
/// 本机文件路径
23-
/// </summary>
24-
private readonly string filePath;
25-
26-
/// <summary>
27-
/// 文件好友名称
28-
/// </summary>
29-
private readonly string fileName;
19+
private readonly Lazy<Stream> stream;
3020

3121
/// <summary>
3222
/// 上传进度变化事件
@@ -36,15 +26,16 @@ public class MulitpartFile : IApiParameterable
3626
/// <summary>
3727
/// 获取文件好友名称
3828
/// </summary>
39-
public string FileName
29+
public string FileName { get; private set; }
30+
31+
/// <summary>
32+
/// 获取编码后的文件好友名称
33+
/// </summary>
34+
public virtual string EncodedFileName
4035
{
4136
get
4237
{
43-
if (string.IsNullOrEmpty(this.fileName))
44-
{
45-
return this.fileName;
46-
}
47-
return HttpUtility.UrlEncode(this.fileName, Encoding.UTF8);
38+
return HttpUtility.UrlEncode(this.FileName, Encoding.UTF8);
4839
}
4940
}
5041

@@ -72,8 +63,13 @@ public MulitpartFile(byte[] buffer, string fileName) :
7263
/// <exception cref="ArgumentNullException"></exception>
7364
public MulitpartFile(Stream stream, string fileName)
7465
{
75-
this.stream = stream ?? throw new ArgumentNullException(nameof(stream));
76-
this.fileName = fileName;
66+
if (stream == null)
67+
{
68+
throw new ArgumentNullException(nameof(stream));
69+
}
70+
71+
this.stream = new Lazy<Stream>(() => stream);
72+
this.FileName = fileName;
7773
}
7874

7975
/// <summary>
@@ -94,8 +90,8 @@ public MulitpartFile(string localFilePath)
9490
throw new FileNotFoundException(localFilePath);
9591
}
9692

97-
this.filePath = localFilePath;
98-
this.fileName = Path.GetFileName(localFilePath);
93+
this.stream = new Lazy<Stream>(() => new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
94+
this.FileName = Path.GetFileName(localFilePath);
9995
}
10096

10197
/// <summary>
@@ -105,18 +101,19 @@ public MulitpartFile(string localFilePath)
105101
/// <param name="parameter">特性关联的参数</param>
106102
async Task IApiParameterable.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
107103
{
108-
context.RequestMessage.AddMulitpartFile(this.GetUploadStream(), parameter.Name, this.FileName, this.ContentType);
109-
await ApiTask.CompletedTask;
104+
await this.BeforeRequestAsync(context, parameter).ConfigureAwait(false);
110105
}
111106

112107
/// <summary>
113-
/// 获取文件流
108+
/// 执行请求前
114109
/// </summary>
115-
/// <returns></returns>
116-
private UploadStream GetUploadStream()
110+
/// <param name="context">上下文</param>
111+
/// <param name="parameter">特性关联的参数</param>
112+
protected virtual async Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
117113
{
118-
var inner = this.stream ?? new FileStream(this.filePath, FileMode.Open, FileAccess.Read);
119-
return new UploadStream(inner, this.OnUploadProgressChanged);
114+
var uploadStream = new UploadStream(this.stream.Value, this.OnUploadProgressChanged);
115+
context.RequestMessage.AddMulitpartFile(uploadStream, parameter.Name, this.EncodedFileName, this.ContentType);
116+
await ApiTask.CompletedTask;
120117
}
121118

122119
/// <summary>

0 commit comments

Comments
 (0)