/// <summary> /// Locate a file at the given path by directly mapping path segments to physical directories. /// </summary> /// <param name="subpath">A path under the root directory</param> /// <returns>The file information. Caller must check Exists property. </returns> public IFileInfo GetFileInfo(string subpath) { if (string.IsNullOrEmpty(subpath) || PathUtils.HasInvalidPathChars(subpath)) { return(new NotFoundFileInfo(subpath)); } // Relative paths starting with leading slashes are okay subpath = subpath.TrimStart(_pathSeparators); // Absolute paths not permitted. if (Path.IsPathRooted(subpath)) { return(new NotFoundFileInfo(subpath)); } var fullPath = GetFullPath(subpath); if (fullPath == null) { return(new NotFoundFileInfo(subpath)); } var fileInfo = new FileInfo(fullPath); if (FileSystemInfoHelper.IsHiddenFile(fileInfo)) { return(new NotFoundFileInfo(subpath)); } return(new PhysicalFileInfo(fileInfo)); }
/// <summary> /// Locate a file at the given path by directly mapping path segments to physical directories. /// </summary> /// <param name="subpath">A path under the root directory</param> /// <returns>The file information. Caller must check Exists property. </returns> public IFileInfo GetFileInfo(string subpath) { if (string.IsNullOrEmpty(subpath) || HasInvalidPathChars(subpath)) { return(new NotFoundFileInfo(subpath)); } // Relative paths starting with a leading slash okay if (subpath.StartsWith("/", StringComparison.Ordinal)) { subpath = subpath.Substring(1); } // Absolute paths not permitted. if (Path.IsPathRooted(subpath)) { return(new NotFoundFileInfo(subpath)); } var fullPath = GetFullPath(subpath); if (fullPath == null) { return(new NotFoundFileInfo(subpath)); } var fileInfo = new FileInfo(fullPath); if (FileSystemInfoHelper.IsHiddenFile(fileInfo)) { return(new NotFoundFileInfo(subpath)); } return(new PhysicalFileInfo(fileInfo)); }
/// <summary> /// Enumerate a directory at the given path, if any. /// </summary> /// <param name="subpath">A path under the root directory</param> /// <returns>Contents of the directory. Caller must check Exists property.</returns> public IDirectoryContents GetDirectoryContents(string subpath) { try { if (subpath == null || HasInvalidPathChars(subpath)) { return(new NotFoundDirectoryContents()); } // Relative paths starting with a leading slash okay if (subpath.StartsWith("/", StringComparison.Ordinal)) { subpath = subpath.Substring(1); } // Absolute paths not permitted. if (Path.IsPathRooted(subpath)) { return(new NotFoundDirectoryContents()); } var fullPath = GetFullPath(subpath); if (fullPath != null) { var directoryInfo = new DirectoryInfo(fullPath); if (!directoryInfo.Exists) { return(new NotFoundDirectoryContents()); } var physicalInfos = directoryInfo .EnumerateFileSystemInfos() .Where(info => !FileSystemInfoHelper.IsHiddenFile(info)); var virtualInfos = new List <IFileInfo>(); foreach (var fileSystemInfo in physicalInfos) { var fileInfo = fileSystemInfo as FileInfo; if (fileInfo != null) { virtualInfos.Add(new PhysicalFileInfo(fileInfo)); } else { virtualInfos.Add(new PhysicalDirectoryInfo((DirectoryInfo)fileSystemInfo)); } } return(new EnumerableDirectoryContents(virtualInfos)); } } catch (DirectoryNotFoundException) { } catch (IOException) { } return(new NotFoundDirectoryContents()); }