Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NullMediaFileStoreCache to avoid injecting IServiceProvider #15391

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Admin;
using OrchardCore.DisplayManagement.Notify;
using OrchardCore.Media.ViewModels;
Expand All @@ -22,14 +20,12 @@ public class MediaCacheController : Controller

public MediaCacheController(
IAuthorizationService authorizationService,
IServiceProvider serviceProvider,
IMediaFileStoreCache mediaFileStoreCache,
INotifier notifier,
IHtmlLocalizer<MediaCacheController> htmlLocalizer
)
IHtmlLocalizer<MediaCacheController> htmlLocalizer)
{
_authorizationService = authorizationService;
// Resolve from service provider as the service will not be registered if configuration is invalid.
_mediaFileStoreCache = serviceProvider.GetService<IMediaFileStoreCache>();
_mediaFileStoreCache = mediaFileStoreCache;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nullcheck below should be adjusted too.

_notifier = notifier;
H = htmlLocalizer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.BackgroundTasks;
Expand All @@ -20,14 +19,14 @@ public class RemoteMediaCacheBackgroundTask : IBackgroundTask

private readonly IMediaFileStore _mediaFileStore;
private readonly ILogger _logger;

private readonly string _cachePath;
private readonly TimeSpan? _cacheMaxStale;

public RemoteMediaCacheBackgroundTask(
ShellSettings shellSettings,
IMediaFileStore mediaFileStore,
IWebHostEnvironment webHostEnvironment,
IMediaFileStoreCache mediaFileStoreCache,
IOptions<MediaOptions> mediaOptions,
ILogger<RemoteMediaCacheBackgroundTask> logger)
{
Expand All @@ -50,12 +49,6 @@ public async Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToke
return;
}

// Ensure that a remote media cache has been registered.
if (serviceProvider.GetService<IMediaFileStoreCache>() is null)
{
return;
}

Piedone marked this conversation as resolved.
Show resolved Hide resolved
// The min write time for an item to be retained in the cache,
// without having to get the item info from the remote store.
var minWriteTimeUtc = DateTimeOffset.UtcNow - _cacheMaxStale.Value;
Expand Down
9 changes: 3 additions & 6 deletions src/OrchardCore.Modules/OrchardCore.Media/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public override void ConfigureServices(IServiceCollection services)
return new DefaultMediaFileStore(fileStore, mediaUrlBase, mediaOptions.CdnBaseUrl, mediaEventHandlers, mediaCreatingEventHandlers, logger);
});

services.AddSingleton<IMediaFileStoreCache, NullMediaFileStoreCache>();

services.AddScoped<IPermissionProvider, Permissions>();
services.AddScoped<IAuthorizationHandler, ManageMediaFolderAuthorizationHandler>();
services.AddScoped<INavigationProvider, AdminMenu>();
Expand Down Expand Up @@ -197,13 +199,8 @@ public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder ro
{
var mediaFileProvider = serviceProvider.GetRequiredService<IMediaFileProvider>();
var mediaOptions = serviceProvider.GetRequiredService<IOptions<MediaOptions>>().Value;
var mediaFileStoreCache = serviceProvider.GetService<IMediaFileStoreCache>();

// FileStore middleware before ImageSharp, but only if a remote storage module has registered a cache provider.
if (mediaFileStoreCache != null)
{
app.UseMiddleware<MediaFileStoreResolverMiddleware>();
}
app.UseMiddleware<MediaFileStoreResolverMiddleware>();
hishamco marked this conversation as resolved.
Show resolved Hide resolved

// ImageSharp before the static file provider.
app.UseImageSharp();
Expand Down
19 changes: 19 additions & 0 deletions src/OrchardCore/OrchardCore.Media.Core/NullMediaFileStoreCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using OrchardCore.FileStorage;

namespace OrchardCore.Media.Core;

public class NullMediaFileStoreCache : IMediaFileStoreCache
{
public Task<bool> IsCachedAsync(string path) => Task.FromResult(false);

public Task<bool> PurgeAsync() => Task.FromResult(false);

public Task SetCacheAsync(Stream stream, IFileStoreEntry fileStoreEntry, CancellationToken cancellationToken) => Task.CompletedTask;

public Task<bool> TryDeleteDirectoryAsync(string path) => Task.FromResult(false);

public Task<bool> TryDeleteFileAsync(string path) => Task.FromResult(false);
}
Loading