From 8938e3f4f66c18a616e5146c79a50581b661648e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kan=20Edling?= Date: Sun, 12 May 2024 08:27:57 +0200 Subject: [PATCH] Added optional version param for local file assets. Closes #2061 --- .../Extensions/FileStorageExtensions.cs | 4 +++- .../FileStorageStartupExtensions.cs | 4 +++- core/Piranha.Local.FileStorage/FileStorage.cs | 22 +++++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/core/Piranha.Local.FileStorage/Extensions/FileStorageExtensions.cs b/core/Piranha.Local.FileStorage/Extensions/FileStorageExtensions.cs index ce228aa991..48374e5681 100644 --- a/core/Piranha.Local.FileStorage/Extensions/FileStorageExtensions.cs +++ b/core/Piranha.Local.FileStorage/Extensions/FileStorageExtensions.cs @@ -21,6 +21,7 @@ public static class FileStorageExtensions /// The optional base path for where uploaded media is stored.null Default is wwwroot/uploads/ /// The optional base url for accessing uploaded media. Default is ~/uploads/ /// How uploaded media files should be named + /// If a version param should be appended to the public url /// The optional service scope. Default is singleton /// The service collection public static IServiceCollection AddPiranhaFileStorage( @@ -28,11 +29,12 @@ public static IServiceCollection AddPiranhaFileStorage( string basePath = null, string baseUrl = null, FileStorageNaming naming = FileStorageNaming.UniqueFileNames, + bool generateVersionParam = false, ServiceLifetime scope = ServiceLifetime.Singleton) { App.Modules.Register(); - services.Add(new ServiceDescriptor(typeof(IStorage), sp => new FileStorage(basePath, baseUrl, naming), scope)); + services.Add(new ServiceDescriptor(typeof(IStorage), sp => new FileStorage(basePath, baseUrl, naming, generateVersionParam), scope)); return services; } diff --git a/core/Piranha.Local.FileStorage/Extensions/FileStorageStartupExtensions.cs b/core/Piranha.Local.FileStorage/Extensions/FileStorageStartupExtensions.cs index a5d87328e8..c98064c408 100644 --- a/core/Piranha.Local.FileStorage/Extensions/FileStorageStartupExtensions.cs +++ b/core/Piranha.Local.FileStorage/Extensions/FileStorageStartupExtensions.cs @@ -21,6 +21,7 @@ public static class FileStorageStartupExtensions /// The optional base path for where uploaded media is stored.null Default is wwwroot/uploads/ /// The optional base url for accessing uploaded media. Default is ~/uploads/ /// How uploaded media files should be named + /// If a version param should be appended to the public url /// The optional service scope. Default is singleton /// The updated builder public static PiranhaServiceBuilder UseFileStorage( @@ -28,9 +29,10 @@ public static PiranhaServiceBuilder UseFileStorage( string basePath = null, string baseUrl = null, FileStorageNaming naming = FileStorageNaming.UniqueFileNames, + bool generateVersionParam = false, ServiceLifetime scope = ServiceLifetime.Singleton) { - serviceBuilder.Services.AddPiranhaFileStorage(basePath, baseUrl, naming, scope); + serviceBuilder.Services.AddPiranhaFileStorage(basePath, baseUrl, naming, generateVersionParam, scope); return serviceBuilder; } diff --git a/core/Piranha.Local.FileStorage/FileStorage.cs b/core/Piranha.Local.FileStorage/FileStorage.cs index 7080a66b16..b1a351275b 100644 --- a/core/Piranha.Local.FileStorage/FileStorage.cs +++ b/core/Piranha.Local.FileStorage/FileStorage.cs @@ -17,6 +17,7 @@ public class FileStorage : IStorage private readonly string _basePath = "wwwroot/uploads/"; private readonly string _baseUrl = "~/uploads/"; private readonly FileStorageNaming _naming; + private readonly bool _versionParam; /// /// Default constructor. @@ -24,10 +25,12 @@ public class FileStorage : IStorage /// The optional base path /// The optional base url /// How uploaded media files should be named + /// If a version param should be appended to the public url public FileStorage( string basePath = null, string baseUrl = null, - FileStorageNaming naming = FileStorageNaming.UniqueFileNames) + FileStorageNaming naming = FileStorageNaming.UniqueFileNames, + bool generateVersionParam = false) { if (!string.IsNullOrEmpty(basePath)) { @@ -44,6 +47,7 @@ public FileStorage( } _naming = naming; + _versionParam = generateVersionParam; } /// @@ -68,7 +72,21 @@ public string GetPublicUrl(Media media, string filename) { if (media != null && !string.IsNullOrWhiteSpace(filename)) { - return _baseUrl + GetResourceName(media, filename, true); + var publicUrl = _baseUrl + GetResourceName(media, filename, true); + if (_versionParam) + { + var versionHash = Math.Abs(media.LastModified.GetHashCode()).ToString(); + + if (!publicUrl.Contains("?")) + { + publicUrl += $"?version={ versionHash }"; + } + else + { + publicUrl += $"&version={ versionHash }"; + } + } + return publicUrl; } return null; }