/
VirtualFileResultExecutor.cs
166 lines (140 loc) · 5.48 KB
/
VirtualFileResultExecutor.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Mvc.Infrastructure
{
/// <summary>
/// A <see cref="IActionResultExecutor{VirtualFileResult}"/> for <see cref="VirtualFileResult"/>.
/// </summary>
public class VirtualFileResultExecutor : FileResultExecutorBase, IActionResultExecutor<VirtualFileResult>
{
private readonly IWebHostEnvironment _hostingEnvironment;
/// <summary>
/// Initializes a new instance of <see cref="VirtualFileResultExecutor"/>.
/// </summary>
/// <param name="loggerFactory">The factory used to create loggers.</param>
/// <param name="hostingEnvironment">The hosting enviornment</param>
public VirtualFileResultExecutor(ILoggerFactory loggerFactory, IWebHostEnvironment hostingEnvironment)
: base(CreateLogger<VirtualFileResultExecutor>(loggerFactory))
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
_hostingEnvironment = hostingEnvironment;
}
/// <inheritdoc />
public virtual Task ExecuteAsync(ActionContext context, VirtualFileResult result)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
var fileInfo = GetFileInformation(result, _hostingEnvironment);
if (!fileInfo.Exists)
{
throw new FileNotFoundException(
Resources.FormatFileResult_InvalidPath(result.FileName), result.FileName);
}
Logger.ExecutingFileResult(result, result.FileName);
var lastModified = result.LastModified ?? fileInfo.LastModified;
var (range, rangeLength, serveBody) = SetHeadersAndLog(
context,
result,
fileInfo.Length,
result.EnableRangeProcessing,
lastModified,
result.EntityTag);
if (serveBody)
{
return WriteFileAsync(context, result, fileInfo, range, rangeLength);
}
return Task.CompletedTask;
}
/// <inheritdoc/>
protected virtual Task WriteFileAsync(ActionContext context, VirtualFileResult result, IFileInfo fileInfo, RangeItemHeaderValue? range, long rangeLength)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
return WriteFileAsyncInternal(context.HttpContext, fileInfo, range, rangeLength, Logger);
}
internal static Task WriteFileAsyncInternal(
HttpContext httpContext,
IFileInfo fileInfo,
RangeItemHeaderValue? range,
long rangeLength,
ILogger logger)
{
if (range != null && rangeLength == 0)
{
return Task.CompletedTask;
}
var response = httpContext.Response;
if (range != null)
{
logger.WritingRangeToBody();
}
if (range != null)
{
return response.SendFileAsync(fileInfo,
offset: range.From ?? 0L,
count: rangeLength);
}
return response.SendFileAsync(fileInfo,
offset: 0,
count: null);
}
internal static IFileInfo GetFileInformation(VirtualFileResult result, IWebHostEnvironment hostingEnvironment)
{
var fileProvider = GetFileProvider(result, hostingEnvironment);
if (fileProvider is NullFileProvider)
{
throw new InvalidOperationException(Resources.VirtualFileResultExecutor_NoFileProviderConfigured);
}
var normalizedPath = result.FileName;
if (normalizedPath.StartsWith("~", StringComparison.Ordinal))
{
normalizedPath = normalizedPath.Substring(1);
}
var fileInfo = fileProvider.GetFileInfo(normalizedPath);
return fileInfo;
}
internal static IFileProvider GetFileProvider(VirtualFileResult result, IWebHostEnvironment hostingEnvironment)
{
if (result.FileProvider != null)
{
return result.FileProvider;
}
result.FileProvider = hostingEnvironment.WebRootFileProvider;
return result.FileProvider;
}
/// <summary>
/// Obsolete, this API is no longer called.
/// </summary>
/// <param name="fileInfo"></param>
/// <returns></returns>
[Obsolete("This API is no longer called.")]
protected virtual Stream GetFileStream(IFileInfo fileInfo)
{
return fileInfo.CreateReadStream();
}
}
}