From b43444c1dfe604f0c386f36627f6b61beb25eea5 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 24 May 2013 15:52:41 -0400 Subject: [PATCH] additional fixes for #305 --- MediaBrowser.Api/Library/LibraryHelpers.cs | 21 +++++++++++++++++-- .../EntryPoints/LibraryChangedNotifier.cs | 11 +++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.Api/Library/LibraryHelpers.cs b/MediaBrowser.Api/Library/LibraryHelpers.cs index d3b7861a67..8abdc4df5a 100644 --- a/MediaBrowser.Api/Library/LibraryHelpers.cs +++ b/MediaBrowser.Api/Library/LibraryHelpers.cs @@ -175,13 +175,22 @@ namespace MediaBrowser.Api.Library { var duplicate = Directory.EnumerateFiles(appPaths.RootFolderPath, "*.lnk", SearchOption.AllDirectories) .Select(FileSystem.ResolveShortcut) - .FirstOrDefault(p => !IsNewPathValid(mediaPath, p)); + .FirstOrDefault(p => !IsNewPathValid(mediaPath, p, false)); if (!string.IsNullOrEmpty(duplicate)) { throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate)); } + duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, "*.lnk", SearchOption.AllDirectories) + .Select(FileSystem.ResolveShortcut) + .FirstOrDefault(p => !IsNewPathValid(mediaPath, p, true)); + + if (!string.IsNullOrEmpty(duplicate)) + { + throw new ArgumentException(string.Format("The path cannot be added to the library because {0} already exists.", duplicate)); + } + // Make sure the current root folder doesn't already have a shortcut to the same path duplicate = Directory.EnumerateFiles(currentViewRootFolderPath, "*.lnk", SearchOption.AllDirectories) .Select(FileSystem.ResolveShortcut) @@ -198,11 +207,13 @@ namespace MediaBrowser.Api.Library /// /// The new path. /// The existing path. + /// if set to true [enforce sub path restriction]. /// true if [is new path valid] [the specified new path]; otherwise, false. - private static bool IsNewPathValid(string newPath, string existingPath) + private static bool IsNewPathValid(string newPath, string existingPath, bool enforceSubPathRestriction) { // Example: D:\Movies is the existing path // D:\ cannot be added + // Neither can D:\Movies\Kids // A D:\Movies duplicate is ok here since that will be caught later if (newPath.Equals(existingPath, StringComparison.OrdinalIgnoreCase)) @@ -210,6 +221,12 @@ namespace MediaBrowser.Api.Library return true; } + // Validate the D:\Movies\Kids scenario + if (enforceSubPathRestriction && newPath.StartsWith(existingPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + // Validate the D:\ scenario if (existingPath.StartsWith(newPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) { diff --git a/MediaBrowser.ServerApplication/EntryPoints/LibraryChangedNotifier.cs b/MediaBrowser.ServerApplication/EntryPoints/LibraryChangedNotifier.cs index 715492aac4..104b09fda1 100644 --- a/MediaBrowser.ServerApplication/EntryPoints/LibraryChangedNotifier.cs +++ b/MediaBrowser.ServerApplication/EntryPoints/LibraryChangedNotifier.cs @@ -4,6 +4,7 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Session; using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; using MoreLinq; using System; using System.Collections.Generic; @@ -22,6 +23,7 @@ namespace MediaBrowser.ServerApplication.EntryPoints private readonly ISessionManager _sessionManager; private readonly IServerManager _serverManager; private readonly IUserManager _userManager; + private readonly ILogger _logger; /// /// The _library changed sync lock @@ -195,7 +197,14 @@ namespace MediaBrowser.ServerApplication.EntryPoints var id = userId; var webSockets = currentSessions.Where(u => u.UserId.HasValue && u.UserId.Value == id).SelectMany(i => i.WebSockets).ToList(); - await _serverManager.SendWebSocketMessageAsync("LibraryChanged", () => GetLibraryUpdateInfo(itemsAdded, itemsUpdated, itemsRemoved, foldersAddedTo, foldersRemovedFrom, id), webSockets, cancellationToken).ConfigureAwait(false); + try + { + await _serverManager.SendWebSocketMessageAsync("LibraryChanged", () => GetLibraryUpdateInfo(itemsAdded, itemsUpdated, itemsRemoved, foldersAddedTo, foldersRemovedFrom, id), webSockets, cancellationToken).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.ErrorException("Error sending LibraryChanged message", ex); + } } }