added theme song support

This commit is contained in:
Luke Pulverenti 2013-04-24 12:03:10 -04:00
parent 0d15e1d631
commit d78c378452
6 changed files with 171 additions and 9 deletions

View File

@ -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<List<BaseItemDto>>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
/// <summary>
/// Class GetSpecialFeatures
/// </summary>
@ -396,6 +415,27 @@ namespace MediaBrowser.Api.UserLibrary
return ToOptimizedResult(items);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
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);
}
/// <summary>
/// Gets the specified request.
/// </summary>

View File

@ -35,6 +35,7 @@ namespace MediaBrowser.Controller.Entities
/// The trailer folder name
/// </summary>
public const string TrailerFolderName = "trailers";
public const string ThemeSongsFolderName = "theme-music";
/// <summary>
/// Gets or sets the name.
@ -656,6 +657,28 @@ namespace MediaBrowser.Controller.Entities
}
}
private List<Audio.Audio> _themeSongs;
private bool _themeSongsInitialized;
private object _themeSongsSyncLock = new object();
[IgnoreDataMember]
public List<Audio.Audio> ThemeSongs
{
get
{
LazyInitializer.EnsureInitialized(ref _themeSongs, ref _themeSongsInitialized, ref _themeSongsSyncLock, LoadThemeSongs);
return _themeSongs;
}
private set
{
_themeSongs = value;
if (value == null)
{
_themeSongsInitialized = false;
}
}
}
/// <summary>
/// Loads local trailers from the file system
/// </summary>
@ -714,6 +737,64 @@ namespace MediaBrowser.Controller.Entities
}).ToList();
}
/// <summary>
/// Loads the theme songs.
/// </summary>
/// <returns>List{Audio.Audio}.</returns>
private List<Audio.Audio> LoadThemeSongs()
{
ItemResolveArgs resolveArgs;
try
{
resolveArgs = ResolveArgs;
}
catch (IOException ex)
{
Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
return new List<Audio.Audio>();
}
if (!resolveArgs.IsDirectory)
{
return new List<Audio.Audio>();
}
var folder = resolveArgs.GetFileSystemEntryByName(ThemeSongsFolderName);
// Path doesn't exist. No biggie
if (folder == null)
{
return new List<Audio.Audio>();
}
IEnumerable<WIN32_FIND_DATA> files;
try
{
files = FileSystem.GetFiles(folder.Value.Path);
}
catch (IOException ex)
{
Logger.ErrorException("Error loading theme songs for {0}", ex, Name);
return new List<Audio.Audio>();
}
return LibraryManager.ResolvePaths<Audio.Audio>(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();
}
/// <summary>
/// Overrides the base implementation to refresh metadata for local trailers
/// </summary>
@ -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();

View File

@ -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)

View File

@ -274,9 +274,11 @@ namespace MediaBrowser.Server.Implementations.Library
var specialFeatures = items.OfType<Movie>().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; });
}
}
}

View File

@ -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
*/

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MediaBrowser.ApiClient.Javascript" version="3.0.85" targetFramework="net45" />
<package id="MediaBrowser.ApiClient.Javascript" version="3.0.86" targetFramework="net45" />
<package id="ServiceStack.Common" version="3.9.43" targetFramework="net45" />
<package id="ServiceStack.Text" version="3.9.43" targetFramework="net45" />
</packages>