Skip to content

Commit

Permalink
in dev environments, override the file provider to use a fallback ima…
Browse files Browse the repository at this point in the history
…ge for certain paths (#2062)
  • Loading branch information
Masterjun3 authored Jan 4, 2025
1 parent 129db21 commit 617784d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
15 changes: 11 additions & 4 deletions TASVideos/Extensions/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ public static IApplicationBuilder UseGzipCompression(this IApplicationBuilder ap

public static IApplicationBuilder UseStaticFilesWithExtensionMapping(this IApplicationBuilder app, IWebHostEnvironment env)
{
var provider = new FileExtensionContentTypeProvider();
return app.UseStaticFiles(new StaticFileOptions
var contentTypeProvider = new FileExtensionContentTypeProvider();
var staticFileOptions = new StaticFileOptions
{
ContentTypeProvider = provider,
ContentTypeProvider = contentTypeProvider,
ServeUnknownFileTypes = true,
DefaultContentType = "text/plain"
});
};

if (env.IsDevelopment())
{
staticFileOptions.FileProvider = new DevFallbackFileProvider(env.WebRootFileProvider);
}

return app.UseStaticFiles(staticFileOptions);
}

public static IApplicationBuilder UseMvcWithOptions(this IApplicationBuilder app, IHostEnvironment env, AppSettings settings)
Expand Down
28 changes: 28 additions & 0 deletions TASVideos/Services/DevFallbackFileProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;

namespace TASVideos.Services;

public class DevFallbackFileProvider(IFileProvider originalProvider) : IFileProvider
{
public IDirectoryContents GetDirectoryContents(string subpath)
{
return originalProvider.GetDirectoryContents(subpath);
}

public IFileInfo GetFileInfo(string subpath)
{
IFileInfo fileInfo = originalProvider.GetFileInfo(subpath);
if (!fileInfo.Exists && subpath.StartsWith("/media/"))
{
return originalProvider.GetFileInfo("/images/tasvideos_rss.png");
}

return fileInfo;
}

public IChangeToken Watch(string filter)
{
return originalProvider.Watch(filter);
}
}

0 comments on commit 617784d

Please sign in to comment.