StaticFiles constructor

StaticFiles(String baseDir, { List<String> defaultFilenames, bool allowFilePathsAsDirectories: true, bool allowDirectoryListing: false })

Constructor

Requests for a directory (i.e. path ending in "/") returns one of the defaultFilenames in the directory (if it is set and a file exists), otherwise if allowDirectoryListing is true a listing of the directory is produced, otherwise an exception is thrown.

Implementation

StaticFiles(String baseDir,
    {List<String> defaultFilenames,
    this.allowFilePathsAsDirectories = true,
    this.allowDirectoryListing = false}) {
  // Check if directory is usable.
  if (baseDir == null) {
    throw new ArgumentError.notNull("baseDir");
  }
  if (baseDir.isEmpty) {
    throw new ArgumentError.value(
        baseDir, "baseDir", "empty string not permitted for StaticFiles");
  }
  while (baseDir.endsWith("/")) {
    // Remove all trailing slashes
    baseDir = baseDir.substring(0, baseDir.length - 1);
  }
  if (baseDir.isEmpty) {
    throw new ArgumentError.value(
        "/", "baseDir", "not permitted for StaticFiles");
  }
  if (["/bin", "/etc", "/home", "/lib", "/tmp", "/var"].contains(baseDir)) {
    throw new ArgumentError.value(
        baseDir, "baseDir", "not permitted for StaticFiles");
  }
  if (!new Directory(baseDir).existsSync()) {
    throw new ArgumentError.value(
        baseDir, "baseDir", "directory does not exist for StaticFiles");
  }
  assert(baseDir.isNotEmpty);

  _baseDir = baseDir;
  this.defaultFilenames = defaultFilenames ?? [];
}