jellyfin/MediaBrowser.Controller/IO/FileData.cs

87 lines
3.5 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Library;
2013-02-21 13:26:35 -07:00
using MediaBrowser.Model.Logging;
2013-02-20 18:33:05 -07:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2013-02-20 18:33:05 -07: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>
/// Gets the filtered file system entries.
2013-02-20 18:33:05 -07:00
/// </summary>
/// <param name="path">The path.</param>
2013-02-21 14:06:23 -07:00
/// <param name="logger">The logger.</param>
2013-02-20 18:33:05 -07:00
/// <param name="searchPattern">The search pattern.</param>
/// <param name="flattenFolderDepth">The flatten folder depth.</param>
2013-04-19 11:03:21 -07:00
/// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
2013-02-20 18:33:05 -07:00
/// <param name="args">The args.</param>
/// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
/// <exception cref="System.ArgumentNullException">path</exception>
public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(string path, ILogger logger, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true, ItemResolveArgs args = null)
2013-02-20 18:33:05 -07:00
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
2013-02-20 18:33:05 -07:00
}
var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
var entries = new DirectoryInfo(path).EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly)
.Where(i => !i.Attributes.HasFlag(FileAttributes.System) && !i.Name.Equals(".") && !i.Name.Equals(".."));
2013-02-20 18:33:05 -07:00
foreach (var entry in entries)
2013-02-20 18:33:05 -07:00
{
var isDirectory = entry.Attributes.HasFlag(FileAttributes.Directory);
2013-02-20 18:33:05 -07:00
if (resolveShortcuts && FileSystem.IsShortcut(entry.FullName))
2013-02-20 18:33:05 -07:00
{
var newPath = FileSystem.ResolveShortcut(entry.FullName);
2013-02-20 18:33:05 -07:00
if (string.IsNullOrWhiteSpace(newPath))
{
//invalid shortcut - could be old or target could just be unavailable
logger.Warn("Encountered invalid shortuct: " + entry.FullName);
2013-02-20 18:33:05 -07:00
continue;
}
var data = FileSystem.GetFileSystemInfo(newPath);
2013-02-20 18:33:05 -07:00
if (data.Exists)
2013-02-20 18:33:05 -07:00
{
// Find out if the shortcut is pointing to a directory or file
if (data.Attributes.HasFlag(FileAttributes.Directory))
2013-02-20 18:33:05 -07:00
{
// add to our physical locations
if (args != null)
{
args.AddAdditionalLocation(newPath);
}
}
dict[data.FullName] = data;
2013-02-20 18:33:05 -07:00
}
}
else if (flattenFolderDepth > 0 && isDirectory)
2013-02-20 18:33:05 -07:00
{
foreach (var child in GetFilteredFileSystemEntries(entry.FullName, logger, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
2013-02-20 18:33:05 -07:00
{
dict[child.Key] = child.Value;
}
}
else
{
dict[entry.FullName] = entry;
2013-02-20 18:33:05 -07:00
}
}
return dict;
}
2013-02-20 18:33:05 -07:00
}
}