From d78c378452df2ce75c7208fe7ba919ab54455448 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 24 Apr 2013 12:03:10 -0400 Subject: [PATCH] added theme song support --- .../UserLibrary/UserLibraryService.cs | 40 +++++++++ MediaBrowser.Controller/Entities/BaseItem.cs | 85 +++++++++++++++++++ .../Library/CoreResolutionIgnoreRule.cs | 11 ++- .../Library/LibraryManager.cs | 21 +++-- MediaBrowser.WebDashboard/ApiClient.js | 21 +++++ MediaBrowser.WebDashboard/packages.config | 2 +- 6 files changed, 171 insertions(+), 9 deletions(-) diff --git a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs index 964315fd2..cd42c99ea 100644 --- a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs +++ b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs @@ -305,6 +305,25 @@ namespace MediaBrowser.Api.UserLibrary public string Id { get; set; } } + [Route("/Users/{UserId}/Items/{Id}/ThemeSongs", "GET")] + [Api(Description = "Gets theme songs for an item")] + public class GetThemeSongs : IReturn> + { + /// + /// Gets or sets the user id. + /// + /// The user id. + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public Guid UserId { get; set; } + + /// + /// Gets or sets the id. + /// + /// The id. + [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public string Id { get; set; } + } + /// /// Class GetSpecialFeatures /// @@ -396,6 +415,27 @@ namespace MediaBrowser.Api.UserLibrary return ToOptimizedResult(items); } + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetThemeSongs request) + { + var user = _userManager.GetUserById(request.UserId); + + var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id); + + // Get everything + var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); + + var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository); + + var items = item.ThemeSongs.Select(i => dtoBuilder.GetBaseItemDto(i, user, fields)).AsParallel().Select(t => t.Result).ToList(); + + return ToOptimizedResult(items); + } + /// /// Gets the specified request. /// diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 7bcd3db91..c478151b4 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -35,6 +35,7 @@ namespace MediaBrowser.Controller.Entities /// The trailer folder name /// public const string TrailerFolderName = "trailers"; + public const string ThemeSongsFolderName = "theme-music"; /// /// Gets or sets the name. @@ -656,6 +657,28 @@ namespace MediaBrowser.Controller.Entities } } + private List _themeSongs; + private bool _themeSongsInitialized; + private object _themeSongsSyncLock = new object(); + [IgnoreDataMember] + public List ThemeSongs + { + get + { + LazyInitializer.EnsureInitialized(ref _themeSongs, ref _themeSongsInitialized, ref _themeSongsSyncLock, LoadThemeSongs); + return _themeSongs; + } + private set + { + _themeSongs = value; + + if (value == null) + { + _themeSongsInitialized = false; + } + } + } + /// /// Loads local trailers from the file system /// @@ -714,6 +737,64 @@ namespace MediaBrowser.Controller.Entities }).ToList(); } + /// + /// Loads the theme songs. + /// + /// List{Audio.Audio}. + private List LoadThemeSongs() + { + ItemResolveArgs resolveArgs; + + try + { + resolveArgs = ResolveArgs; + } + catch (IOException ex) + { + Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path); + return new List(); + } + + if (!resolveArgs.IsDirectory) + { + return new List(); + } + + var folder = resolveArgs.GetFileSystemEntryByName(ThemeSongsFolderName); + + // Path doesn't exist. No biggie + if (folder == null) + { + return new List(); + } + + IEnumerable files; + + try + { + files = FileSystem.GetFiles(folder.Value.Path); + } + catch (IOException ex) + { + Logger.ErrorException("Error loading theme songs for {0}", ex, Name); + return new List(); + } + + return LibraryManager.ResolvePaths(files, null).Select(audio => + { + // Try to retrieve it from the db. If we don't find it, use the resolved version + var dbItem = LibraryManager.RetrieveItem(audio.Id) as Audio.Audio; + + if (dbItem != null) + { + dbItem.ResolveArgs = audio.ResolveArgs; + audio = dbItem; + } + + return audio; + }).ToList(); + } + /// /// Overrides the base implementation to refresh metadata for local trailers /// @@ -732,6 +813,7 @@ namespace MediaBrowser.Controller.Entities // Lazy load these again LocalTrailers = null; + ThemeSongs = null; // Refresh for the item var itemRefreshTask = ProviderManager.ExecuteMetadataProviders(this, cancellationToken, forceRefresh, allowSlowProviders); @@ -741,10 +823,13 @@ namespace MediaBrowser.Controller.Entities // Refresh metadata for local trailers var trailerTasks = LocalTrailers.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders)); + var themeSongTasks = ThemeSongs.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders)); + cancellationToken.ThrowIfCancellationRequested(); // Await the trailer tasks await Task.WhenAll(trailerTasks).ConfigureAwait(false); + await Task.WhenAll(themeSongTasks).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); diff --git a/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs b/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs index cebd5e192..a120f074e 100644 --- a/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs +++ b/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs @@ -1,4 +1,6 @@ -using MediaBrowser.Controller.Library; +using System.IO; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; using System; using System.Collections.Generic; using System.Linq; @@ -36,7 +38,12 @@ namespace MediaBrowser.Server.Implementations.Library // Ignore hidden files and folders if (args.IsHidden) { - return true; + var parentFolderName = Path.GetFileName(Path.GetDirectoryName(args.Path)); + + if (!string.Equals(parentFolderName, BaseItem.ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase)) + { + return true; + } } if (args.IsDirectory) diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 132dca4e2..5f07e0d87 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -274,9 +274,11 @@ namespace MediaBrowser.Server.Implementations.Library var specialFeatures = items.OfType().SelectMany(i => i.SpecialFeatures).ToList(); var localTrailers = items.SelectMany(i => i.LocalTrailers).ToList(); + var themeSongs = items.SelectMany(i => i.ThemeSongs).ToList(); items.AddRange(specialFeatures); items.AddRange(localTrailers); + items.AddRange(themeSongs); // Need to use DistinctBy Id because there could be multiple instances with the same id // due to sharing the default library @@ -325,22 +327,29 @@ namespace MediaBrowser.Server.Implementations.Library { LibraryItemsCache.AddOrUpdate(item.Id, item, delegate { return item; }); - foreach (var trailer in item.LocalTrailers) + foreach (var subItem in item.LocalTrailers) { // Prevent access to foreach variable in closure - var trailer1 = trailer; - LibraryItemsCache.AddOrUpdate(trailer.Id, trailer, delegate { return trailer1; }); + var trailer1 = subItem; + LibraryItemsCache.AddOrUpdate(subItem.Id, subItem, delegate { return trailer1; }); + } + + foreach (var subItem in item.ThemeSongs) + { + // Prevent access to foreach variable in closure + var trailer1 = subItem; + LibraryItemsCache.AddOrUpdate(subItem.Id, subItem, delegate { return trailer1; }); } var movie = item as Movie; if (movie != null) { - foreach (var special in movie.SpecialFeatures) + foreach (var subItem in movie.SpecialFeatures) { // Prevent access to foreach variable in closure - var special1 = special; - LibraryItemsCache.AddOrUpdate(special.Id, special, delegate { return special1; }); + var special1 = subItem; + LibraryItemsCache.AddOrUpdate(subItem.Id, subItem, delegate { return special1; }); } } } diff --git a/MediaBrowser.WebDashboard/ApiClient.js b/MediaBrowser.WebDashboard/ApiClient.js index 759fb0dc4..42aa7d52c 100644 --- a/MediaBrowser.WebDashboard/ApiClient.js +++ b/MediaBrowser.WebDashboard/ApiClient.js @@ -1712,6 +1712,27 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { }); }; + /** + * Gets theme songs for an item + */ + self.getThemeSongs = function (userId, itemId) { + + if (!userId) { + throw new Error("null userId"); + } + if (!itemId) { + throw new Error("null itemId"); + } + + var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/ThemeSongs"); + + return self.ajax({ + type: "GET", + url: url, + dataType: "json" + }); + }; + /** * Gets special features for an item */ diff --git a/MediaBrowser.WebDashboard/packages.config b/MediaBrowser.WebDashboard/packages.config index 56ea6b5c9..31331b60d 100644 --- a/MediaBrowser.WebDashboard/packages.config +++ b/MediaBrowser.WebDashboard/packages.config @@ -1,6 +1,6 @@  - + \ No newline at end of file