diff --git a/MediaBrowser.Api/Library/LibraryStructureService.cs b/MediaBrowser.Api/Library/LibraryStructureService.cs
index 91018168b2..ac2a109244 100644
--- a/MediaBrowser.Api/Library/LibraryStructureService.cs
+++ b/MediaBrowser.Api/Library/LibraryStructureService.cs
@@ -1,11 +1,12 @@
-using System.Threading;
-using MediaBrowser.Controller;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using ServiceStack.ServiceHost;
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Threading;
namespace MediaBrowser.Api.Library
{
@@ -152,6 +153,8 @@ namespace MediaBrowser.Api.Library
///
private readonly ILibraryManager _libraryManager;
+ private readonly IDirectoryWatchers _directoryWatchers;
+
///
/// Initializes a new instance of the class.
///
@@ -159,7 +162,7 @@ namespace MediaBrowser.Api.Library
/// The user manager.
/// The library manager.
/// appPaths
- public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager)
+ public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IDirectoryWatchers directoryWatchers)
{
if (appPaths == null)
{
@@ -169,6 +172,7 @@ namespace MediaBrowser.Api.Library
_userManager = userManager;
_appPaths = appPaths;
_libraryManager = libraryManager;
+ _directoryWatchers = directoryWatchers;
}
///
@@ -200,15 +204,24 @@ namespace MediaBrowser.Api.Library
/// The request.
public void Post(AddVirtualFolder request)
{
- if (string.IsNullOrEmpty(request.UserId))
- {
- LibraryHelpers.AddVirtualFolder(request.Name, request.CollectionType, null, _appPaths);
- }
- else
- {
- var user = _userManager.GetUserById(new Guid(request.UserId));
+ _directoryWatchers.Stop();
- LibraryHelpers.AddVirtualFolder(request.Name, request.CollectionType, user, _appPaths);
+ try
+ {
+ if (string.IsNullOrEmpty(request.UserId))
+ {
+ LibraryHelpers.AddVirtualFolder(request.Name, request.CollectionType, null, _appPaths);
+ }
+ else
+ {
+ var user = _userManager.GetUserById(new Guid(request.UserId));
+
+ LibraryHelpers.AddVirtualFolder(request.Name, request.CollectionType, user, _appPaths);
+ }
+ }
+ finally
+ {
+ _directoryWatchers.Start();
}
_libraryManager.ValidateMediaLibrary(new Progress(), CancellationToken.None);
@@ -220,15 +233,24 @@ namespace MediaBrowser.Api.Library
/// The request.
public void Post(RenameVirtualFolder request)
{
- if (string.IsNullOrEmpty(request.UserId))
- {
- LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, null, _appPaths);
- }
- else
- {
- var user = _userManager.GetUserById(new Guid(request.UserId));
+ _directoryWatchers.Stop();
- LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, user, _appPaths);
+ try
+ {
+ if (string.IsNullOrEmpty(request.UserId))
+ {
+ LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, null, _appPaths);
+ }
+ else
+ {
+ var user = _userManager.GetUserById(new Guid(request.UserId));
+
+ LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, user, _appPaths);
+ }
+ }
+ finally
+ {
+ _directoryWatchers.Start();
}
_libraryManager.ValidateMediaLibrary(new Progress(), CancellationToken.None);
@@ -240,15 +262,24 @@ namespace MediaBrowser.Api.Library
/// The request.
public void Delete(RemoveVirtualFolder request)
{
- if (string.IsNullOrEmpty(request.UserId))
- {
- LibraryHelpers.RemoveVirtualFolder(request.Name, null, _appPaths);
- }
- else
- {
- var user = _userManager.GetUserById(new Guid(request.UserId));
+ _directoryWatchers.Stop();
- LibraryHelpers.RemoveVirtualFolder(request.Name, user, _appPaths);
+ try
+ {
+ if (string.IsNullOrEmpty(request.UserId))
+ {
+ LibraryHelpers.RemoveVirtualFolder(request.Name, null, _appPaths);
+ }
+ else
+ {
+ var user = _userManager.GetUserById(new Guid(request.UserId));
+
+ LibraryHelpers.RemoveVirtualFolder(request.Name, user, _appPaths);
+ }
+ }
+ finally
+ {
+ _directoryWatchers.Start();
}
_libraryManager.ValidateMediaLibrary(new Progress(), CancellationToken.None);
@@ -260,15 +291,24 @@ namespace MediaBrowser.Api.Library
/// The request.
public void Post(AddMediaPath request)
{
- if (string.IsNullOrEmpty(request.UserId))
- {
- LibraryHelpers.AddMediaPath(request.Name, request.Path, null, _appPaths);
- }
- else
- {
- var user = _userManager.GetUserById(new Guid(request.UserId));
+ _directoryWatchers.Stop();
- LibraryHelpers.AddMediaPath(request.Name, request.Path, user, _appPaths);
+ try
+ {
+ if (string.IsNullOrEmpty(request.UserId))
+ {
+ LibraryHelpers.AddMediaPath(request.Name, request.Path, null, _appPaths);
+ }
+ else
+ {
+ var user = _userManager.GetUserById(new Guid(request.UserId));
+
+ LibraryHelpers.AddMediaPath(request.Name, request.Path, user, _appPaths);
+ }
+ }
+ finally
+ {
+ _directoryWatchers.Start();
}
_libraryManager.ValidateMediaLibrary(new Progress(), CancellationToken.None);
@@ -280,15 +320,24 @@ namespace MediaBrowser.Api.Library
/// The request.
public void Delete(RemoveMediaPath request)
{
- if (string.IsNullOrEmpty(request.UserId))
- {
- LibraryHelpers.RemoveMediaPath(request.Name, request.Path, null, _appPaths);
- }
- else
- {
- var user = _userManager.GetUserById(new Guid(request.UserId));
+ _directoryWatchers.Stop();
- LibraryHelpers.RemoveMediaPath(request.Name, request.Path, user, _appPaths);
+ try
+ {
+ if (string.IsNullOrEmpty(request.UserId))
+ {
+ LibraryHelpers.RemoveMediaPath(request.Name, request.Path, null, _appPaths);
+ }
+ else
+ {
+ var user = _userManager.GetUserById(new Guid(request.UserId));
+
+ LibraryHelpers.RemoveMediaPath(request.Name, request.Path, user, _appPaths);
+ }
+ }
+ finally
+ {
+ _directoryWatchers.Start();
}
_libraryManager.ValidateMediaLibrary(new Progress(), CancellationToken.None);
diff --git a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs
index 902bcaae3f..ff103b320b 100644
--- a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs
+++ b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs
@@ -576,6 +576,7 @@ namespace MediaBrowser.Server.Implementations.IO
}
}
+ _fileSystemWatchers.Clear();
_affectedPaths.Clear();
}