forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipartFormDataRemoteStreamProvider.cs
More file actions
101 lines (90 loc) · 4.7 KB
/
MultipartFormDataRemoteStreamProvider.cs
File metadata and controls
101 lines (90 loc) · 4.7 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Net.Http.Formatting.Internal;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace System.Net.Http
{
/// <summary>
/// A <see cref="MultipartStreamProvider"/> implementation suited for use with HTML file uploads for writing file
/// content to a remote storage <see cref="Stream"/>. The stream provider looks at the <b>Content-Disposition</b>
/// header field and determines an output remote <see cref="Stream"/> based on the presence of a <b>filename</b>
/// parameter. If a <b>filename</b> parameter is present in the <b>Content-Disposition</b> header field, then the
/// body part is written to a remote <see cref="Stream"/> provided by <see cref="GetRemoteStream"/>.
/// Otherwise it is written to a <see cref="MemoryStream"/>.
/// </summary>
public abstract class MultipartFormDataRemoteStreamProvider : MultipartStreamProvider
{
private CancellationToken _cancellationToken = CancellationToken.None;
/// <summary>
/// Initializes a new instance of the <see cref="MultipartFormDataRemoteStreamProvider"/> class.
/// </summary>
protected MultipartFormDataRemoteStreamProvider()
{
FormData = HttpValueCollection.Create();
FileData = new Collection<MultipartRemoteFileData>();
}
/// <summary>
/// Gets a collection of file data passed as part of the multipart form data.
/// </summary>
public Collection<MultipartRemoteFileData> FileData { get; private set; }
/// <summary>
/// Gets a <see cref="NameValueCollection"/> of form data passed as part of the multipart form data.
/// </summary>
public NameValueCollection FormData { get; private set; }
/// <summary>
/// Provides a <see cref="RemoteStreamInfo"/> for <see cref="GetStream"/>. Override this method to provide a
/// remote stream to which the data should be written.
/// </summary>
/// <param name="parent">The parent <see cref="HttpContent"/> MIME multipart instance.</param>
/// <param name="headers">The header fields describing the body part's content. </param>
/// <returns>
/// A result specifying a remote stream where the file will be written to and a location where the file can be
/// accessed. It cannot be null and the stream must be writable.
/// </returns>
public abstract RemoteStreamInfo GetRemoteStream(HttpContent parent, HttpContentHeaders headers);
/// <inheritdoc />
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
if (MultipartFormDataStreamProviderHelper.IsFileContent(parent, headers))
{
RemoteStreamInfo remoteStreamInfo = GetRemoteStream(parent, headers);
if (remoteStreamInfo == null)
{
throw Error.InvalidOperation(Properties.Resources.RemoteStreamInfoCannotBeNull,
"GetRemoteStream", GetType().Name);
}
FileData.Add(new MultipartRemoteFileData(headers, remoteStreamInfo.Location, remoteStreamInfo.FileName));
return remoteStreamInfo.RemoteStream;
}
return new MemoryStream();
}
/// <summary>
/// Read the non-file contents as form data.
/// </summary>
/// <returns>A <see cref="Task"/> representing the post processing.</returns>
public override Task ExecutePostProcessingAsync()
{
// In consistency with existing MultipartFormDataStreamProvider,
// this method predates support for cancellation, and we need to make sure it is always invoked when
// ExecutePostProcessingAsync is called for compatability.
return MultipartFormDataStreamProviderHelper.ReadFormDataAsync(Contents, FormData,
_cancellationToken);
}
/// <summary>
/// Read the non-file contents as form data.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task"/> representing the post processing.</returns>
public override Task ExecutePostProcessingAsync(CancellationToken cancellationToken)
{
_cancellationToken = cancellationToken;
return ExecutePostProcessingAsync();
}
}
}