jellyfin/MediaBrowser.Controller/IO/FileData.cs

99 lines
3.5 KiB
C#
Raw Normal View History

using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Library;
2014-02-11 14:41:01 -07:00
using MediaBrowser.Controller.Providers;
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;
2015-10-03 21:23:11 -07:00
using CommonIO;
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>
2014-02-11 14:41:01 -07:00
/// <param name="directoryService">The directory service.</param>
2013-02-20 18:33:05 -07:00
/// <param name="path">The path.</param>
/// <param name="fileSystem">The file system.</param>
2013-02-21 14:06:23 -07:00
/// <param name="logger">The logger.</param>
2013-07-12 12:56:40 -07:00
/// <param name="args">The args.</param>
2013-02-20 18:33:05 -07:00
/// <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>
/// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
/// <exception cref="System.ArgumentNullException">path</exception>
2015-10-03 20:38:46 -07:00
public static Dictionary<string, FileSystemMetadata> GetFilteredFileSystemEntries(IDirectoryService directoryService,
2014-10-25 11:32:58 -07:00
string path,
IFileSystem fileSystem,
ILogger logger,
ItemResolveArgs args,
int flattenFolderDepth = 0,
bool resolveShortcuts = true)
2013-02-20 18:33:05 -07:00
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
2013-02-20 18:33:05 -07:00
}
2013-07-12 12:56:40 -07:00
if (args == null)
{
throw new ArgumentNullException("args");
}
2013-02-20 18:33:05 -07:00
if (!resolveShortcuts && flattenFolderDepth == 0)
{
2014-10-25 11:32:58 -07:00
return directoryService.GetFileSystemDictionary(path);
}
2014-10-25 11:32:58 -07:00
var entries = directoryService.GetFileSystemEntries(path);
2015-10-03 20:38:46 -07:00
var dict = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
foreach (var entry in entries)
2013-02-20 18:33:05 -07:00
{
2015-11-12 13:51:39 -07:00
var isDirectory = entry.IsDirectory;
2013-02-20 18:33:05 -07:00
var fullName = entry.FullName;
if (resolveShortcuts && fileSystem.IsShortcut(fullName))
2013-02-20 18:33:05 -07:00
{
var newPath = fileSystem.ResolveShortcut(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 shortcut: " + fullName);
2013-02-20 18:33:05 -07:00
continue;
}
2013-06-10 19:51:23 -07:00
// Don't check if it exists here because that could return false for network shares.
2015-10-03 20:38:46 -07:00
var data = fileSystem.GetDirectoryInfo(newPath);
2013-02-20 18:33:05 -07:00
2013-06-10 19:51:23 -07:00
// add to our physical locations
2013-07-12 12:56:40 -07:00
args.AddAdditionalLocation(newPath);
2013-06-10 19:51:23 -07:00
dict[newPath] = data;
2013-02-20 18:33:05 -07:00
}
else if (flattenFolderDepth > 0 && isDirectory)
2013-02-20 18:33:05 -07:00
{
2014-02-11 14:41:01 -07:00
foreach (var child in GetFilteredFileSystemEntries(directoryService, fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
2013-02-20 18:33:05 -07:00
{
dict[child.Key] = child.Value;
}
}
else
{
dict[fullName] = entry;
2013-02-20 18:33:05 -07:00
}
}
return dict;
}
2013-02-20 18:33:05 -07:00
}
}