2012-07-12 06:55:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2012-07-26 02:33:11 +00:00
|
|
|
|
using System.ComponentModel.Composition;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2012-08-20 13:36:18 +00:00
|
|
|
|
using System.Reflection;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2012-08-19 15:58:35 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
using MediaBrowser.Common.Kernel;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
using MediaBrowser.Controller.Events;
|
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2012-08-19 15:58:35 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
using MediaBrowser.Controller.Resolvers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2012-08-11 19:49:45 +00:00
|
|
|
|
using MediaBrowser.Model.Progress;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller
|
|
|
|
|
{
|
2012-08-18 20:38:02 +00:00
|
|
|
|
public class Kernel : BaseKernel<ServerConfiguration, ServerApplicationPaths>
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
public static Kernel Instance { get; private set; }
|
|
|
|
|
|
|
|
|
|
public ItemController ItemController { get; private set; }
|
|
|
|
|
|
|
|
|
|
public IEnumerable<User> Users { get; private set; }
|
|
|
|
|
public Folder RootFolder { get; private set; }
|
|
|
|
|
|
|
|
|
|
private DirectoryWatchers DirectoryWatchers { get; set; }
|
|
|
|
|
|
|
|
|
|
private string MediaRootFolderPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-02 03:13:44 +00:00
|
|
|
|
return ApplicationPaths.RootFolderPath;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-11 19:49:45 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list of currently registered metadata prvoiders
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ImportMany(typeof(BaseMetadataProvider))]
|
|
|
|
|
public IEnumerable<BaseMetadataProvider> MetadataProviders { get; private set; }
|
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list of currently registered entity resolvers
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ImportMany(typeof(IBaseItemResolver))]
|
|
|
|
|
public IEnumerable<IBaseItemResolver> EntityResolvers { get; private set; }
|
2012-08-11 19:49:45 +00:00
|
|
|
|
|
2012-07-12 06:55:27 +00:00
|
|
|
|
/// <summary>
|
2012-08-18 20:38:02 +00:00
|
|
|
|
/// Creates a kernel based on a Data path, which is akin to our current programdata path
|
2012-07-12 06:55:27 +00:00
|
|
|
|
/// </summary>
|
2012-07-21 18:39:47 +00:00
|
|
|
|
public Kernel()
|
|
|
|
|
: base()
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
|
|
|
|
|
ItemController = new ItemController();
|
|
|
|
|
DirectoryWatchers = new DirectoryWatchers();
|
|
|
|
|
|
|
|
|
|
ItemController.PreBeginResolvePath += ItemController_PreBeginResolvePath;
|
|
|
|
|
ItemController.BeginResolvePath += ItemController_BeginResolvePath;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
public async override Task Init(IProgress<TaskProgress> progress)
|
2012-08-11 19:49:45 +00:00
|
|
|
|
{
|
2012-08-19 22:35:45 +00:00
|
|
|
|
await Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
await base.Init(progress);
|
2012-08-11 19:49:45 +00:00
|
|
|
|
|
2012-08-19 22:35:45 +00:00
|
|
|
|
progress.Report(new TaskProgress() { Description = "Loading Users", PercentComplete = 15 });
|
|
|
|
|
ReloadUsers();
|
2012-08-11 19:49:45 +00:00
|
|
|
|
|
2012-08-20 13:36:18 +00:00
|
|
|
|
progress.Report(new TaskProgress() { Description = "Extracting FFMpeg", PercentComplete = 20 });
|
|
|
|
|
await ExtractFFMpeg();
|
|
|
|
|
|
|
|
|
|
progress.Report(new TaskProgress() { Description = "Loading Media Library", PercentComplete = 25 });
|
2012-08-19 22:35:45 +00:00
|
|
|
|
await ReloadRoot();
|
2012-08-11 19:49:45 +00:00
|
|
|
|
|
2012-08-19 22:35:45 +00:00
|
|
|
|
progress.Report(new TaskProgress() { Description = "Loading Complete", PercentComplete = 100 });
|
|
|
|
|
});
|
2012-08-11 19:49:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
protected override void OnComposablePartsLoaded()
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-07-26 13:51:26 +00:00
|
|
|
|
// The base class will start up all the plugins
|
2012-07-26 02:33:11 +00:00
|
|
|
|
base.OnComposablePartsLoaded();
|
2012-08-20 15:55:05 +00:00
|
|
|
|
|
|
|
|
|
// Initialize the metadata providers
|
2012-08-19 15:58:35 +00:00
|
|
|
|
Parallel.ForEach(MetadataProviders, provider =>
|
|
|
|
|
{
|
|
|
|
|
provider.Init();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 06:55:27 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires when a path is about to be resolved, but before child folders and files
|
|
|
|
|
/// have been collected from the file system.
|
|
|
|
|
/// This gives us a chance to cancel it if needed, resulting in the path being ignored
|
|
|
|
|
/// </summary>
|
|
|
|
|
void ItemController_PreBeginResolvePath(object sender, PreBeginResolveEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.IsHidden || e.IsSystemFile)
|
|
|
|
|
{
|
|
|
|
|
// Ignore hidden files and folders
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (Path.GetFileName(e.Path).Equals("trailers", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
// Ignore any folders named "trailers"
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires when a path is about to be resolved, but after child folders and files
|
|
|
|
|
/// This gives us a chance to cancel it if needed, resulting in the path being ignored
|
|
|
|
|
/// </summary>
|
|
|
|
|
void ItemController_BeginResolvePath(object sender, ItemResolveEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.IsFolder)
|
|
|
|
|
{
|
|
|
|
|
if (e.ContainsFile(".ignore"))
|
|
|
|
|
{
|
|
|
|
|
// Ignore any folders containing a file called .ignore
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReloadUsers()
|
|
|
|
|
{
|
2012-07-31 13:28:12 +00:00
|
|
|
|
Users = GetAllUsers();
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reloads the root media folder
|
|
|
|
|
/// </summary>
|
2012-08-19 15:58:35 +00:00
|
|
|
|
public async Task ReloadRoot()
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(MediaRootFolderPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(MediaRootFolderPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirectoryWatchers.Stop();
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
RootFolder = await ItemController.GetItem(null, MediaRootFolderPath) as Folder;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
|
|
|
|
DirectoryWatchers.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Guid GetMD5(string str)
|
|
|
|
|
{
|
2012-08-20 19:24:02 +00:00
|
|
|
|
using (var provider = new MD5CryptoServiceProvider())
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-20 19:24:02 +00:00
|
|
|
|
return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
public async Task ReloadItem(BaseItem item)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
Folder folder = item as Folder;
|
|
|
|
|
|
|
|
|
|
if (folder != null && folder.IsRoot)
|
|
|
|
|
{
|
2012-08-19 15:58:35 +00:00
|
|
|
|
await ReloadRoot();
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(item.Path) && !File.Exists(item.Path))
|
|
|
|
|
{
|
2012-08-19 15:58:35 +00:00
|
|
|
|
await ReloadItem(item.Parent);
|
2012-07-12 06:55:27 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
BaseItem newItem = await ItemController.GetItem(item.Parent, item.Path);
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
|
|
|
|
List<BaseItem> children = item.Parent.Children.ToList();
|
|
|
|
|
|
|
|
|
|
int index = children.IndexOf(item);
|
|
|
|
|
|
|
|
|
|
children.RemoveAt(index);
|
|
|
|
|
|
|
|
|
|
children.Insert(index, newItem);
|
|
|
|
|
|
|
|
|
|
item.Parent.Children = children.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-16 16:50:44 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds a library item by Id
|
|
|
|
|
/// </summary>
|
|
|
|
|
public BaseItem GetItemById(Guid id)
|
|
|
|
|
{
|
|
|
|
|
if (id == Guid.Empty)
|
|
|
|
|
{
|
|
|
|
|
return RootFolder;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-18 08:22:54 +00:00
|
|
|
|
return RootFolder.FindItemById(id);
|
2012-07-16 16:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-31 13:28:12 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all users within the system
|
|
|
|
|
/// </summary>
|
|
|
|
|
private IEnumerable<User> GetAllUsers()
|
|
|
|
|
{
|
|
|
|
|
List<User> list = new List<User>();
|
|
|
|
|
|
2012-08-02 12:51:43 +00:00
|
|
|
|
// Return a dummy user for now since all calls to get items requre a userId
|
|
|
|
|
User user = new User();
|
2012-07-31 13:28:12 +00:00
|
|
|
|
|
2012-08-02 12:51:43 +00:00
|
|
|
|
user.Name = "Default User";
|
2012-08-14 16:06:46 +00:00
|
|
|
|
user.Id = Guid.Parse("5d1cf7fce25943b790d140095457a42b");
|
2012-08-02 12:51:43 +00:00
|
|
|
|
|
|
|
|
|
list.Add(user);
|
2012-07-31 13:28:12 +00:00
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-20 13:36:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs all metadata providers for an entity
|
|
|
|
|
/// </summary>
|
2012-08-19 20:38:31 +00:00
|
|
|
|
internal async Task ExecuteMetadataProviders(BaseEntity item, ItemResolveEventArgs args)
|
|
|
|
|
{
|
2012-08-20 13:36:18 +00:00
|
|
|
|
// Get all supported providers
|
2012-08-19 20:38:31 +00:00
|
|
|
|
var supportedProviders = Kernel.Instance.MetadataProviders.Where(i => i.Supports(item));
|
|
|
|
|
|
2012-08-20 15:55:05 +00:00
|
|
|
|
// First priority providers
|
|
|
|
|
var providers = supportedProviders.Where(i => !i.RequiresInternet && i.Priority == MetadataProviderPriority.First);
|
|
|
|
|
|
|
|
|
|
if (providers.Any())
|
2012-08-19 20:38:31 +00:00
|
|
|
|
{
|
2012-08-20 15:55:05 +00:00
|
|
|
|
await Task.WhenAll(
|
|
|
|
|
providers.Select(i => i.Fetch(item, args))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Second priority providers
|
|
|
|
|
providers = supportedProviders.Where(i => !i.RequiresInternet && i.Priority == MetadataProviderPriority.Second);
|
|
|
|
|
|
|
|
|
|
if (providers.Any())
|
|
|
|
|
{
|
|
|
|
|
await Task.WhenAll(
|
|
|
|
|
providers.Select(i => i.Fetch(item, args))
|
|
|
|
|
);
|
2012-08-19 20:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 19:16:51 +00:00
|
|
|
|
// Third priority providers
|
|
|
|
|
providers = supportedProviders.Where(i => !i.RequiresInternet && i.Priority == MetadataProviderPriority.Third);
|
|
|
|
|
|
|
|
|
|
if (providers.Any())
|
|
|
|
|
{
|
|
|
|
|
await Task.WhenAll(
|
|
|
|
|
providers.Select(i => i.Fetch(item, args))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 15:55:05 +00:00
|
|
|
|
// Lowest priority providers
|
|
|
|
|
providers = supportedProviders.Where(i => !i.RequiresInternet && i.Priority == MetadataProviderPriority.Last);
|
|
|
|
|
|
|
|
|
|
if (providers.Any())
|
|
|
|
|
{
|
|
|
|
|
await Task.WhenAll(
|
|
|
|
|
providers.Select(i => i.Fetch(item, args))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute internet providers
|
|
|
|
|
providers = supportedProviders.Where(i => i.RequiresInternet);
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-20 15:55:05 +00:00
|
|
|
|
if (providers.Any())
|
2012-08-19 20:38:31 +00:00
|
|
|
|
{
|
|
|
|
|
await Task.WhenAll(
|
2012-08-20 15:55:05 +00:00
|
|
|
|
providers.Select(i => i.Fetch(item, args))
|
2012-08-19 20:38:31 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 13:36:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Run these during Init.
|
|
|
|
|
/// Can't run do this on-demand because there will be multiple workers accessing them at once and we'd have to lock them
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task ExtractFFMpeg()
|
|
|
|
|
{
|
|
|
|
|
// FFMpeg.exe
|
|
|
|
|
await ExtractFFMpeg(ApplicationPaths.FFMpegPath);
|
|
|
|
|
await ExtractFFMpeg(ApplicationPaths.FFProbePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ExtractFFMpeg(string exe)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(exe))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(exe);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract exe
|
|
|
|
|
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Controller.FFMpeg." + Path.GetFileName(exe)))
|
|
|
|
|
{
|
|
|
|
|
using (FileStream fileStream = new FileStream(exe, FileMode.Create))
|
|
|
|
|
{
|
|
|
|
|
await stream.CopyToAsync(fileStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
protected override void DisposeComposableParts()
|
|
|
|
|
{
|
|
|
|
|
base.DisposeComposableParts();
|
|
|
|
|
|
|
|
|
|
DisposeProviders();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes all providers
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void DisposeProviders()
|
|
|
|
|
{
|
|
|
|
|
if (MetadataProviders != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var provider in MetadataProviders)
|
|
|
|
|
{
|
|
|
|
|
provider.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|