fix bug in vite tag helper

This commit is contained in:
2026-05-05 14:10:31 +02:00
parent 4e99e0233a
commit f70ce7b46c
3 changed files with 108 additions and 26 deletions
@@ -0,0 +1,91 @@
// using System.IO;
// using System.Security.Cryptography;
// 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 Mixtape.FileStorage;
//
// /// <summary>
// /// Provides version hash for a specified file.
// /// </summary>
// internal sealed class ManifestFileVersionProvider : IFileVersionProvider
// {
// private const string VersionKey = "v";
//
// public ManifestFileVersionProvider(IWebHostEnvironment hostingEnvironment)
// {
// ArgumentNullException.ThrowIfNull(hostingEnvironment);
//
// FileProvider = hostingEnvironment.WebRootFileProvider;
// Cache = null; //cacheProvider.Cache;
// }
//
// public IFileProvider FileProvider { get; }
//
// public IMemoryCache Cache { get; }
//
//
// public string AddFileVersionToPath(PathString requestPathBase, string path)
// {
// ArgumentNullException.ThrowIfNull(path);
//
// string resolvedPath = path;
//
// int queryStringOrFragmentStartIndex = path.AsSpan().IndexOfAny('?', '#');
// if (queryStringOrFragmentStartIndex != -1)
// {
// resolvedPath = path[..queryStringOrFragmentStartIndex];
// }
//
// if (Uri.TryCreate(resolvedPath, UriKind.Absolute, out Uri uri) && !uri.IsFile)
// {
// // Don't append version if the path is absolute.
// return path;
// }
//
// if (Cache.TryGetValue<string>(path, out string value) && value is not null)
// {
// return value;
// }
//
// MemoryCacheEntryOptions cacheEntryOptions = new();
// cacheEntryOptions.AddExpirationToken(FileProvider.Watch(resolvedPath));
// IFileInfo fileInfo = FileProvider.GetFileInfo(resolvedPath);
//
// if (!fileInfo.Exists &&
// requestPathBase.HasValue &&
// resolvedPath.StartsWith(requestPathBase.Value, StringComparison.OrdinalIgnoreCase))
// {
// string 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));
// Cache.Set(path, value, cacheEntryOptions);
// return value;
// }
//
// private static string GetHashForFile(IFileInfo fileInfo)
// {
// using (Stream readStream = fileInfo.CreateReadStream())
// {
// byte[] hash = SHA256.HashData(readStream);
// return WebEncoders.Base64UrlEncode(hash);
// }
// }
// }
+9 -2
View File
@@ -8,7 +8,11 @@ using Microsoft.Extensions.Hosting;
namespace Mixtape.Frontend;
[HtmlTargetElement("app-vitescript", Attributes = "src", TagStructure = TagStructure.NormalOrSelfClosing)]
public class ViteScriptTagHelper(IWebHostEnvironment env, IMixtapeOptions options) : TagHelper
public class ViteScriptTagHelper(IWebHostEnvironment env
#if DEBUG
,IMixtapeOptions options
#endif
) : TagHelper
{
[HtmlAttributeNotBound]
[ViewContext]
@@ -26,7 +30,10 @@ public class ViteScriptTagHelper(IWebHostEnvironment env, IMixtapeOptions option
return;
}
int? viteProxyPort = options.For<ViteProxy.ViteProxyOptions>().Port;
int? viteProxyPort = 5123;
#if DEBUG
viteProxyPort = options.For<ViteProxy.ViteProxyOptions>().Port;
#endif
HttpRequest request = ViewContext.HttpContext.Request;
string fullPath = $"{request.Scheme}://{request.Host.Host}:{viteProxyPort}/{Src}";
+8 -24
View File
@@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Mixtape.TagHelpers;
[HtmlTargetElement(Attributes = "app-resize")]
public class ResizeTagHelper : TagHelper
public class ResizeTagHelper(IFileVersionProvider fileVersionProvider) : TagHelper
{
[HtmlAttributeName("src")]
public string Src { get; set; }
@@ -18,14 +18,6 @@ public class ResizeTagHelper : TagHelper
[ViewContext]
public ViewContext ViewContext { get; set; }
protected IFileVersionProvider FileVersionProvider { get; set; }
public ResizeTagHelper(IFileVersionProvider fileVersionProvider)
{
FileVersionProvider = fileVersionProvider;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
@@ -42,7 +34,7 @@ public class ResizeTagHelper : TagHelper
};
}
string src = FileVersionProvider.AddFileVersionToPath(ViewContext.HttpContext.Request.PathBase, Src).Resize(Preset, focalPoint);
string src = fileVersionProvider.AddFileVersionToPath(ViewContext.HttpContext.Request.PathBase, Src).Resize(Preset, focalPoint);
output.Attributes.RemoveAll("app-resize");
output.Attributes.RemoveAll("app-focal-point");
output.Attributes.SetAttribute("src", src);
@@ -50,14 +42,14 @@ public class ResizeTagHelper : TagHelper
}
[HtmlTargetElement(Attributes = PREFIX + "*")]
public class ResizeCustomTagHelper : TagHelper
[HtmlTargetElement(Attributes = Prefix + "*")]
public class ResizeCustomTagHelper(IFileVersionProvider fileVersionProvider) : TagHelper
{
private const string PREFIX = "app-resize:";
private const string Prefix = "app-resize:";
private IDictionary<string, string> _values;
[HtmlAttributeName("", DictionaryAttributePrefix = PREFIX)]
[HtmlAttributeName("", DictionaryAttributePrefix = Prefix)]
public IDictionary<string, string> Values
{
get => _values ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
@@ -67,24 +59,16 @@ public class ResizeCustomTagHelper : TagHelper
[ViewContext]
public ViewContext ViewContext { get; set; }
protected IFileVersionProvider FileVersionProvider { get; set; }
public ResizeCustomTagHelper(IFileVersionProvider fileVersionProvider)
{
FileVersionProvider = fileVersionProvider;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
foreach (var value in Values)
foreach (KeyValuePair<string, string> value in Values)
{
if (output.Attributes.TryGetAttribute(value.Key, out TagHelperAttribute attr))
{
string parameterName = attr.Name.ToString();
string parameterValue = attr.Value.ToString();
string newValue = FileVersionProvider.AddFileVersionToPath(ViewContext.HttpContext.Request.PathBase, parameterValue).Resize(value.Value);
string newValue = fileVersionProvider.AddFileVersionToPath(ViewContext.HttpContext.Request.PathBase, parameterValue).Resize(value.Value);
output.Attributes.SetAttribute(parameterName, newValue);
}
}