2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-11 21:41:01 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-02-21 20:26:35 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
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 05:29:27 +00:00
|
|
|
|
/// Gets the filtered file system entries.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2014-02-11 21:41:01 +00:00
|
|
|
|
/// <param name="directoryService">The directory service.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <param name="path">The path.</param>
|
2013-10-30 14:40:14 +00:00
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2013-02-21 21:06:23 +00:00
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-07-12 19:56:40 +00:00
|
|
|
|
/// <param name="args">The args.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <param name="flattenFolderDepth">The flatten folder depth.</param>
|
2013-04-19 18:03:21 +00:00
|
|
|
|
/// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
|
2013-04-28 05:29:27 +00:00
|
|
|
|
/// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">path</exception>
|
2014-02-11 21:41:01 +00:00
|
|
|
|
public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(IDirectoryService directoryService, string path, IFileSystem fileSystem, ILogger logger, ItemResolveArgs args, int flattenFolderDepth = 0, bool resolveShortcuts = true)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
2013-04-28 05:29:27 +00:00
|
|
|
|
throw new ArgumentNullException("path");
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2013-07-12 19:56:40 +00:00
|
|
|
|
if (args == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("args");
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2014-02-11 21:41:01 +00:00
|
|
|
|
var entries = directoryService.GetFileSystemEntries(path);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-06-20 14:40:05 +00:00
|
|
|
|
if (!resolveShortcuts && flattenFolderDepth == 0)
|
|
|
|
|
{
|
2013-08-25 17:18:56 +00:00
|
|
|
|
// Seeing dupes on some users file system for some reason
|
2013-09-18 02:43:34 +00:00
|
|
|
|
var dictionary = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
foreach (var info in entries)
|
|
|
|
|
{
|
|
|
|
|
dictionary[info.FullName] = info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dictionary;
|
2013-06-20 14:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
foreach (var entry in entries)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-06-11 20:35:54 +00:00
|
|
|
|
var isDirectory = (entry.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-06-20 02:21:50 +00:00
|
|
|
|
var fullName = entry.FullName;
|
|
|
|
|
|
2013-10-30 14:40:14 +00:00
|
|
|
|
if (resolveShortcuts && fileSystem.IsShortcut(fullName))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-10-30 14:40:14 +00:00
|
|
|
|
var newPath = fileSystem.ResolveShortcut(fullName);
|
2013-05-27 01:24:07 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(newPath))
|
|
|
|
|
{
|
|
|
|
|
//invalid shortcut - could be old or target could just be unavailable
|
2013-06-20 02:21:50 +00:00
|
|
|
|
logger.Warn("Encountered invalid shortcut: " + fullName);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-11 02:51:23 +00:00
|
|
|
|
// Don't check if it exists here because that could return false for network shares.
|
|
|
|
|
var data = new DirectoryInfo(newPath);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-06-11 02:51:23 +00:00
|
|
|
|
// add to our physical locations
|
2013-07-12 19:56:40 +00:00
|
|
|
|
args.AddAdditionalLocation(newPath);
|
2013-06-11 02:51:23 +00:00
|
|
|
|
|
2013-06-20 02:21:50 +00:00
|
|
|
|
dict[newPath] = data;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2013-04-28 05:29:27 +00:00
|
|
|
|
else if (flattenFolderDepth > 0 && isDirectory)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2014-02-11 21:41:01 +00:00
|
|
|
|
foreach (var child in GetFilteredFileSystemEntries(directoryService, fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
dict[child.Key] = child.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-20 02:21:50 +00:00
|
|
|
|
dict[fullName] = entry;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
2013-04-28 05:29:27 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|