mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
fix merge conflicts
This commit is contained in:
parent
e7e0a1ecfc
commit
bc58e2862c
@ -1,634 +0,0 @@
|
|||||||
using MediaBrowser.Common.ScheduledTasks;
|
|
||||||
using MediaBrowser.Controller.Configuration;
|
|
||||||
using MediaBrowser.Controller.Entities;
|
|
||||||
using MediaBrowser.Controller.Library;
|
|
||||||
using MediaBrowser.Controller.Plugins;
|
|
||||||
using MediaBrowser.Model.Configuration;
|
|
||||||
using MediaBrowser.Model.Logging;
|
|
||||||
using Microsoft.Win32;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CommonIO;
|
|
||||||
using MediaBrowser.Controller;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Server.Implementations.IO
|
|
||||||
{
|
|
||||||
public class LibraryMonitor : ILibraryMonitor
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// The file system watchers
|
|
||||||
/// </summary>
|
|
||||||
private readonly ConcurrentDictionary<string, FileSystemWatcher> _fileSystemWatchers = new ConcurrentDictionary<string, FileSystemWatcher>(StringComparer.OrdinalIgnoreCase);
|
|
||||||
/// <summary>
|
|
||||||
/// The affected paths
|
|
||||||
/// </summary>
|
|
||||||
private readonly List<FileRefresher> _activeRefreshers = new List<FileRefresher>();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A dynamic list of paths that should be ignored. Added to during our own file sytem modifications.
|
|
||||||
/// </summary>
|
|
||||||
private readonly ConcurrentDictionary<string, string> _tempIgnoredPaths = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Any file name ending in any of these will be ignored by the watchers
|
|
||||||
/// </summary>
|
|
||||||
private readonly IReadOnlyList<string> _alwaysIgnoreFiles = new List<string>
|
|
||||||
{
|
|
||||||
"small.jpg",
|
|
||||||
"albumart.jpg",
|
|
||||||
|
|
||||||
// WMC temp recording directories that will constantly be written to
|
|
||||||
"TempRec",
|
|
||||||
"TempSBE"
|
|
||||||
};
|
|
||||||
|
|
||||||
private readonly IReadOnlyList<string> _alwaysIgnoreSubstrings = new List<string>
|
|
||||||
{
|
|
||||||
// Synology
|
|
||||||
"eaDir",
|
|
||||||
"#recycle",
|
|
||||||
".wd_tv",
|
|
||||||
".actors"
|
|
||||||
};
|
|
||||||
|
|
||||||
private readonly IReadOnlyList<string> _alwaysIgnoreExtensions = new List<string>
|
|
||||||
{
|
|
||||||
// thumbs.db
|
|
||||||
".db",
|
|
||||||
|
|
||||||
// bts sync files
|
|
||||||
".bts",
|
|
||||||
".sync"
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Add the path to our temporary ignore list. Use when writing to a path within our listening scope.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">The path.</param>
|
|
||||||
private void TemporarilyIgnore(string path)
|
|
||||||
{
|
|
||||||
_tempIgnoredPaths[path] = path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ReportFileSystemChangeBeginning(string path)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(path))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("path");
|
|
||||||
}
|
|
||||||
|
|
||||||
TemporarilyIgnore(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsPathLocked(string path)
|
|
||||||
{
|
|
||||||
var lockedPaths = _tempIgnoredPaths.Keys.ToList();
|
|
||||||
return lockedPaths.Any(i => string.Equals(i, path, StringComparison.OrdinalIgnoreCase) || _fileSystem.ContainsSubPath(i, path));
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void ReportFileSystemChangeComplete(string path, bool refreshPath)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(path))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("path");
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is an arbitraty amount of time, but delay it because file system writes often trigger events long after the file was actually written to.
|
|
||||||
// Seeing long delays in some situations, especially over the network, sometimes up to 45 seconds
|
|
||||||
// But if we make this delay too high, we risk missing legitimate changes, such as user adding a new file, or hand-editing metadata
|
|
||||||
await Task.Delay(45000).ConfigureAwait(false);
|
|
||||||
|
|
||||||
string val;
|
|
||||||
_tempIgnoredPaths.TryRemove(path, out val);
|
|
||||||
|
|
||||||
if (refreshPath)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ReportFileSystemChanged(path);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.ErrorException("Error in ReportFileSystemChanged for {0}", ex, path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the logger.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The logger.</value>
|
|
||||||
private ILogger Logger { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the task manager.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The task manager.</value>
|
|
||||||
private ITaskManager TaskManager { get; set; }
|
|
||||||
|
|
||||||
private ILibraryManager LibraryManager { get; set; }
|
|
||||||
private IServerConfigurationManager ConfigurationManager { get; set; }
|
|
||||||
|
|
||||||
private readonly IFileSystem _fileSystem;
|
|
||||||
private readonly IServerApplicationHost _appHost;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="LibraryMonitor" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public LibraryMonitor(ILogManager logManager, ITaskManager taskManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem, IServerApplicationHost appHost)
|
|
||||||
{
|
|
||||||
if (taskManager == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("taskManager");
|
|
||||||
}
|
|
||||||
|
|
||||||
LibraryManager = libraryManager;
|
|
||||||
TaskManager = taskManager;
|
|
||||||
Logger = logManager.GetLogger(GetType().Name);
|
|
||||||
ConfigurationManager = configurationManager;
|
|
||||||
_fileSystem = fileSystem;
|
|
||||||
_appHost = appHost;
|
|
||||||
|
|
||||||
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles the PowerModeChanged event of the SystemEvents control.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">The source of the event.</param>
|
|
||||||
/// <param name="e">The <see cref="PowerModeChangedEventArgs"/> instance containing the event data.</param>
|
|
||||||
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
|
|
||||||
{
|
|
||||||
Restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Restart()
|
|
||||||
{
|
|
||||||
Stop();
|
|
||||||
Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsLibraryMonitorEnabaled(BaseItem item)
|
|
||||||
{
|
|
||||||
if (item is BasePluginFolder)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var options = LibraryManager.GetLibraryOptions(item);
|
|
||||||
|
|
||||||
if (options != null)
|
|
||||||
{
|
|
||||||
return options.EnableRealtimeMonitor;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Start()
|
|
||||||
{
|
|
||||||
LibraryManager.ItemAdded += LibraryManager_ItemAdded;
|
|
||||||
LibraryManager.ItemRemoved += LibraryManager_ItemRemoved;
|
|
||||||
|
|
||||||
var pathsToWatch = new List<string> { };
|
|
||||||
|
|
||||||
var paths = LibraryManager
|
|
||||||
.RootFolder
|
|
||||||
.Children
|
|
||||||
.Where(IsLibraryMonitorEnabaled)
|
|
||||||
.OfType<Folder>()
|
|
||||||
.SelectMany(f => f.PhysicalLocations)
|
|
||||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
||||||
.OrderBy(i => i)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
foreach (var path in paths)
|
|
||||||
{
|
|
||||||
if (!ContainsParentFolder(pathsToWatch, path))
|
|
||||||
{
|
|
||||||
pathsToWatch.Add(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var path in pathsToWatch)
|
|
||||||
{
|
|
||||||
StartWatchingPath(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void StartWatching(BaseItem item)
|
|
||||||
{
|
|
||||||
if (IsLibraryMonitorEnabaled(item))
|
|
||||||
{
|
|
||||||
StartWatchingPath(item.Path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles the ItemRemoved event of the LibraryManager control.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">The source of the event.</param>
|
|
||||||
/// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param>
|
|
||||||
void LibraryManager_ItemRemoved(object sender, ItemChangeEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.Item.GetParent() is AggregateFolder)
|
|
||||||
{
|
|
||||||
StopWatchingPath(e.Item.Path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles the ItemAdded event of the LibraryManager control.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">The source of the event.</param>
|
|
||||||
/// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param>
|
|
||||||
void LibraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.Item.GetParent() is AggregateFolder)
|
|
||||||
{
|
|
||||||
StartWatching(e.Item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Examine a list of strings assumed to be file paths to see if it contains a parent of
|
|
||||||
/// the provided path.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="lst">The LST.</param>
|
|
||||||
/// <param name="path">The path.</param>
|
|
||||||
/// <returns><c>true</c> if [contains parent folder] [the specified LST]; otherwise, <c>false</c>.</returns>
|
|
||||||
/// <exception cref="System.ArgumentNullException">path</exception>
|
|
||||||
private static bool ContainsParentFolder(IEnumerable<string> lst, string path)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(path))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("path");
|
|
||||||
}
|
|
||||||
|
|
||||||
path = path.TrimEnd(Path.DirectorySeparatorChar);
|
|
||||||
|
|
||||||
return lst.Any(str =>
|
|
||||||
{
|
|
||||||
//this should be a little quicker than examining each actual parent folder...
|
|
||||||
var compare = str.TrimEnd(Path.DirectorySeparatorChar);
|
|
||||||
|
|
||||||
return path.Equals(compare, StringComparison.OrdinalIgnoreCase) || (path.StartsWith(compare, StringComparison.OrdinalIgnoreCase) && path[compare.Length] == Path.DirectorySeparatorChar);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Starts the watching path.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">The path.</param>
|
|
||||||
private void StartWatchingPath(string path)
|
|
||||||
{
|
|
||||||
// Creating a FileSystemWatcher over the LAN can take hundreds of milliseconds, so wrap it in a Task to do them all in parallel
|
|
||||||
Task.Run(() =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var newWatcher = new FileSystemWatcher(path, "*")
|
|
||||||
{
|
|
||||||
IncludeSubdirectories = true
|
|
||||||
};
|
|
||||||
|
|
||||||
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
|
|
||||||
{
|
|
||||||
newWatcher.InternalBufferSize = 32767;
|
|
||||||
}
|
|
||||||
|
|
||||||
newWatcher.NotifyFilter = NotifyFilters.CreationTime |
|
|
||||||
NotifyFilters.DirectoryName |
|
|
||||||
NotifyFilters.FileName |
|
|
||||||
NotifyFilters.LastWrite |
|
|
||||||
NotifyFilters.Size |
|
|
||||||
NotifyFilters.Attributes;
|
|
||||||
|
|
||||||
newWatcher.Created += watcher_Changed;
|
|
||||||
newWatcher.Deleted += watcher_Changed;
|
|
||||||
newWatcher.Renamed += watcher_Changed;
|
|
||||||
newWatcher.Changed += watcher_Changed;
|
|
||||||
|
|
||||||
newWatcher.Error += watcher_Error;
|
|
||||||
|
|
||||||
if (_fileSystemWatchers.TryAdd(path, newWatcher))
|
|
||||||
{
|
|
||||||
newWatcher.EnableRaisingEvents = true;
|
|
||||||
Logger.Info("Watching directory " + path);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Logger.Info("Unable to add directory watcher for {0}. It already exists in the dictionary.", path);
|
|
||||||
newWatcher.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.ErrorException("Error watching path: {0}", ex, path);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops the watching path.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">The path.</param>
|
|
||||||
private void StopWatchingPath(string path)
|
|
||||||
{
|
|
||||||
FileSystemWatcher watcher;
|
|
||||||
|
|
||||||
if (_fileSystemWatchers.TryGetValue(path, out watcher))
|
|
||||||
{
|
|
||||||
DisposeWatcher(watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Disposes the watcher.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="watcher">The watcher.</param>
|
|
||||||
private void DisposeWatcher(FileSystemWatcher watcher)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (watcher)
|
|
||||||
{
|
|
||||||
Logger.Info("Stopping directory watching for path {0}", watcher.Path);
|
|
||||||
|
|
||||||
watcher.EnableRaisingEvents = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
RemoveWatcherFromList(watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes the watcher from list.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="watcher">The watcher.</param>
|
|
||||||
private void RemoveWatcherFromList(FileSystemWatcher watcher)
|
|
||||||
{
|
|
||||||
FileSystemWatcher removed;
|
|
||||||
|
|
||||||
_fileSystemWatchers.TryRemove(watcher.Path, out removed);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles the Error event of the watcher control.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">The source of the event.</param>
|
|
||||||
/// <param name="e">The <see cref="ErrorEventArgs" /> instance containing the event data.</param>
|
|
||||||
void watcher_Error(object sender, ErrorEventArgs e)
|
|
||||||
{
|
|
||||||
var ex = e.GetException();
|
|
||||||
var dw = (FileSystemWatcher)sender;
|
|
||||||
|
|
||||||
Logger.ErrorException("Error in Directory watcher for: " + dw.Path, ex);
|
|
||||||
|
|
||||||
DisposeWatcher(dw);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles the Changed event of the watcher control.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">The source of the event.</param>
|
|
||||||
/// <param name="e">The <see cref="FileSystemEventArgs" /> instance containing the event data.</param>
|
|
||||||
void watcher_Changed(object sender, FileSystemEventArgs e)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Logger.Debug("Changed detected of type " + e.ChangeType + " to " + e.FullPath);
|
|
||||||
|
|
||||||
var path = e.FullPath;
|
|
||||||
|
|
||||||
// For deletes, use the parent path
|
|
||||||
if (e.ChangeType == WatcherChangeTypes.Deleted)
|
|
||||||
{
|
|
||||||
var parentPath = Path.GetDirectoryName(path);
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(parentPath))
|
|
||||||
{
|
|
||||||
path = parentPath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ReportFileSystemChanged(path);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.ErrorException("Exception in ReportFileSystemChanged. Path: {0}", ex, e.FullPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ReportFileSystemChanged(string path)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(path))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException("path");
|
|
||||||
}
|
|
||||||
|
|
||||||
var filename = Path.GetFileName(path);
|
|
||||||
|
|
||||||
var monitorPath = !string.IsNullOrEmpty(filename) &&
|
|
||||||
!_alwaysIgnoreFiles.Contains(filename, StringComparer.OrdinalIgnoreCase) &&
|
|
||||||
!_alwaysIgnoreExtensions.Contains(Path.GetExtension(path) ?? string.Empty, StringComparer.OrdinalIgnoreCase) &&
|
|
||||||
_alwaysIgnoreSubstrings.All(i => path.IndexOf(i, StringComparison.OrdinalIgnoreCase) == -1);
|
|
||||||
|
|
||||||
// Ignore certain files
|
|
||||||
var tempIgnorePaths = _tempIgnoredPaths.Keys.ToList();
|
|
||||||
|
|
||||||
// If the parent of an ignored path has a change event, ignore that too
|
|
||||||
if (tempIgnorePaths.Any(i =>
|
|
||||||
{
|
|
||||||
if (string.Equals(i, path, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
Logger.Debug("Ignoring change to {0}", path);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_fileSystem.ContainsSubPath(i, path))
|
|
||||||
{
|
|
||||||
Logger.Debug("Ignoring change to {0}", path);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Go up a level
|
|
||||||
var parent = Path.GetDirectoryName(i);
|
|
||||||
if (!string.IsNullOrEmpty(parent))
|
|
||||||
{
|
|
||||||
if (string.Equals(parent, path, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
Logger.Debug("Ignoring change to {0}", path);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
|
|
||||||
}))
|
|
||||||
{
|
|
||||||
monitorPath = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (monitorPath)
|
|
||||||
{
|
|
||||||
// Avoid implicitly captured closure
|
|
||||||
CreateRefresher(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CreateRefresher(string path)
|
|
||||||
{
|
|
||||||
var parentPath = Path.GetDirectoryName(path);
|
|
||||||
|
|
||||||
lock (_activeRefreshers)
|
|
||||||
{
|
|
||||||
var refreshers = _activeRefreshers.ToList();
|
|
||||||
foreach (var refresher in refreshers)
|
|
||||||
{
|
|
||||||
// Path is already being refreshed
|
|
||||||
if (string.Equals(path, refresher.Path, StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
refresher.RestartTimer();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parent folder is already being refreshed
|
|
||||||
if (_fileSystem.ContainsSubPath(refresher.Path, path))
|
|
||||||
{
|
|
||||||
refresher.AddPath(path);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// New path is a parent
|
|
||||||
if (_fileSystem.ContainsSubPath(path, refresher.Path))
|
|
||||||
{
|
|
||||||
refresher.ResetPath(path, null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// They are siblings. Rebase the refresher to the parent folder.
|
|
||||||
if (string.Equals(parentPath, Path.GetDirectoryName(refresher.Path), StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
refresher.ResetPath(parentPath, path);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var newRefresher = new FileRefresher(path, _fileSystem, ConfigurationManager, LibraryManager, TaskManager, Logger);
|
|
||||||
newRefresher.Completed += NewRefresher_Completed;
|
|
||||||
_activeRefreshers.Add(newRefresher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void NewRefresher_Completed(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
var refresher = (FileRefresher)sender;
|
|
||||||
DisposeRefresher(refresher);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops this instance.
|
|
||||||
/// </summary>
|
|
||||||
public void Stop()
|
|
||||||
{
|
|
||||||
LibraryManager.ItemAdded -= LibraryManager_ItemAdded;
|
|
||||||
LibraryManager.ItemRemoved -= LibraryManager_ItemRemoved;
|
|
||||||
|
|
||||||
foreach (var watcher in _fileSystemWatchers.Values.ToList())
|
|
||||||
{
|
|
||||||
watcher.Created -= watcher_Changed;
|
|
||||||
watcher.Deleted -= watcher_Changed;
|
|
||||||
watcher.Renamed -= watcher_Changed;
|
|
||||||
watcher.Changed -= watcher_Changed;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
watcher.EnableRaisingEvents = false;
|
|
||||||
}
|
|
||||||
catch (InvalidOperationException)
|
|
||||||
{
|
|
||||||
// Seeing this under mono on linux sometimes
|
|
||||||
// Collection was modified; enumeration operation may not execute.
|
|
||||||
}
|
|
||||||
|
|
||||||
watcher.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
_fileSystemWatchers.Clear();
|
|
||||||
DisposeRefreshers();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DisposeRefresher(FileRefresher refresher)
|
|
||||||
{
|
|
||||||
lock (_activeRefreshers)
|
|
||||||
{
|
|
||||||
refresher.Dispose();
|
|
||||||
_activeRefreshers.Remove(refresher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DisposeRefreshers()
|
|
||||||
{
|
|
||||||
lock (_activeRefreshers)
|
|
||||||
{
|
|
||||||
foreach (var refresher in _activeRefreshers.ToList())
|
|
||||||
{
|
|
||||||
refresher.Dispose();
|
|
||||||
}
|
|
||||||
_activeRefreshers.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
||||||
/// </summary>
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
Dispose(true);
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Releases unmanaged and - optionally - managed resources.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
||||||
protected virtual void Dispose(bool dispose)
|
|
||||||
{
|
|
||||||
if (dispose)
|
|
||||||
{
|
|
||||||
Stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class LibraryMonitorStartup : IServerEntryPoint
|
|
||||||
{
|
|
||||||
private readonly ILibraryMonitor _monitor;
|
|
||||||
|
|
||||||
public LibraryMonitorStartup(ILibraryMonitor monitor)
|
|
||||||
{
|
|
||||||
_monitor = monitor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Run()
|
|
||||||
{
|
|
||||||
_monitor.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,384 +0,0 @@
|
|||||||
using MediaBrowser.Common.Configuration;
|
|
||||||
using MediaBrowser.Common.Security;
|
|
||||||
using MediaBrowser.Controller.Entities;
|
|
||||||
using MediaBrowser.Controller.Entities.Movies;
|
|
||||||
using MediaBrowser.Controller.Entities.TV;
|
|
||||||
using MediaBrowser.Controller.Library;
|
|
||||||
using MediaBrowser.Controller.Localization;
|
|
||||||
using MediaBrowser.Model.Configuration;
|
|
||||||
using MediaBrowser.Model.Entities;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CommonIO;
|
|
||||||
using MoreLinq;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Server.Implementations.Intros
|
|
||||||
{
|
|
||||||
public class DefaultIntroProvider : IIntroProvider
|
|
||||||
{
|
|
||||||
private readonly ISecurityManager _security;
|
|
||||||
private readonly ILocalizationManager _localization;
|
|
||||||
private readonly IConfigurationManager _serverConfig;
|
|
||||||
private readonly ILibraryManager _libraryManager;
|
|
||||||
private readonly IFileSystem _fileSystem;
|
|
||||||
private readonly IMediaSourceManager _mediaSourceManager;
|
|
||||||
|
|
||||||
public DefaultIntroProvider(ISecurityManager security, ILocalizationManager localization, IConfigurationManager serverConfig, ILibraryManager libraryManager, IFileSystem fileSystem, IMediaSourceManager mediaSourceManager)
|
|
||||||
{
|
|
||||||
_security = security;
|
|
||||||
_localization = localization;
|
|
||||||
_serverConfig = serverConfig;
|
|
||||||
_libraryManager = libraryManager;
|
|
||||||
_fileSystem = fileSystem;
|
|
||||||
_mediaSourceManager = mediaSourceManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IEnumerable<IntroInfo>> GetIntros(BaseItem item, User user)
|
|
||||||
{
|
|
||||||
var config = GetOptions();
|
|
||||||
|
|
||||||
if (item is Movie)
|
|
||||||
{
|
|
||||||
if (!config.EnableIntrosForMovies)
|
|
||||||
{
|
|
||||||
return new List<IntroInfo>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (item is Episode)
|
|
||||||
{
|
|
||||||
if (!config.EnableIntrosForEpisodes)
|
|
||||||
{
|
|
||||||
return new List<IntroInfo>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return new List<IntroInfo>();
|
|
||||||
}
|
|
||||||
|
|
||||||
var ratingLevel = string.IsNullOrWhiteSpace(item.OfficialRating)
|
|
||||||
? null
|
|
||||||
: _localization.GetRatingLevel(item.OfficialRating);
|
|
||||||
|
|
||||||
var candidates = new List<ItemWithTrailer>();
|
|
||||||
|
|
||||||
var trailerTypes = new List<TrailerType>();
|
|
||||||
var sourceTypes = new List<SourceType>();
|
|
||||||
|
|
||||||
if (config.EnableIntrosFromMoviesInLibrary)
|
|
||||||
{
|
|
||||||
trailerTypes.Add(TrailerType.LocalTrailer);
|
|
||||||
sourceTypes.Add(SourceType.Library);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IsSupporter)
|
|
||||||
{
|
|
||||||
if (config.EnableIntrosFromUpcomingTrailers)
|
|
||||||
{
|
|
||||||
trailerTypes.Add(TrailerType.ComingSoonToTheaters);
|
|
||||||
sourceTypes.Clear();
|
|
||||||
}
|
|
||||||
if (config.EnableIntrosFromUpcomingDvdMovies)
|
|
||||||
{
|
|
||||||
trailerTypes.Add(TrailerType.ComingSoonToDvd);
|
|
||||||
sourceTypes.Clear();
|
|
||||||
}
|
|
||||||
if (config.EnableIntrosFromUpcomingStreamingMovies)
|
|
||||||
{
|
|
||||||
trailerTypes.Add(TrailerType.ComingSoonToStreaming);
|
|
||||||
sourceTypes.Clear();
|
|
||||||
}
|
|
||||||
if (config.EnableIntrosFromSimilarMovies)
|
|
||||||
{
|
|
||||||
trailerTypes.Add(TrailerType.Archive);
|
|
||||||
sourceTypes.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (trailerTypes.Count > 0)
|
|
||||||
{
|
|
||||||
var trailerResult = _libraryManager.GetItemList(new InternalItemsQuery
|
|
||||||
{
|
|
||||||
IncludeItemTypes = new[] { typeof(Trailer).Name },
|
|
||||||
TrailerTypes = trailerTypes.ToArray(),
|
|
||||||
SimilarTo = item,
|
|
||||||
IsPlayed = config.EnableIntrosForWatchedContent ? (bool?)null : false,
|
|
||||||
MaxParentalRating = config.EnableIntrosParentalControl ? ratingLevel : null,
|
|
||||||
BlockUnratedItems = config.EnableIntrosParentalControl ? new[] { UnratedItem.Trailer } : new UnratedItem[] { },
|
|
||||||
|
|
||||||
// Account for duplicates by imdb id, since the database doesn't support this yet
|
|
||||||
Limit = config.TrailerLimit * 2,
|
|
||||||
SourceTypes = sourceTypes.ToArray()
|
|
||||||
|
|
||||||
}).Where(i => string.IsNullOrWhiteSpace(i.GetProviderId(MetadataProviders.Imdb)) || !string.Equals(i.GetProviderId(MetadataProviders.Imdb), item.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase)).Take(config.TrailerLimit);
|
|
||||||
|
|
||||||
candidates.AddRange(trailerResult.Select(i => new ItemWithTrailer
|
|
||||||
{
|
|
||||||
Item = i,
|
|
||||||
Type = i.SourceType == SourceType.Channel ? ItemWithTrailerType.ChannelTrailer : ItemWithTrailerType.ItemWithTrailer,
|
|
||||||
LibraryManager = _libraryManager
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
return GetResult(item, candidates, config);
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<IntroInfo> GetResult(BaseItem item, IEnumerable<ItemWithTrailer> candidates, CinemaModeConfiguration config)
|
|
||||||
{
|
|
||||||
var customIntros = !string.IsNullOrWhiteSpace(config.CustomIntroPath) ?
|
|
||||||
GetCustomIntros(config) :
|
|
||||||
new List<IntroInfo>();
|
|
||||||
|
|
||||||
var mediaInfoIntros = !string.IsNullOrWhiteSpace(config.MediaInfoIntroPath) ?
|
|
||||||
GetMediaInfoIntros(config, item) :
|
|
||||||
new List<IntroInfo>();
|
|
||||||
|
|
||||||
// Avoid implicitly captured closure
|
|
||||||
return candidates.Select(i => i.IntroInfo)
|
|
||||||
.Concat(customIntros.Take(1))
|
|
||||||
.Concat(mediaInfoIntros);
|
|
||||||
}
|
|
||||||
|
|
||||||
private CinemaModeConfiguration GetOptions()
|
|
||||||
{
|
|
||||||
return _serverConfig.GetConfiguration<CinemaModeConfiguration>("cinemamode");
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<IntroInfo> GetCustomIntros(CinemaModeConfiguration options)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return GetCustomIntroFiles(options, true, false)
|
|
||||||
.OrderBy(i => Guid.NewGuid())
|
|
||||||
.Select(i => new IntroInfo
|
|
||||||
{
|
|
||||||
Path = i
|
|
||||||
|
|
||||||
}).ToList();
|
|
||||||
}
|
|
||||||
catch (IOException)
|
|
||||||
{
|
|
||||||
return new List<IntroInfo>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<IntroInfo> GetMediaInfoIntros(CinemaModeConfiguration options, BaseItem item)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var hasMediaSources = item as IHasMediaSources;
|
|
||||||
|
|
||||||
if (hasMediaSources == null)
|
|
||||||
{
|
|
||||||
return new List<IntroInfo>();
|
|
||||||
}
|
|
||||||
|
|
||||||
var mediaSource = _mediaSourceManager.GetStaticMediaSources(hasMediaSources, false)
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
if (mediaSource == null)
|
|
||||||
{
|
|
||||||
return new List<IntroInfo>();
|
|
||||||
}
|
|
||||||
|
|
||||||
var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
|
|
||||||
var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
|
|
||||||
|
|
||||||
var allIntros = GetCustomIntroFiles(options, false, true)
|
|
||||||
.OrderBy(i => Guid.NewGuid())
|
|
||||||
.Select(i => new IntroInfo
|
|
||||||
{
|
|
||||||
Path = i
|
|
||||||
|
|
||||||
}).ToList();
|
|
||||||
|
|
||||||
var returnResult = new List<IntroInfo>();
|
|
||||||
|
|
||||||
if (videoStream != null)
|
|
||||||
{
|
|
||||||
returnResult.AddRange(GetMediaInfoIntrosByVideoStream(allIntros, videoStream).Take(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (audioStream != null)
|
|
||||||
{
|
|
||||||
returnResult.AddRange(GetMediaInfoIntrosByAudioStream(allIntros, audioStream).Take(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
returnResult.AddRange(GetMediaInfoIntrosByTags(allIntros, item.Tags).Take(1));
|
|
||||||
|
|
||||||
return returnResult.DistinctBy(i => i.Path, StringComparer.OrdinalIgnoreCase);
|
|
||||||
}
|
|
||||||
catch (IOException)
|
|
||||||
{
|
|
||||||
return new List<IntroInfo>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<IntroInfo> GetMediaInfoIntrosByVideoStream(List<IntroInfo> allIntros, MediaStream stream)
|
|
||||||
{
|
|
||||||
var codec = stream.Codec;
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(codec))
|
|
||||||
{
|
|
||||||
return new List<IntroInfo>();
|
|
||||||
}
|
|
||||||
|
|
||||||
return allIntros
|
|
||||||
.Where(i => IsMatch(i.Path, codec))
|
|
||||||
.OrderBy(i => Guid.NewGuid());
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<IntroInfo> GetMediaInfoIntrosByAudioStream(List<IntroInfo> allIntros, MediaStream stream)
|
|
||||||
{
|
|
||||||
var codec = stream.Codec;
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(codec))
|
|
||||||
{
|
|
||||||
return new List<IntroInfo>();
|
|
||||||
}
|
|
||||||
|
|
||||||
return allIntros
|
|
||||||
.Where(i => IsAudioMatch(i.Path, stream))
|
|
||||||
.OrderBy(i => Guid.NewGuid());
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<IntroInfo> GetMediaInfoIntrosByTags(List<IntroInfo> allIntros, List<string> tags)
|
|
||||||
{
|
|
||||||
return allIntros
|
|
||||||
.Where(i => tags.Any(t => IsMatch(i.Path, t)))
|
|
||||||
.OrderBy(i => Guid.NewGuid());
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsMatch(string file, string attribute)
|
|
||||||
{
|
|
||||||
var filename = Path.GetFileNameWithoutExtension(file) ?? string.Empty;
|
|
||||||
filename = Normalize(filename);
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(filename))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
attribute = Normalize(attribute);
|
|
||||||
if (string.IsNullOrWhiteSpace(attribute))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return string.Equals(filename, attribute, StringComparison.OrdinalIgnoreCase);
|
|
||||||
}
|
|
||||||
|
|
||||||
private string Normalize(string value)
|
|
||||||
{
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsAudioMatch(string path, MediaStream stream)
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrWhiteSpace(stream.Codec))
|
|
||||||
{
|
|
||||||
if (IsMatch(path, stream.Codec))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!string.IsNullOrWhiteSpace(stream.Profile))
|
|
||||||
{
|
|
||||||
if (IsMatch(path, stream.Profile))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<string> GetCustomIntroFiles(CinemaModeConfiguration options, bool enableCustomIntros, bool enableMediaInfoIntros)
|
|
||||||
{
|
|
||||||
var list = new List<string>();
|
|
||||||
|
|
||||||
if (enableCustomIntros && !string.IsNullOrWhiteSpace(options.CustomIntroPath))
|
|
||||||
{
|
|
||||||
list.AddRange(_fileSystem.GetFilePaths(options.CustomIntroPath, true)
|
|
||||||
.Where(_libraryManager.IsVideoFile));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (enableMediaInfoIntros && !string.IsNullOrWhiteSpace(options.MediaInfoIntroPath))
|
|
||||||
{
|
|
||||||
list.AddRange(_fileSystem.GetFilePaths(options.MediaInfoIntroPath, true)
|
|
||||||
.Where(_libraryManager.IsVideoFile));
|
|
||||||
}
|
|
||||||
|
|
||||||
return list.Distinct(StringComparer.OrdinalIgnoreCase);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<string> GetAllIntroFiles()
|
|
||||||
{
|
|
||||||
return GetCustomIntroFiles(GetOptions(), true, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsSupporter
|
|
||||||
{
|
|
||||||
get { return _security.IsMBSupporter; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Name
|
|
||||||
{
|
|
||||||
get { return "Default"; }
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class ItemWithTrailer
|
|
||||||
{
|
|
||||||
internal BaseItem Item;
|
|
||||||
internal ItemWithTrailerType Type;
|
|
||||||
internal ILibraryManager LibraryManager;
|
|
||||||
|
|
||||||
public IntroInfo IntroInfo
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var id = Item.Id;
|
|
||||||
|
|
||||||
if (Type == ItemWithTrailerType.ItemWithTrailer)
|
|
||||||
{
|
|
||||||
var hasTrailers = Item as IHasTrailers;
|
|
||||||
|
|
||||||
if (hasTrailers != null)
|
|
||||||
{
|
|
||||||
id = hasTrailers.LocalTrailerIds.FirstOrDefault();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new IntroInfo
|
|
||||||
{
|
|
||||||
ItemId = id
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal enum ItemWithTrailerType
|
|
||||||
{
|
|
||||||
ChannelTrailer,
|
|
||||||
ItemWithTrailer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class CinemaModeConfigurationFactory : IConfigurationFactory
|
|
||||||
{
|
|
||||||
public IEnumerable<ConfigurationStore> GetConfigurations()
|
|
||||||
{
|
|
||||||
return new[]
|
|
||||||
{
|
|
||||||
new ConfigurationStore
|
|
||||||
{
|
|
||||||
ConfigurationType = typeof(CinemaModeConfiguration),
|
|
||||||
Key = "cinemamode"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user