-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
DefaultFileVersionProvider.cs
110 lines (93 loc) · 3.81 KB
/
DefaultFileVersionProvider.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
// 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;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.AspNetCore.Mvc.Razor.Infrastructure
{
/// <summary>
/// Provides version hash for a specified file.
/// </summary>
internal class DefaultFileVersionProvider : IFileVersionProvider
{
private const string VersionKey = "v";
private static readonly char[] QueryStringAndFragmentTokens = new [] { '?', '#' };
public DefaultFileVersionProvider(
IWebHostEnvironment hostingEnvironment,
TagHelperMemoryCacheProvider cacheProvider)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
if (cacheProvider == null)
{
throw new ArgumentNullException(nameof(cacheProvider));
}
FileProvider = hostingEnvironment.WebRootFileProvider;
Cache = cacheProvider.Cache;
}
public IFileProvider FileProvider { get; }
public IMemoryCache Cache { get; }
public string AddFileVersionToPath(PathString requestPathBase, string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
var resolvedPath = path;
var queryStringOrFragmentStartIndex = path.IndexOfAny(QueryStringAndFragmentTokens);
if (queryStringOrFragmentStartIndex != -1)
{
resolvedPath = path.Substring(0, queryStringOrFragmentStartIndex);
}
if (Uri.TryCreate(resolvedPath, UriKind.Absolute, out var uri) && !uri.IsFile)
{
// Don't append version if the path is absolute.
return path;
}
if (Cache.TryGetValue(path, out string value))
{
return value;
}
var cacheEntryOptions = new MemoryCacheEntryOptions();
cacheEntryOptions.AddExpirationToken(FileProvider.Watch(resolvedPath));
var fileInfo = FileProvider.GetFileInfo(resolvedPath);
if (!fileInfo.Exists &&
requestPathBase.HasValue &&
resolvedPath.StartsWith(requestPathBase.Value, StringComparison.OrdinalIgnoreCase))
{
var requestPathBaseRelativePath = resolvedPath.Substring(requestPathBase.Value.Length);
cacheEntryOptions.AddExpirationToken(FileProvider.Watch(requestPathBaseRelativePath));
fileInfo = FileProvider.GetFileInfo(requestPathBaseRelativePath);
}
if (fileInfo.Exists)
{
value = QueryHelpers.AddQueryString(path, VersionKey, GetHashForFile(fileInfo));
}
else
{
// if the file is not in the current server.
value = path;
}
cacheEntryOptions.SetSize(value.Length * sizeof(char));
value = Cache.Set(path, value, cacheEntryOptions);
return value;
}
private static string GetHashForFile(IFileInfo fileInfo)
{
using (var sha256 = CryptographyAlgorithms.CreateSHA256())
{
using (var readStream = fileInfo.CreateReadStream())
{
var hash = sha256.ComputeHash(readStream);
return WebEncoders.Base64UrlEncode(hash);
}
}
}
}
}