From 758d18a652a157240bd80e9e2db7b47688ba3d3b Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Mon, 20 Aug 2012 19:53:32 -0400 Subject: Switched to low-level io methods for better performance --- MediaBrowser.Controller/Library/ItemController.cs | 48 ++++++++++++----------- 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'MediaBrowser.Controller/Library/ItemController.cs') diff --git a/MediaBrowser.Controller/Library/ItemController.cs b/MediaBrowser.Controller/Library/ItemController.cs index 55deea835..92af513c5 100644 --- a/MediaBrowser.Controller/Library/ItemController.cs +++ b/MediaBrowser.Controller/Library/ItemController.cs @@ -20,13 +20,13 @@ namespace MediaBrowser.Controller.Library /// This gives listeners a chance to cancel the operation and cause the path to be ignored. /// public event EventHandler PreBeginResolvePath; - private bool OnPreBeginResolvePath(Folder parent, string path, FileAttributes attributes) + private bool OnPreBeginResolvePath(Folder parent, string path, WIN32_FIND_DATA fileData) { PreBeginResolveEventArgs args = new PreBeginResolveEventArgs() { Path = path, Parent = parent, - FileAttributes = attributes, + FileData = fileData, Cancel = false }; @@ -111,39 +111,41 @@ namespace MediaBrowser.Controller.Library /// public async Task GetItem(Folder parent, string path) { - return await GetItemInternal(parent, path, File.GetAttributes(path)).ConfigureAwait(false); + WIN32_FIND_DATA fileData = FileData.GetFileData(path); + + return await GetItemInternal(parent, path, fileData).ConfigureAwait(false); } /// /// Resolves a path into a BaseItem /// - private async Task GetItemInternal(Folder parent, string path, FileAttributes attributes) + private async Task GetItemInternal(Folder parent, string path, WIN32_FIND_DATA fileData) { - if (!OnPreBeginResolvePath(parent, path, attributes)) + if (!OnPreBeginResolvePath(parent, path, fileData)) { return null; } - IEnumerable> fileSystemChildren; + IEnumerable> fileSystemChildren; // Gather child folder and files - if (attributes.HasFlag(FileAttributes.Directory)) + if (fileData.IsDirectory) { - fileSystemChildren = Directory.GetFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair(f, File.GetAttributes(f))); + fileSystemChildren = Directory.GetFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair(f, FileData.GetFileData(f))); bool isVirtualFolder = parent != null && parent.IsRoot; fileSystemChildren = FilterChildFileSystemEntries(fileSystemChildren, isVirtualFolder); } else { - fileSystemChildren = new KeyValuePair[] { }; + fileSystemChildren = new KeyValuePair[] { }; } ItemResolveEventArgs args = new ItemResolveEventArgs() { Path = path, - FileAttributes = attributes, FileSystemChildren = fileSystemChildren, + FileData = fileData, Parent = parent, Cancel = false }; @@ -175,9 +177,9 @@ namespace MediaBrowser.Controller.Library /// /// Finds child BaseItems for a given Folder /// - private async Task AttachChildren(Folder folder, IEnumerable> fileSystemChildren) + private async Task AttachChildren(Folder folder, IEnumerable> fileSystemChildren) { - KeyValuePair[] fileSystemChildrenArray = fileSystemChildren.ToArray(); + KeyValuePair[] fileSystemChildrenArray = fileSystemChildren.ToArray(); int count = fileSystemChildrenArray.Length; @@ -203,15 +205,15 @@ namespace MediaBrowser.Controller.Library /// /// Transforms shortcuts into their actual paths /// - private List> FilterChildFileSystemEntries(IEnumerable> fileSystemChildren, bool flattenShortcuts) + private List> FilterChildFileSystemEntries(IEnumerable> fileSystemChildren, bool flattenShortcuts) { - List> returnFiles = new List>(); + List> returnFiles = new List>(); // Loop through each file - foreach (KeyValuePair file in fileSystemChildren) + foreach (KeyValuePair file in fileSystemChildren) { // Folders - if (file.Value.HasFlag(FileAttributes.Directory)) + if (file.Value.IsDirectory) { returnFiles.Add(file); } @@ -220,28 +222,28 @@ namespace MediaBrowser.Controller.Library else if (Shortcut.IsShortcut(file.Key)) { string newPath = Shortcut.ResolveShortcut(file.Key); - FileAttributes newPathAttributes = File.GetAttributes(newPath); + WIN32_FIND_DATA newPathData = FileData.GetFileData(newPath); // Find out if the shortcut is pointing to a directory or file - if (newPathAttributes.HasFlag(FileAttributes.Directory)) + if (newPathData.IsDirectory) { // If we're flattening then get the shortcut's children if (flattenShortcuts) { - IEnumerable> newChildren = Directory.GetFileSystemEntries(newPath, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair(f, File.GetAttributes(f))); + IEnumerable> newChildren = Directory.GetFileSystemEntries(newPath, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair(f, FileData.GetFileData(f))); returnFiles.AddRange(FilterChildFileSystemEntries(newChildren, false)); } else { - returnFiles.Add(new KeyValuePair(newPath, newPathAttributes)); + returnFiles.Add(new KeyValuePair(newPath, newPathData)); } } else { - returnFiles.Add(new KeyValuePair(newPath, newPathAttributes)); + returnFiles.Add(new KeyValuePair(newPath, newPathData)); } } else @@ -335,8 +337,8 @@ namespace MediaBrowser.Controller.Library ItemResolveEventArgs args = new ItemResolveEventArgs(); args.Path = path; - args.FileAttributes = File.GetAttributes(path); - args.FileSystemChildren = Directory.GetFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair(f, File.GetAttributes(f))); + args.FileData = FileData.GetFileData(path); + args.FileSystemChildren = Directory.GetFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair(f, FileData.GetFileData(f))); await Kernel.Instance.ExecuteMetadataProviders(item, args).ConfigureAwait(false); -- cgit v1.2.3