private static Func <FileInfo, FileSystemInfo, bool> GetFor(PathMatchingMode pathMatchingMode) { Func <FileInfo, FileInfo, bool>? fileMatcher = null; Func <FileInfo, DirectoryInfo, bool>?dirMatcher = null; if ((pathMatchingMode & PathMatchingMode.AnyMatchingDirOrSubDir) != 0) { dirMatcher = AnyDirMatcher; } else if ((pathMatchingMode & PathMatchingMode.ExactDirMatch) != 0) { dirMatcher = ExactDirMatcher; } if ((pathMatchingMode & PathMatchingMode.AnyMatchingFileName) != 0) { fileMatcher = AnyFileMatcher; } else if ((pathMatchingMode & PathMatchingMode.ExactFileMatch) != 0) { fileMatcher = ExactFileMatcher; } return((script, fileSystemInfo) => script.Exists && fileSystemInfo.Exists && fileSystemInfo is FileInfo fileInfo // if in the same directory and the file matches, or if in a subdirectory and it matches ? fileInfo.Directory?.FullName != script.Directory?.FullName ? dirMatcher?.Invoke(script, fileInfo.Directory !) ?? false : fileMatcher?.Invoke(script, fileInfo) ?? false : fileSystemInfo is DirectoryInfo directoryInfo && (dirMatcher?.Invoke(script, directoryInfo) ?? false));
/// <summary> /// Creates a handler from the given delegate and, optionally, path matching mode. /// </summary> /// <remarks> /// Example CSX file using this: /// <code> /// OppyHelpers.Handler /// ( /// async (context, nextHandler) => /// { /// Console.WriteLine("Hello!"); /// await nextHandler(); /// } /// ); /// </code> /// </remarks> /// <param name="handler">The handler delegate to use.</param> /// <param name="matchingMode">The default path matching mode to use.</param> /// <returns>A scripted handler object.</returns> public static HandlerScript Handler(Func <HttpContext, Func <Task>, Task> handler, PathMatchingMode matchingMode = PathMatchingMode.Default) { return(new HandlerScript(handler, matchingMode)); }
/// <summary> /// Creates a scripted handler using the given handler delegate and path matching mode (for the default path /// matcher) /// </summary> /// <param name="handler">The handler delegate to use.</param> /// <param name="pathMatchingMode">The path matching mode, used by the default path matcher.</param> public HandlerScript(Func <HttpContext, Func <Task>, Task> handler, PathMatchingMode pathMatchingMode) : this(handler, GetFor(pathMatchingMode)) { }