diff --git a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs index 964315fd2e..22212d287f 100644 --- a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs +++ b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs @@ -161,7 +161,7 @@ namespace MediaBrowser.Api.UserLibrary public string Id { get; set; } /// - /// Gets or sets a value indicating whether this is likes. + /// Gets or sets a value indicating whether this is likes. /// /// true if likes; otherwise, false. [ApiMember(Name = "Likes", Description = "Whether the user likes the item or not. true/false", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "POST")] @@ -212,6 +212,9 @@ namespace MediaBrowser.Api.UserLibrary public string Id { get; set; } } + /// + /// Class OnPlaybackStart + /// [Route("/Users/{UserId}/PlayingItems/{Id}", "POST")] [Api(Description = "Reports that a user has begun playing an item")] public class OnPlaybackStart : IReturnVoid @@ -231,6 +234,9 @@ namespace MediaBrowser.Api.UserLibrary public string Id { get; set; } } + /// + /// Class OnPlaybackProgress + /// [Route("/Users/{UserId}/PlayingItems/{Id}/Progress", "POST")] [Api(Description = "Reports a user's playback progress")] public class OnPlaybackProgress : IReturnVoid @@ -257,6 +263,9 @@ namespace MediaBrowser.Api.UserLibrary public long? PositionTicks { get; set; } } + /// + /// Class OnPlaybackStopped + /// [Route("/Users/{UserId}/PlayingItems/{Id}", "DELETE")] [Api(Description = "Reports that a user has stopped playing an item")] public class OnPlaybackStopped : IReturnVoid @@ -282,7 +291,7 @@ namespace MediaBrowser.Api.UserLibrary [ApiMember(Name = "PositionTicks", Description = "Optional. The position, in ticks, where playback stopped. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "DELETE")] public long? PositionTicks { get; set; } } - + /// /// Class GetLocalTrailers /// @@ -305,6 +314,28 @@ namespace MediaBrowser.Api.UserLibrary public string Id { get; set; } } + /// + /// Class GetThemeSongs + /// + [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 /// @@ -337,12 +368,21 @@ namespace MediaBrowser.Api.UserLibrary /// The _user manager /// private readonly IUserManager _userManager; + /// + /// The _user data repository + /// private readonly IUserDataRepository _userDataRepository; + /// + /// The _library manager + /// private readonly ILibraryManager _libraryManager; - + /// /// Initializes a new instance of the class. /// + /// The user manager. + /// The library manager. + /// The user data repository. /// jsonSerializer public UserLibraryService(IUserManager userManager, ILibraryManager libraryManager, IUserDataRepository userDataRepository) : base() @@ -396,6 +436,34 @@ 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).ToArray(); + + var result = new ThemeSongsResult + { + Items = items, + TotalRecordCount = items.Length, + OwnerId = DtoBuilder.GetClientItemId(item) + }; + + return ToOptimizedResult(result); + } + /// /// Gets the specified request. /// @@ -417,6 +485,11 @@ namespace MediaBrowser.Api.UserLibrary return ToOptimizedResult(result); } + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. public object Get(GetRootFolder request) { var user = _userManager.GetUserById(request.UserId); @@ -432,7 +505,7 @@ namespace MediaBrowser.Api.UserLibrary return ToOptimizedResult(result); } - + /// /// Gets the specified request. /// @@ -483,7 +556,7 @@ namespace MediaBrowser.Api.UserLibrary var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id); var key = item.GetUserDataKey(); - + // Get the user data for this item var data = _userDataRepository.GetUserData(user.Id, key).Result; @@ -506,7 +579,7 @@ namespace MediaBrowser.Api.UserLibrary var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id); var key = item.GetUserDataKey(); - + // Get the user data for this item var data = _userDataRepository.GetUserData(user.Id, key).Result; @@ -528,7 +601,7 @@ namespace MediaBrowser.Api.UserLibrary var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.Id, _userManager, _libraryManager, user.Id); var key = item.GetUserDataKey(); - + // Get the user data for this item var data = _userDataRepository.GetUserData(user.Id, key).Result; diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 7bcd3db912..c478151b49 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.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csproj index 41aa86693d..8e29d302c8 100644 --- a/MediaBrowser.Model/MediaBrowser.Model.csproj +++ b/MediaBrowser.Model/MediaBrowser.Model.csproj @@ -84,6 +84,7 @@ + diff --git a/MediaBrowser.Model/Querying/ThemeSongsResult.cs b/MediaBrowser.Model/Querying/ThemeSongsResult.cs new file mode 100644 index 0000000000..d1319f6e12 --- /dev/null +++ b/MediaBrowser.Model/Querying/ThemeSongsResult.cs @@ -0,0 +1,16 @@ +using System; + +namespace MediaBrowser.Model.Querying +{ + /// + /// Class ThemeSongsResult + /// + public class ThemeSongsResult : ItemsResult + { + /// + /// Gets or sets the owner id. + /// + /// The owner id. + public string OwnerId { get; set; } + } +} diff --git a/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs b/MediaBrowser.Server.Implementations/Library/CoreResolutionIgnoreRule.cs index cebd5e192a..04c247f7e1 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; @@ -16,7 +18,6 @@ namespace MediaBrowser.Server.Implementations.Library /// private static readonly List IgnoreFolders = new List { - "trailers", "metadata", "certificate", "backup", @@ -36,7 +37,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) @@ -48,6 +54,16 @@ namespace MediaBrowser.Server.Implementations.Library { return true; } + + if (string.Equals(filename, BaseItem.TrailerFolderName, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + + if (string.Equals(filename, BaseItem.ThemeSongsFolderName, StringComparison.OrdinalIgnoreCase)) + { + return true; + } } return false; diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 132dca4e22..5f07e0d87c 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 759fb0dc47..42aa7d52c0 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 56ea6b5c94..31331b60d3 100644 --- a/MediaBrowser.WebDashboard/packages.config +++ b/MediaBrowser.WebDashboard/packages.config @@ -1,6 +1,6 @@  - + \ No newline at end of file