diff --git a/mixtape/FileStorage/ManifestFileVersionProvider.cs b/mixtape/FileStorage/ManifestFileVersionProvider.cs new file mode 100644 index 00000000..824ba5ce --- /dev/null +++ b/mixtape/FileStorage/ManifestFileVersionProvider.cs @@ -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; +// +// /// +// /// Provides version hash for a specified file. +// /// +// 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(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); +// } +// } +// } \ No newline at end of file diff --git a/mixtape/Frontend/ViteScriptTagHelper.cs b/mixtape/Frontend/ViteScriptTagHelper.cs index a5de24a0..696fed36 100644 --- a/mixtape/Frontend/ViteScriptTagHelper.cs +++ b/mixtape/Frontend/ViteScriptTagHelper.cs @@ -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().Port; + int? viteProxyPort = 5123; + #if DEBUG + viteProxyPort = options.For().Port; + #endif HttpRequest request = ViewContext.HttpContext.Request; string fullPath = $"{request.Scheme}://{request.Host.Host}:{viteProxyPort}/{Src}"; diff --git a/mixtape/TagHelpers/ResizeTagHelper.cs b/mixtape/TagHelpers/ResizeTagHelper.cs index a79ea37d..046be7ca 100644 --- a/mixtape/TagHelpers/ResizeTagHelper.cs +++ b/mixtape/TagHelpers/ResizeTagHelper.cs @@ -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 _values; - [HtmlAttributeName("", DictionaryAttributePrefix = PREFIX)] + [HtmlAttributeName("", DictionaryAttributePrefix = Prefix)] public IDictionary Values { get => _values ??= new Dictionary(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 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); } }