1

I've setup static files and directory browsing like this:

    PhysicalFileProvider physicalFileProvider = new PhysicalFileProvider(somePath, ExclusionFilters.None);
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = physicalFileProvider,
        RequestPath = "/files",
        ServeUnknownFileTypes = true
    }); 

    app.UseDirectoryBrowser(new DirectoryBrowserOptions
    {
        FileProvider = new PhysicalFileProvider(somePath),
        RequestPath = "/files"
    });

I've been searching documentation and browsing through object models but I can't figure out how to set the include and exclude filters. My current code is filtering files starting with . (hidden? but I'm running on Windows ) I want to show and download these files but hide other types such as *.json and web.config.

4
  • Simplest will be to just put everything behind IFileProvider. All matching, providing, etc. Also new PhysicalFileProvider("root", ExclusionFilters.DotPrefixed) exclude everything hidden by dot prefix, so you can use this. Or just dont put everything in static files folder. Commented May 10, 2022 at 18:39
  • I don't follow. The intellisense for DotPrefixed says it excludes files and directories prefixed with a period. I need them included. Commented May 10, 2022 at 18:46
  • Then just overwrite file provider and pick whatever you need. Commented May 10, 2022 at 19:17
  • 1
    If you just want to do not display and download the file such as .json file, you can use app.UseWhen()> to judge the request if ends with .json to use the static file middleware. But if you also want to limit browsering file type by UseDirectoryBrowser, maybe you need custom PhysicalFileProvider. Commented May 11, 2022 at 6:17

1 Answer 1

2

This is a bit of a hack, but it worked for me. You can make a new fileprovider that uses PhysicalFileProvider (or anything else really) under the hood, but hides files according to a pattern.

public class TplFileProvider : IFileProvider
{
    private readonly IFileProvider fileProvider;

    public TplFileProvider(string root)
    {
        fileProvider = new PhysicalFileProvider(root);
    }
    public TplFileProvider(string root, ExclusionFilters exclusionFilter)
    {
        fileProvider = new PhysicalFileProvider(root, exclusionFilter);
    }

    public IDirectoryContents GetDirectoryContents(string subpath)
    {
        return (IDirectoryContents) fileProvider.GetDirectoryContents(subpath).Where(i => isAllowed(i.Name));
    }

    public IFileInfo GetFileInfo(string subpath)
    {
        var file = fileProvider.GetFileInfo(subpath);
        if (isAllowed(subpath))
        {
            return file;
        }
        else
        {
            return new NotFoundFileInfo(file.Name);
        }
    }

    private static bool isAllowed(string subpath)
    {
        return subpath.EndsWith(".json") || subpath.Equals("web.config");
    }
}

Edit: Here's a better way to do it using filetype whitelist:

var path = Path.GetFullPath(rawPath);

var mapping = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
    {
        { ".json", "application/json" },
        { ".jpg", "image/jpeg" },
        { ".png", "image/png" },
    };
staticFileOptions = new StaticFileOptions()
{
    RequestPath = "/StaticTiles",
    ServeUnknownFileTypes = false,
    ContentTypeProvider = new FileExtensionContentTypeProvider(mapping),
    FileProvider = new PhysicalFileProvider(path),
};
app.UseStaticFiles(staticFileOptions);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.