2013-02-23 18:54:51 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-02-21 21:26:35 +01:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2013-06-20 16:40:05 +02:00
|
|
|
|
using System.Linq;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.IO
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides low level File access that is much faster than the File/Directory api's
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class FileData
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2013-04-28 07:29:27 +02:00
|
|
|
|
/// Gets the filtered file system entries.
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
2013-02-21 22:06:23 +01:00
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-07-12 21:56:40 +02:00
|
|
|
|
/// <param name="args">The args.</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <param name="searchPattern">The search pattern.</param>
|
|
|
|
|
/// <param name="flattenFolderDepth">The flatten folder depth.</param>
|
2013-04-19 20:03:21 +02:00
|
|
|
|
/// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
|
2013-04-28 07:29:27 +02:00
|
|
|
|
/// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">path</exception>
|
2013-07-12 21:56:40 +02:00
|
|
|
|
public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(string path, ILogger logger, ItemResolveArgs args, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
2013-04-28 07:29:27 +02:00
|
|
|
|
throw new ArgumentNullException("path");
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2013-07-12 21:56:40 +02:00
|
|
|
|
if (args == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("args");
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-10 21:21:55 +02:00
|
|
|
|
var entries = new DirectoryInfo(path).EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-06-20 16:40:05 +02:00
|
|
|
|
if (!resolveShortcuts && flattenFolderDepth == 0)
|
|
|
|
|
{
|
|
|
|
|
return entries.ToDictionary(i => i.FullName, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
2013-04-28 07:29:27 +02:00
|
|
|
|
foreach (var entry in entries)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-06-11 22:35:54 +02:00
|
|
|
|
var isDirectory = (entry.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-06-20 04:21:50 +02:00
|
|
|
|
var fullName = entry.FullName;
|
|
|
|
|
|
|
|
|
|
if (resolveShortcuts && FileSystem.IsShortcut(fullName))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-06-20 04:21:50 +02:00
|
|
|
|
var newPath = FileSystem.ResolveShortcut(fullName);
|
2013-05-27 03:24:07 +02:00
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(newPath))
|
|
|
|
|
{
|
|
|
|
|
//invalid shortcut - could be old or target could just be unavailable
|
2013-06-20 04:21:50 +02:00
|
|
|
|
logger.Warn("Encountered invalid shortcut: " + fullName);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-11 04:51:23 +02:00
|
|
|
|
// Don't check if it exists here because that could return false for network shares.
|
|
|
|
|
var data = new DirectoryInfo(newPath);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-06-11 04:51:23 +02:00
|
|
|
|
// add to our physical locations
|
2013-07-12 21:56:40 +02:00
|
|
|
|
args.AddAdditionalLocation(newPath);
|
2013-06-11 04:51:23 +02:00
|
|
|
|
|
2013-06-20 04:21:50 +02:00
|
|
|
|
dict[newPath] = data;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2013-04-28 07:29:27 +02:00
|
|
|
|
else if (flattenFolderDepth > 0 && isDirectory)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-07-12 21:56:40 +02:00
|
|
|
|
foreach (var child in GetFilteredFileSystemEntries(fullName, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
dict[child.Key] = child.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-20 04:21:50 +02:00
|
|
|
|
dict[fullName] = entry;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
2013-04-28 07:29:27 +02:00
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|