From f218e6b5832ad2eec2c6391bf2f942ecd6a0330f Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Thu, 23 Aug 2012 16:51:10 -0400 Subject: More comments and cleanup. Added special feature provider for movies --- MediaBrowser.Controller/IO/FileData.cs | 61 ++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 11 deletions(-) (limited to 'MediaBrowser.Controller/IO/FileData.cs') diff --git a/MediaBrowser.Controller/IO/FileData.cs b/MediaBrowser.Controller/IO/FileData.cs index eca166ead..21d090a27 100644 --- a/MediaBrowser.Controller/IO/FileData.cs +++ b/MediaBrowser.Controller/IO/FileData.cs @@ -1,32 +1,61 @@ using System; +using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; -using System.Runtime.ConstrainedExecution; -using Microsoft.Win32.SafeHandles; -using System.Collections.Generic; -using System.Linq; - namespace MediaBrowser.Controller.IO { + /// + /// Provides low level File access that is much faster than the File/Directory api's + /// public static class FileData { public const int MAX_PATH = 260; public const int MAX_ALTERNATE = 14; - public static WIN32_FIND_DATA GetFileData(string fileName) + /// + /// Gets information about a path + /// + public static WIN32_FIND_DATA GetFileData(string path) { WIN32_FIND_DATA data; - IntPtr handle = FindFirstFile(fileName, out data); + IntPtr handle = FindFirstFile(path, out data); if (handle == IntPtr.Zero) throw new IOException("FindFirstFile failed"); FindClose(handle); - data.Path = fileName; + data.Path = path; return data; } + /// + /// Gets all file system entries within a foler + /// public static IEnumerable GetFileSystemEntries(string path, string searchPattern) + { + return GetFileSystemEntries(path, searchPattern, true, true); + } + + /// + /// Gets all files within a folder + /// + public static IEnumerable GetFiles(string path, string searchPattern) + { + return GetFileSystemEntries(path, searchPattern, true, false); + } + + /// + /// Gets all sub-directories within a folder + /// + public static IEnumerable GetDirectories(string path, string searchPattern) + { + return GetFileSystemEntries(path, searchPattern, false, true); + } + + /// + /// Gets all file system entries within a foler + /// + public static IEnumerable GetFileSystemEntries(string path, string searchPattern, bool includeFiles, bool includeDirectories) { string lpFileName = Path.Combine(path, searchPattern); @@ -43,14 +72,14 @@ namespace MediaBrowser.Controller.IO yield break; } - if (IsValid(lpFindFileData.cFileName)) + if (IncludeInOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories)) { yield return lpFindFileData; } while (FindNextFile(handle, out lpFindFileData) != IntPtr.Zero) { - if (IsValid(lpFindFileData.cFileName)) + if (IncludeInOutput(lpFindFileData.cFileName, lpFindFileData.dwFileAttributes, includeFiles, includeDirectories)) { lpFindFileData.Path = Path.Combine(path, lpFindFileData.cFileName); yield return lpFindFileData; @@ -60,7 +89,7 @@ namespace MediaBrowser.Controller.IO FindClose(handle); } - private static bool IsValid(string cFileName) + private static bool IncludeInOutput(string cFileName, FileAttributes attributes, bool includeFiles, bool includeDirectories) { if (cFileName.Equals(".", StringComparison.OrdinalIgnoreCase)) { @@ -71,6 +100,16 @@ namespace MediaBrowser.Controller.IO return false; } + if (!includeFiles && !attributes.HasFlag(FileAttributes.Directory)) + { + return false; + } + + if (!includeDirectories && attributes.HasFlag(FileAttributes.Directory)) + { + return false; + } + return true; } -- cgit v1.2.3