jellyfin/MediaBrowser.Controller/IO/FileData.cs

104 lines
3.8 KiB
C#
Raw Normal View History

2016-03-27 23:11:27 +02:00
using MediaBrowser.Controller.Library;
2014-02-11 22:41:01 +01:00
using MediaBrowser.Controller.Providers;
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;
2015-10-04 06:23:11 +02:00
using CommonIO;
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>
/// Gets the filtered file system entries.
2013-02-21 02:33:05 +01:00
/// </summary>
2014-02-11 22:41:01 +01:00
/// <param name="directoryService">The directory service.</param>
2013-02-21 02:33:05 +01:00
/// <param name="path">The path.</param>
/// <param name="fileSystem">The file system.</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="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>
/// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
/// <exception cref="System.ArgumentNullException">path</exception>
public static Dictionary<string, FileSystemMetadata> GetFilteredFileSystemEntries(IDirectoryService directoryService,
string path,
IFileSystem fileSystem,
ILogger logger,
ItemResolveArgs args,
int flattenFolderDepth = 0,
2014-10-25 20:32:58 +02:00
bool resolveShortcuts = true)
2013-02-21 02:33:05 +01:00
{
if (string.IsNullOrEmpty(path))
{
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
if (!resolveShortcuts && flattenFolderDepth == 0)
{
2014-10-25 20:32:58 +02:00
return directoryService.GetFileSystemDictionary(path);
}
2014-10-25 20:32:58 +02:00
var entries = directoryService.GetFileSystemEntries(path);
2015-10-04 05:38:46 +02:00
var dict = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
foreach (var entry in entries)
2013-02-21 02:33:05 +01:00
{
2015-11-12 21:51:39 +01:00
var isDirectory = entry.IsDirectory;
2013-02-21 02:33:05 +01:00
var fullName = entry.FullName;
if (resolveShortcuts && fileSystem.IsShortcut(fullName))
2013-02-21 02:33:05 +01:00
{
try
2013-02-21 02:33:05 +01:00
{
var newPath = fileSystem.ResolveShortcut(fullName);
if (string.IsNullOrWhiteSpace(newPath))
{
//invalid shortcut - could be old or target could just be unavailable
logger.Warn("Encountered invalid shortcut: " + fullName);
continue;
}
2013-02-21 02:33:05 +01:00
// Don't check if it exists here because that could return false for network shares.
var data = fileSystem.GetDirectoryInfo(newPath);
2013-02-21 02:33:05 +01:00
// add to our physical locations
args.AddAdditionalLocation(newPath);
2013-06-11 04:51:23 +02:00
dict[newPath] = data;
}
catch (Exception ex)
{
logger.ErrorException("Error resolving shortcut from {0}", ex, fullName);
}
2013-02-21 02:33:05 +01:00
}
else if (flattenFolderDepth > 0 && isDirectory)
2013-02-21 02:33:05 +01:00
{
2014-02-11 22:41:01 +01:00
foreach (var child in GetFilteredFileSystemEntries(directoryService, fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
2013-02-21 02:33:05 +01:00
{
dict[child.Key] = child.Value;
}
}
else
{
dict[fullName] = entry;
2013-02-21 02:33:05 +01:00
}
}
return dict;
}
2013-02-21 02:33:05 +01:00
}
}