aspnet/FileSystem

PhysicalFileProvider: improvements for filtering options

evil-shrike opened this issue · 2 comments

As continue of #280 (comment)
In PR enum ExclusionFilters was introduced to allow the physical file provider to show hidden files. I'd like to suggest more enhancements to PhysicalFileProvider.

  • enum flags ExclusionFilters can be fine but wouldn't be safer to add an options object to ctor signature to allow future feature additions:
public PhysicalFileProvider(string root, ExclusionFilters filters)

what if it will be needed to add something more? Another argument?
I suggest creating PhysicalFileProviderOptions:

public class PhysicalFileProviderOptions 
{
    ExclusionFilters ExclusionFilters { get;set }
}

and use it as ctor argument instead:

public PhysicalFileProvider(string root, PhysicalFileProviderOptions options)
  • Besides flags for exclusion modes I'd suggest adding Glob-patttens
public class PhysicalFileProviderOptions 
{
    ExclusionFilters ExclusionFilters { get;set }
    string[] ExcludePaths { get;set }
}

usage:

new PhysicalFileProvider("root", new PhysicalFileProviderOptions { 
  // I need include dot-files but exclude others (not very handy btw)
  ExclusionFilters = ExclusionFilters.Sensitive & ~ExclusionFilters.DotPrefixed ,
  // exclude ".idea" subfolder with all contentl; exclude *.map files indide 'src' folder
  ExcludePaths = new[] { ".idea/", "src/**/*.map" } 
});

I know you will say - use StaticFileOptions.OnPrepareResponse callback in StaticFilesMiddleware. But the same is appliable for dot-files - why not to filter them inside middleware instead of FileProvider?

  • serving files with dots if it's just js/css is definitely normal, why are they excluded by default?
    Moreover it's a breaking change as compared with IIS + ASP.NET 4.
    So everybody have to write this expression over and over:
    ExclusionFilters.Sensitive & ~ExclusionFilters.DotPrefixed
    Suggestion - include dot-files by default (but exclude folders starting with ".")
  • anyway of default, if we have the options object I'd suggest to move "exclude dot-files" option into a property:
public class PhysicalFileProviderOptions 
{
    ExclusionFilters ExcludeByAttributes { get;set }
    string[] Exclude { get;set }
    bool IncludeDotFile { get; set } // or ExcludeDotFiles
}
  • make GetFileInfo and GetDirectoryContents methods virtual to allow overriding PhysicalFileProvider
  • make PathUtils are public as currently it's hard to implement own PhysicalFileProvider with the similar logic - we have to copy-paste methods like PathNavigatesAboveRoot, HasInvalidPathChars and so on.
  • Support special files inside folder like .ignore which would tell FileProvider to ignore the folder with all content inside. The file name would be passed via options object
public class PhysicalFileProviderOptions 
{
    ExclusionFilters ExcludeByAttributes { get;set }
    string[] Exclude { get;set }
    bool IncludeDotFile { get; set } // or ExcludeDotFiles
    string IgnoreFileName {get;set;}
}

Looks like an interesting idea, but somewhat breaking. Putting this into the next major milestone tentatively.

This issue was moved to aspnet/Home#2540