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;
|
2016-10-25 21:02:04 +02:00
|
|
|
|
using MediaBrowser.Model.IO;
|
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
|
|
|
|
|
{
|
2017-08-09 21:56:38 +02:00
|
|
|
|
private static Dictionary<string, FileSystemMetadata> GetFileSystemDictionary(FileSystemMetadata[] list)
|
|
|
|
|
{
|
|
|
|
|
var dict = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
foreach (var file in list)
|
|
|
|
|
{
|
|
|
|
|
dict[file.FullName] = file;
|
|
|
|
|
}
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
2013-04-28 07:29:27 +02:00
|
|
|
|
/// 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>
|
2013-10-30 15:40:14 +01:00
|
|
|
|
/// <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>
|
2013-04-28 07:29:27 +02:00
|
|
|
|
/// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">path</exception>
|
2017-08-20 21:10:00 +02:00
|
|
|
|
public static FileSystemMetadata[] GetFilteredFileSystemEntries(IDirectoryService directoryService,
|
2015-12-01 04:54:24 +01:00
|
|
|
|
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))
|
|
|
|
|
{
|
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
|
|
|
|
|
2017-08-09 21:56:38 +02:00
|
|
|
|
var entries = directoryService.GetFileSystemEntries(path);
|
|
|
|
|
|
2013-06-20 16:40:05 +02:00
|
|
|
|
if (!resolveShortcuts && flattenFolderDepth == 0)
|
|
|
|
|
{
|
2017-08-20 21:10:00 +02:00
|
|
|
|
return entries;
|
2013-06-20 16:40:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-04 05:38:46 +02:00
|
|
|
|
var dict = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
|
2013-06-20 16:40:05 +02:00
|
|
|
|
|
2013-04-28 07:29:27 +02:00
|
|
|
|
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
|
|
|
|
|
2013-06-20 04:21:50 +02:00
|
|
|
|
var fullName = entry.FullName;
|
|
|
|
|
|
2013-10-30 15:40:14 +01:00
|
|
|
|
if (resolveShortcuts && fileSystem.IsShortcut(fullName))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-12-01 04:54:24 +01:00
|
|
|
|
try
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-12-01 04:54:24 +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
|
|
|
|
|
2015-12-01 04:54:24 +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
|
|
|
|
|
2015-12-01 04:54:24 +01:00
|
|
|
|
// add to our physical locations
|
|
|
|
|
args.AddAdditionalLocation(newPath);
|
2013-06-11 04:51:23 +02:00
|
|
|
|
|
2015-12-01 04:54:24 +01:00
|
|
|
|
dict[newPath] = data;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.ErrorException("Error resolving shortcut from {0}", ex, fullName);
|
|
|
|
|
}
|
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
|
|
|
|
{
|
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
|
|
|
|
{
|
2017-08-20 21:10:00 +02:00
|
|
|
|
dict[child.FullName] = child;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-20 04:21:50 +02:00
|
|
|
|
dict[fullName] = entry;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 21:52:19 +02:00
|
|
|
|
var returnResult = new FileSystemMetadata[dict.Count];
|
|
|
|
|
var index = 0;
|
|
|
|
|
var values = dict.Values;
|
|
|
|
|
foreach (var value in values)
|
|
|
|
|
{
|
|
|
|
|
returnResult[index] = value;
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
return returnResult;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2013-04-28 07:29:27 +02:00
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|