Skip to content

Commit

Permalink
Added optional version param for local file assets. Closes #2061
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed May 12, 2024
1 parent d03ebad commit 8938e3f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ public static class FileStorageExtensions
/// <param name="basePath">The optional base path for where uploaded media is stored.null Default is wwwroot/uploads/</param>
/// <param name="baseUrl">The optional base url for accessing uploaded media. Default is ~/uploads/</param>
/// <param name="naming">How uploaded media files should be named</param>
/// <param name="generateVersionParam">If a version param should be appended to the public url</param>
/// <param name="scope">The optional service scope. Default is singleton</param>
/// <returns>The service collection</returns>
public static IServiceCollection AddPiranhaFileStorage(
this IServiceCollection services,
string basePath = null,
string baseUrl = null,
FileStorageNaming naming = FileStorageNaming.UniqueFileNames,
bool generateVersionParam = false,
ServiceLifetime scope = ServiceLifetime.Singleton)
{
App.Modules.Register<FileStorageModule>();

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ public static class FileStorageStartupExtensions
/// <param name="basePath">The optional base path for where uploaded media is stored.null Default is wwwroot/uploads/</param>
/// <param name="baseUrl">The optional base url for accessing uploaded media. Default is ~/uploads/</param>
/// <param name="naming">How uploaded media files should be named</param>
/// <param name="generateVersionParam">If a version param should be appended to the public url</param>
/// <param name="scope">The optional service scope. Default is singleton</param>
/// <returns>The updated builder</returns>
public static PiranhaServiceBuilder UseFileStorage(
this PiranhaServiceBuilder serviceBuilder,
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;
}
Expand Down
22 changes: 20 additions & 2 deletions core/Piranha.Local.FileStorage/FileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ public class FileStorage : IStorage
private readonly string _basePath = "wwwroot/uploads/";
private readonly string _baseUrl = "~/uploads/";
private readonly FileStorageNaming _naming;
private readonly bool _versionParam;

/// <summary>
/// Default constructor.
/// </summary>
/// <param name="basePath">The optional base path</param>
/// <param name="baseUrl">The optional base url</param>
/// <param name="naming">How uploaded media files should be named</param>
/// <param name="generateVersionParam">If a version param should be appended to the public url</param>
public FileStorage(
string basePath = null,
string baseUrl = null,
FileStorageNaming naming = FileStorageNaming.UniqueFileNames)
FileStorageNaming naming = FileStorageNaming.UniqueFileNames,
bool generateVersionParam = false)
{
if (!string.IsNullOrEmpty(basePath))
{
Expand All @@ -44,6 +47,7 @@ public class FileStorage : IStorage
}

_naming = naming;
_versionParam = generateVersionParam;
}

/// <summary>
Expand All @@ -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;
}
Expand Down

0 comments on commit 8938e3f

Please sign in to comment.