diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/Library/LibraryManager.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Library/LibraryManager.cs | 529 |
1 files changed, 407 insertions, 122 deletions
diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 28671fb7c..712ea4ef3 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -33,6 +33,9 @@ using System.Net; using System.Threading; using System.Threading.Tasks; using CommonIO; +using MediaBrowser.Controller.Channels; +using MediaBrowser.Model.Channels; +using MediaBrowser.Model.Dto; using MediaBrowser.Model.Extensions; using MediaBrowser.Model.Library; using MediaBrowser.Model.Net; @@ -143,6 +146,7 @@ namespace MediaBrowser.Server.Implementations.Library private readonly Func<ILibraryMonitor> _libraryMonitorFactory; private readonly Func<IProviderManager> _providerManagerFactory; private readonly Func<IUserViewManager> _userviewManager; + public bool IsScanRunning { get; private set; } /// <summary> /// The _library items cache @@ -305,9 +309,14 @@ namespace MediaBrowser.Server.Implementations.Library /// <returns>Task.</returns> private async Task UpdateSeasonZeroNames(string newName, CancellationToken cancellationToken) { - var seasons = RootFolder.GetRecursiveChildren(i => i is Season) - .Cast<Season>() - .Where(i => i.IndexNumber.HasValue && i.IndexNumber.Value == 0 && !string.Equals(i.Name, newName, StringComparison.Ordinal)) + var seasons = GetItemList(new InternalItemsQuery + { + IncludeItemTypes = new[] { typeof(Season).Name }, + Recursive = true, + IndexNumber = 0 + + }).Cast<Season>() + .Where(i => !string.Equals(i.Name, newName, StringComparison.Ordinal)) .ToList(); foreach (var season in seasons) @@ -345,10 +354,6 @@ namespace MediaBrowser.Server.Implementations.Library private void RegisterItem(Guid id, BaseItem item) { - if (item.SourceType != SourceType.Library) - { - return; - } if (item is IItemByName) { if (!(item is MusicArtist)) @@ -356,10 +361,25 @@ namespace MediaBrowser.Server.Implementations.Library return; } } - //if (!(item is Folder)) - //{ - // return; - //} + + if (item.IsFolder) + { + if (!(item is ICollectionFolder) && !(item is UserView) && !(item is Channel)) + { + if (item.SourceType != SourceType.Library) + { + return; + } + } + } + else + { + if (item is Photo) + { + return; + } + } + LibraryItemsCache.AddOrUpdate(id, item, delegate { return item; }); } @@ -514,38 +534,6 @@ namespace MediaBrowser.Server.Implementations.Library return key.GetMD5(); } - public IEnumerable<BaseItem> ReplaceVideosWithPrimaryVersions(IEnumerable<BaseItem> items) - { - if (items == null) - { - throw new ArgumentNullException("items"); - } - - var dict = new Dictionary<Guid, BaseItem>(); - - foreach (var item in items) - { - var video = item as Video; - - if (video != null) - { - if (video.PrimaryVersionId.HasValue) - { - var primary = GetItemById(video.PrimaryVersionId.Value) as Video; - - if (primary != null) - { - dict[primary.Id] = primary; - continue; - } - } - } - dict[item.Id] = item; - } - - return dict.Values; - } - /// <summary> /// Ensure supplied item has only one instance throughout /// </summary> @@ -800,27 +788,22 @@ namespace MediaBrowser.Server.Implementations.Library return _userRootFolder; } - public BaseItem FindByPath(string path) + public BaseItem FindByPath(string path, bool? isFolder) { + // If this returns multiple items it could be tricky figuring out which one is correct. + // In most cases, the newest one will be and the others obsolete but not yet cleaned up + var query = new InternalItemsQuery { - Path = path + Path = path, + IsFolder = isFolder, + SortBy = new[] { ItemSortBy.DateCreated }, + SortOrder = SortOrder.Descending, + Limit = 1 }; - // Only use the database result if there's exactly one item, otherwise we run the risk of returning old data that hasn't been cleaned yet. - var items = GetItemIds(query).Select(GetItemById).Where(i => i != null).ToArray(); - - if (items.Length == 1) - { - return items[0]; - } - - if (items.Length == 0) - { - return null; - } - - return RootFolder.FindByPath(path); + return GetItemList(query) + .FirstOrDefault(); } /// <summary> @@ -929,7 +912,10 @@ namespace MediaBrowser.Server.Implementations.Library throw new ArgumentNullException("name"); } - var validFilename = _fileSystem.GetValidFilename(name).Trim(); + // Trim the period at the end because windows will have a hard time with that + var validFilename = _fileSystem.GetValidFilename(name) + .Trim() + .TrimEnd('.'); string subFolderPrefix = null; @@ -951,31 +937,23 @@ namespace MediaBrowser.Server.Implementations.Library Path.Combine(path, validFilename) : Path.Combine(path, subFolderPrefix, validFilename); - var id = GetNewItemId(fullPath, type); - - BaseItem obj; - - if (!_libraryItemsCache.TryGetValue(id, out obj)) - { - obj = CreateItemByName<T>(fullPath, name, id); - - RegisterItem(id, obj); - } - - return obj as T; + return CreateItemByName<T>(fullPath, name); } - private T CreateItemByName<T>(string path, string name, Guid id) + private T CreateItemByName<T>(string path, string name) where T : BaseItem, new() { - var isArtist = typeof(T) == typeof(MusicArtist); - - if (isArtist) + if (typeof(T) == typeof(MusicArtist)) { - var existing = RootFolder - .GetRecursiveChildren(i => i is T && NameExtensions.AreEqual(i.Name, name)) - .Cast<T>() - .FirstOrDefault(); + var existing = GetItemList(new InternalItemsQuery + { + IncludeItemTypes = new[] { typeof(T).Name }, + Name = name + + }).Cast<MusicArtist>() + .OrderBy(i => i.IsAccessedByName ? 1 : 0) + .Cast<T>() + .FirstOrDefault(); if (existing != null) { @@ -983,6 +961,8 @@ namespace MediaBrowser.Server.Implementations.Library } } + var id = GetNewItemId(path, typeof(T)); + var item = GetItemById(id) as T; if (item == null) @@ -996,11 +976,6 @@ namespace MediaBrowser.Server.Implementations.Library Path = path }; - if (isArtist) - { - (item as MusicArtist).IsAccessedByName = true; - } - var task = CreateItem(item, CancellationToken.None); Task.WaitAll(task); } @@ -1102,6 +1077,7 @@ namespace MediaBrowser.Server.Implementations.Library /// <returns>Task.</returns> public async Task ValidateMediaLibraryInternal(IProgress<double> progress, CancellationToken cancellationToken) { + IsScanRunning = true; _libraryMonitorFactory().Stop(); try @@ -1111,6 +1087,7 @@ namespace MediaBrowser.Server.Implementations.Library finally { _libraryMonitorFactory().Start(); + IsScanRunning = false; } } @@ -1289,6 +1266,8 @@ namespace MediaBrowser.Server.Implementations.Library item = RetrieveItem(id); + //_logger.Debug("GetitemById {0}", id); + if (item != null) { RegisterItem(item); @@ -1297,59 +1276,162 @@ namespace MediaBrowser.Server.Implementations.Library return item; } - public BaseItem GetMemoryItemById(Guid id) + public IEnumerable<BaseItem> GetItemList(InternalItemsQuery query) { - if (id == Guid.Empty) + if (query.Recursive && query.ParentId.HasValue) { - throw new ArgumentNullException("id"); + var parent = GetItemById(query.ParentId.Value); + if (parent != null) + { + SetTopParentIdsOrAncestors(query, new List<BaseItem> { parent }); + query.ParentId = null; + } } - BaseItem item; + if (query.User != null) + { + AddUserToQuery(query, query.User); + } - LibraryItemsCache.TryGetValue(id, out item); + return ItemRepository.GetItemList(query); + } - return item; + public IEnumerable<BaseItem> GetItemList(InternalItemsQuery query, IEnumerable<string> parentIds) + { + var parents = parentIds.Select(i => GetItemById(new Guid(i))).Where(i => i != null).ToList(); + + SetTopParentIdsOrAncestors(query, parents); + + if (query.AncestorIds.Length == 0 && query.TopParentIds.Length == 0) + { + if (query.User != null) + { + AddUserToQuery(query, query.User); + } + } + + return ItemRepository.GetItemList(query); } - public IEnumerable<BaseItem> GetItemList(InternalItemsQuery query) + public QueryResult<BaseItem> QueryItems(InternalItemsQuery query) { if (query.User != null) { AddUserToQuery(query, query.User); } - var result = ItemRepository.GetItemIdsList(query); + if (query.EnableTotalRecordCount) + { + return ItemRepository.GetItems(query); + } - return result.Select(GetItemById).Where(i => i != null); + return new QueryResult<BaseItem> + { + Items = ItemRepository.GetItemList(query).ToArray() + }; } - public QueryResult<BaseItem> QueryItems(InternalItemsQuery query) + public List<Guid> GetItemIds(InternalItemsQuery query) { if (query.User != null) { AddUserToQuery(query, query.User); } - return ItemRepository.GetItems(query); + return ItemRepository.GetItemIdsList(query); } - public List<Guid> GetItemIds(InternalItemsQuery query) + public QueryResult<Tuple<BaseItem, ItemCounts>> GetStudios(InternalItemsQuery query) { if (query.User != null) { AddUserToQuery(query, query.User); } - return ItemRepository.GetItemIdsList(query); + SetTopParentOrAncestorIds(query); + return ItemRepository.GetStudios(query); } - public IEnumerable<BaseItem> GetItemList(InternalItemsQuery query, IEnumerable<string> parentIds) + public QueryResult<Tuple<BaseItem, ItemCounts>> GetGenres(InternalItemsQuery query) { - var parents = parentIds.Select(i => GetItemById(new Guid(i))).Where(i => i != null).ToList(); + if (query.User != null) + { + AddUserToQuery(query, query.User); + } - SetTopParentIdsOrAncestors(query, parents); + SetTopParentOrAncestorIds(query); + return ItemRepository.GetGenres(query); + } - return GetItemIds(query).Select(GetItemById).Where(i => i != null); + public QueryResult<Tuple<BaseItem, ItemCounts>> GetGameGenres(InternalItemsQuery query) + { + if (query.User != null) + { + AddUserToQuery(query, query.User); + } + + SetTopParentOrAncestorIds(query); + return ItemRepository.GetGameGenres(query); + } + + public QueryResult<Tuple<BaseItem, ItemCounts>> GetMusicGenres(InternalItemsQuery query) + { + if (query.User != null) + { + AddUserToQuery(query, query.User); + } + + SetTopParentOrAncestorIds(query); + return ItemRepository.GetMusicGenres(query); + } + + public QueryResult<Tuple<BaseItem, ItemCounts>> GetArtists(InternalItemsQuery query) + { + if (query.User != null) + { + AddUserToQuery(query, query.User); + } + + SetTopParentOrAncestorIds(query); + return ItemRepository.GetArtists(query); + } + + private void SetTopParentOrAncestorIds(InternalItemsQuery query) + { + if (query.AncestorIds.Length == 0) + { + return; + } + + var parents = query.AncestorIds.Select(i => GetItemById(new Guid(i))).ToList(); + + if (parents.All(i => + { + if (i is ICollectionFolder || i is UserView) + { + return true; + } + + //_logger.Debug("Query requires ancestor query due to type: " + i.GetType().Name); + return false; + + })) + { + // Optimize by querying against top level views + query.TopParentIds = parents.SelectMany(i => GetTopParentsForQuery(i, query.User)).Select(i => i.Id.ToString("N")).ToArray(); + query.AncestorIds = new string[] { }; + } + } + + public QueryResult<Tuple<BaseItem, ItemCounts>> GetAlbumArtists(InternalItemsQuery query) + { + if (query.User != null) + { + AddUserToQuery(query, query.User); + } + + SetTopParentOrAncestorIds(query); + return ItemRepository.GetAlbumArtists(query); } public QueryResult<BaseItem> GetItemsResult(InternalItemsQuery query) @@ -1369,24 +1451,17 @@ namespace MediaBrowser.Server.Implementations.Library AddUserToQuery(query, query.User); } - var initialResult = ItemRepository.GetItemIds(query); + if (query.EnableTotalRecordCount) + { + return ItemRepository.GetItems(query); + } return new QueryResult<BaseItem> { - TotalRecordCount = initialResult.TotalRecordCount, - Items = initialResult.Items.Select(GetItemById).Where(i => i != null).ToArray() + Items = ItemRepository.GetItemList(query).ToArray() }; } - public QueryResult<BaseItem> GetItemsResult(InternalItemsQuery query, IEnumerable<string> parentIds) - { - var parents = parentIds.Select(i => GetItemById(new Guid(i))).Where(i => i != null).ToList(); - - SetTopParentIdsOrAncestors(query, parents); - - return GetItemsResult(query); - } - private void SetTopParentIdsOrAncestors(InternalItemsQuery query, List<BaseItem> parents) { if (parents.All(i => @@ -1396,7 +1471,7 @@ namespace MediaBrowser.Server.Implementations.Library return true; } - _logger.Debug("Query requires ancestor query due to type: " + i.GetType().Name); + //_logger.Debug("Query requires ancestor query due to type: " + i.GetType().Name); return false; })) @@ -1413,7 +1488,7 @@ namespace MediaBrowser.Server.Implementations.Library private void AddUserToQuery(InternalItemsQuery query, User user) { - if (query.AncestorIds.Length == 0 && !query.ParentId.HasValue && query.ChannelIds.Length == 0 && query.TopParentIds.Length == 0) + if (query.AncestorIds.Length == 0 && !query.ParentId.HasValue && query.ChannelIds.Length == 0 && query.TopParentIds.Length == 0 && string.IsNullOrWhiteSpace(query.AncestorWithPresentationUniqueKey)) { var userViews = _userviewManager().GetUserViews(new UserViewQuery { @@ -1438,8 +1513,13 @@ namespace MediaBrowser.Server.Implementations.Library } if (string.Equals(view.ViewType, CollectionType.Channels)) { - // TODO: Return channels - return new[] { view }; + var channelResult = BaseItem.ChannelManager.GetChannelsInternal(new ChannelQuery + { + UserId = user.Id.ToString("N") + + }, CancellationToken.None).Result; + + return channelResult.Items; } // Translate view into folders @@ -1465,8 +1545,12 @@ namespace MediaBrowser.Server.Implementations.Library // Handle grouping if (user != null && !string.IsNullOrWhiteSpace(view.ViewType) && UserView.IsEligibleForGrouping(view.ViewType)) { - var collectionFolders = user.RootFolder.GetChildren(user, true).OfType<CollectionFolder>().Where(i => string.IsNullOrWhiteSpace(i.CollectionType) || string.Equals(i.CollectionType, view.ViewType, StringComparison.OrdinalIgnoreCase)); - return collectionFolders.SelectMany(i => GetTopParentsForQuery(i, user)); + return user.RootFolder + .GetChildren(user, true) + .OfType<CollectionFolder>() + .Where(i => string.IsNullOrWhiteSpace(i.CollectionType) || string.Equals(i.CollectionType, view.ViewType, StringComparison.OrdinalIgnoreCase)) + .Where(i => user.IsFolderGrouped(i.Id)) + .SelectMany(i => GetTopParentsForQuery(i, user)); } return new BaseItem[] { }; } @@ -1847,7 +1931,7 @@ namespace MediaBrowser.Server.Implementations.Library private string GetContentTypeOverride(string path, bool inherit) { - var nameValuePair = ConfigurationManager.Configuration.ContentTypes.FirstOrDefault(i => string.Equals(i.Name, path, StringComparison.OrdinalIgnoreCase) || (inherit && _fileSystem.ContainsSubPath(i.Name, path))); + var nameValuePair = ConfigurationManager.Configuration.ContentTypes.FirstOrDefault(i => string.Equals(i.Name, path, StringComparison.OrdinalIgnoreCase) || (inherit && !string.IsNullOrWhiteSpace(i.Name) && _fileSystem.ContainsSubPath(i.Name, path))); if (nameValuePair != null) { return nameValuePair.Value; @@ -2322,7 +2406,7 @@ namespace MediaBrowser.Server.Implementations.Library public IEnumerable<Video> FindTrailers(BaseItem owner, List<FileSystemMetadata> fileSystemChildren, IDirectoryService directoryService) { - var files = fileSystemChildren.Where(i => i.IsDirectory) + var files = owner.IsInMixedFolder ? new List<FileSystemMetadata>() : fileSystemChildren.Where(i => i.IsDirectory) .Where(i => string.Equals(i.Name, BaseItem.TrailerFolderName, StringComparison.OrdinalIgnoreCase)) .SelectMany(i => _fileSystem.GetFiles(i.FullName, false)) .ToList(); @@ -2361,6 +2445,7 @@ namespace MediaBrowser.Server.Implementations.Library } video.ExtraType = ExtraType.Trailer; + video.TrailerTypes = new List<TrailerType> { TrailerType.LocalTrailer }; return video; @@ -2575,5 +2660,205 @@ namespace MediaBrowser.Server.Implementations.Library throw new InvalidOperationException(); } + + public void AddVirtualFolder(string name, string collectionType, string[] mediaPaths, bool refreshLibrary) + { + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentNullException("name"); + } + + name = _fileSystem.GetValidFilename(name); + + var rootFolderPath = ConfigurationManager.ApplicationPaths.DefaultUserViewsPath; + + var virtualFolderPath = Path.Combine(rootFolderPath, name); + while (_fileSystem.DirectoryExists(virtualFolderPath)) + { + name += "1"; + virtualFolderPath = Path.Combine(rootFolderPath, name); + } + + if (mediaPaths != null) + { + var invalidpath = mediaPaths.FirstOrDefault(i => !_fileSystem.DirectoryExists(i)); + if (invalidpath != null) + { + throw new ArgumentException("The specified path does not exist: " + invalidpath + "."); + } + } + + _libraryMonitorFactory().Stop(); + + try + { + _fileSystem.CreateDirectory(virtualFolderPath); + + if (!string.IsNullOrEmpty(collectionType)) + { + var path = Path.Combine(virtualFolderPath, collectionType + ".collection"); + + using (File.Create(path)) + { + + } + } + + if (mediaPaths != null) + { + foreach (var path in mediaPaths) + { + AddMediaPath(name, path); + } + } + } + finally + { + Task.Run(() => + { + // No need to start if scanning the library because it will handle it + if (refreshLibrary) + { + ValidateMediaLibrary(new Progress<double>(), CancellationToken.None); + } + else + { + // Need to add a delay here or directory watchers may still pick up the changes + var task = Task.Delay(1000); + // Have to block here to allow exceptions to bubble + Task.WaitAll(task); + + _libraryMonitorFactory().Start(); + } + }); + } + } + + public void RemoveVirtualFolder(string name, bool refreshLibrary) + { + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentNullException("name"); + } + + var rootFolderPath = ConfigurationManager.ApplicationPaths.DefaultUserViewsPath; + + var path = Path.Combine(rootFolderPath, name); + + if (!_fileSystem.DirectoryExists(path)) + { + throw new DirectoryNotFoundException("The media folder does not exist"); + } + + _libraryMonitorFactory().Stop(); + + try + { + _fileSystem.DeleteDirectory(path, true); + } + finally + { + Task.Run(() => + { + // No need to start if scanning the library because it will handle it + if (refreshLibrary) + { + ValidateMediaLibrary(new Progress<double>(), CancellationToken.None); + } + else + { + // Need to add a delay here or directory watchers may still pick up the changes + var task = Task.Delay(1000); + // Have to block here to allow exceptions to bubble + Task.WaitAll(task); + + _libraryMonitorFactory().Start(); + } + }); + } + } + + private const string ShortcutFileExtension = ".mblink"; + private const string ShortcutFileSearch = "*" + ShortcutFileExtension; + public void AddMediaPath(string virtualFolderName, string path) + { + if (string.IsNullOrWhiteSpace(path)) + { + throw new ArgumentNullException("path"); + } + + if (!_fileSystem.DirectoryExists(path)) + { + throw new DirectoryNotFoundException("The path does not exist."); + } + + var rootFolderPath = ConfigurationManager.ApplicationPaths.DefaultUserViewsPath; + var virtualFolderPath = Path.Combine(rootFolderPath, virtualFolderName); + + var shortcutFilename = _fileSystem.GetFileNameWithoutExtension(path); + + var lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension); + + while (_fileSystem.FileExists(lnk)) + { + shortcutFilename += "1"; + lnk = Path.Combine(virtualFolderPath, shortcutFilename + ShortcutFileExtension); + } + + _fileSystem.CreateShortcut(lnk, path); + + RemoveContentTypeOverrides(path); + } + + private void RemoveContentTypeOverrides(string path) + { + if (string.IsNullOrWhiteSpace(path)) + { + throw new ArgumentNullException("path"); + } + + var removeList = new List<NameValuePair>(); + + foreach (var contentType in ConfigurationManager.Configuration.ContentTypes) + { + if (string.Equals(path, contentType.Name, StringComparison.OrdinalIgnoreCase) + || _fileSystem.ContainsSubPath(path, contentType.Name)) + { + removeList.Add(contentType); + } + } + + if (removeList.Count > 0) + { + ConfigurationManager.Configuration.ContentTypes = ConfigurationManager.Configuration.ContentTypes + .Except(removeList) + .ToArray(); + + ConfigurationManager.SaveConfiguration(); + } + } + + public void RemoveMediaPath(string virtualFolderName, string mediaPath) + { + if (string.IsNullOrWhiteSpace(mediaPath)) + { + throw new ArgumentNullException("mediaPath"); + } + + var rootFolderPath = ConfigurationManager.ApplicationPaths.DefaultUserViewsPath; + var path = Path.Combine(rootFolderPath, virtualFolderName); + + if (!_fileSystem.DirectoryExists(path)) + { + throw new DirectoryNotFoundException(string.Format("The media collection {0} does not exist", virtualFolderName)); + } + + var shortcut = Directory.EnumerateFiles(path, ShortcutFileSearch, SearchOption.AllDirectories).FirstOrDefault(f => _fileSystem.ResolveShortcut(f).Equals(mediaPath, StringComparison.OrdinalIgnoreCase)); + + if (!string.IsNullOrEmpty(shortcut)) + { + _fileSystem.DeleteFile(shortcut); + } + } } }
\ No newline at end of file |
