2013-05-15 16:56:38 +00:00
using MediaBrowser.Controller.Dto ;
using MediaBrowser.Controller.Entities ;
2013-05-14 05:36:36 +00:00
using MediaBrowser.Controller.Entities.Audio ;
using MediaBrowser.Controller.Entities.Movies ;
using MediaBrowser.Controller.Entities.TV ;
2013-05-13 01:46:36 +00:00
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.Persistence ;
2013-05-14 05:36:36 +00:00
using MediaBrowser.Model.Dto ;
2013-05-15 22:55:24 +00:00
using MediaBrowser.Model.Entities ;
2013-05-11 04:10:58 +00:00
using MediaBrowser.Model.Querying ;
2013-12-07 15:52:38 +00:00
using ServiceStack ;
2013-05-11 04:10:58 +00:00
using System ;
2013-10-06 03:03:08 +00:00
using System.Collections ;
2013-05-14 05:36:36 +00:00
using System.Collections.Generic ;
2013-10-06 03:03:08 +00:00
using System.Globalization ;
2013-05-22 14:44:51 +00:00
using System.IO ;
2013-05-11 04:10:58 +00:00
using System.Linq ;
2013-05-14 05:36:36 +00:00
using System.Threading ;
2013-05-11 04:10:58 +00:00
using System.Threading.Tasks ;
namespace MediaBrowser.Api
{
2013-08-28 14:26:19 +00:00
[Route("/Items/{Id}/File", "GET")]
[Api(Description = "Gets the original file of an item")]
2013-09-17 15:14:22 +00:00
public class GetFile
2013-08-28 14:26:19 +00:00
{
/// <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 ; }
}
2013-09-17 15:14:22 +00:00
2013-05-11 04:10:58 +00:00
/// <summary>
/// Class GetCriticReviews
/// </summary>
[Route("/Items/{Id}/CriticReviews", "GET")]
[Api(Description = "Gets critic reviews for an item")]
2013-11-25 20:39:23 +00:00
public class GetCriticReviews : IReturn < QueryResult < ItemReview > >
2013-05-11 04:10:58 +00:00
{
/// <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>
/// Skips over a given number of items within the results. Use for paging.
/// </summary>
/// <value>The start index.</value>
[ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? StartIndex { get ; set ; }
/// <summary>
/// The maximum number of items to return
/// </summary>
/// <value>The limit.</value>
[ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? Limit { get ; set ; }
}
2013-05-15 16:56:38 +00:00
/// <summary>
/// Class GetThemeSongs
/// </summary>
[Route("/Items/{Id}/ThemeSongs", "GET")]
[Api(Description = "Gets theme songs for an item")]
2013-07-10 20:06:11 +00:00
public class GetThemeSongs : IReturn < ThemeMediaResult >
2013-05-15 16:56:38 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", 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 ; }
2013-07-10 20:06:11 +00:00
[ApiMember(Name = "InheritFromParent", Description = "Determines whether or not parent items should be searched for theme media.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public bool InheritFromParent { get ; set ; }
2013-05-15 16:56:38 +00:00
}
/// <summary>
/// Class GetThemeVideos
/// </summary>
[Route("/Items/{Id}/ThemeVideos", "GET")]
2013-07-10 20:06:11 +00:00
[Api(Description = "Gets theme videos for an item")]
public class GetThemeVideos : IReturn < ThemeMediaResult >
2013-05-15 16:56:38 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", 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 ; }
2013-07-10 20:06:11 +00:00
[ApiMember(Name = "InheritFromParent", Description = "Determines whether or not parent items should be searched for theme media.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public bool InheritFromParent { get ; set ; }
}
/// <summary>
/// Class GetThemeVideos
/// </summary>
[Route("/Items/{Id}/ThemeMedia", "GET")]
[Api(Description = "Gets theme videos and songs for an item")]
2013-11-12 15:36:08 +00:00
public class GetThemeMedia : IReturn < AllThemeMediaResult >
2013-07-10 20:06:11 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", 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 ; }
[ApiMember(Name = "InheritFromParent", Description = "Determines whether or not parent items should be searched for theme media.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public bool InheritFromParent { get ; set ; }
2013-05-15 16:56:38 +00:00
}
2013-05-13 01:46:36 +00:00
[Route("/Library/Refresh", "POST")]
[Api(Description = "Starts a library scan")]
public class RefreshLibrary : IReturnVoid
{
}
2013-05-14 05:36:36 +00:00
2013-05-22 14:44:51 +00:00
[Route("/Items/{Id}", "DELETE")]
[Api(Description = "Deletes an item from the library and file system")]
public class DeleteItem : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get ; set ; }
}
2013-05-14 05:36:36 +00:00
[Route("/Items/Counts", "GET")]
[Api(Description = "Gets counts of various item types")]
public class GetItemCounts : IReturn < ItemCounts >
{
[ApiMember(Name = "UserId", Description = "Optional. Get counts from a specific user's library.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid ? UserId { get ; set ; }
2013-10-21 03:01:22 +00:00
[ApiMember(Name = "IsFavorite", Description = "Optional. Get counts of favorite items", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool? IsFavorite { get ; set ; }
2013-05-14 05:36:36 +00:00
}
2013-07-10 20:06:11 +00:00
2013-08-02 20:36:44 +00:00
[Route("/Items/{Id}/Ancestors", "GET")]
[Api(Description = "Gets all parents of an item")]
public class GetAncestors : IReturn < BaseItemDto [ ] >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", 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 ; }
}
2013-09-17 15:14:22 +00:00
2013-10-06 03:03:08 +00:00
[Route("/Items/YearIndex", "GET")]
[Api(Description = "Gets a year index based on an item query.")]
public class GetYearIndex : IReturn < List < ItemIndex > >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid ? UserId { get ; set ; }
[ApiMember(Name = "IncludeItemTypes", Description = "Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimeted.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string IncludeItemTypes { get ; set ; }
}
2013-05-11 04:10:58 +00:00
/// <summary>
/// Class LibraryService
/// </summary>
public class LibraryService : BaseApiService
{
/// <summary>
/// The _item repo
/// </summary>
private readonly IItemRepository _itemRepo ;
2013-05-13 01:46:36 +00:00
private readonly ILibraryManager _libraryManager ;
2013-05-14 05:36:36 +00:00
private readonly IUserManager _userManager ;
2013-10-21 01:03:33 +00:00
private readonly IUserDataManager _userDataManager ;
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService ;
2013-05-13 01:46:36 +00:00
2013-05-11 04:10:58 +00:00
/// <summary>
2013-05-13 01:46:36 +00:00
/// Initializes a new instance of the <see cref="LibraryService" /> class.
2013-05-11 04:10:58 +00:00
/// </summary>
2013-05-22 19:49:12 +00:00
public LibraryService ( IItemRepository itemRepo , ILibraryManager libraryManager , IUserManager userManager ,
2013-10-31 21:03:24 +00:00
IDtoService dtoService , IUserDataManager userDataManager )
2013-05-11 04:10:58 +00:00
{
_itemRepo = itemRepo ;
2013-05-13 01:46:36 +00:00
_libraryManager = libraryManager ;
2013-05-14 05:36:36 +00:00
_userManager = userManager ;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService ;
2013-10-21 01:03:33 +00:00
_userDataManager = userDataManager ;
2013-05-11 04:10:58 +00:00
}
2013-08-28 14:26:19 +00:00
public object Get ( GetFile request )
{
2013-09-04 17:02:19 +00:00
var item = _dtoService . GetItemByDtoId ( request . Id ) ;
2013-08-28 14:26:19 +00:00
if ( item . LocationType = = LocationType . Remote | | item . LocationType = = LocationType . Virtual )
{
throw new ArgumentException ( "This command cannot be used for remote or virtual items." ) ;
}
if ( Directory . Exists ( item . Path ) )
{
throw new ArgumentException ( "This command cannot be used for directories." ) ;
}
return ToStaticFileResult ( item . Path ) ;
}
2013-09-17 15:14:22 +00:00
2013-08-02 20:36:44 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetAncestors request )
{
2013-09-17 02:44:06 +00:00
var result = GetAncestors ( request ) ;
2013-08-02 20:36:44 +00:00
return ToOptimizedResult ( result ) ;
}
/// <summary>
/// Gets the ancestors.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task{BaseItemDto[]}.</returns>
2013-09-20 00:53:18 +00:00
public List < BaseItemDto > GetAncestors ( GetAncestors request )
2013-08-02 20:36:44 +00:00
{
2013-09-04 17:02:19 +00:00
var item = _dtoService . GetItemByDtoId ( request . Id ) ;
2013-08-02 20:36:44 +00:00
2013-09-17 02:44:06 +00:00
var baseItemDtos = new List < BaseItemDto > ( ) ;
2013-08-02 20:36:44 +00:00
var user = request . UserId . HasValue ? _userManager . GetUserById ( request . UserId . Value ) : null ;
// Get everything
var fields = Enum . GetNames ( typeof ( ItemFields ) )
. Select ( i = > ( ItemFields ) Enum . Parse ( typeof ( ItemFields ) , i , true ) )
. ToList ( ) ;
BaseItem parent = item . Parent ;
while ( parent ! = null )
{
if ( user ! = null )
{
parent = TranslateParentItem ( parent , user ) ;
}
2013-09-17 02:44:06 +00:00
baseItemDtos . Add ( _dtoService . GetBaseItemDto ( parent , fields , user ) ) ;
2013-08-02 20:36:44 +00:00
if ( parent is UserRootFolder )
{
break ;
}
parent = parent . Parent ;
}
2013-09-20 00:53:18 +00:00
return baseItemDtos . ToList ( ) ;
2013-08-02 20:36:44 +00:00
}
private BaseItem TranslateParentItem ( BaseItem item , User user )
{
if ( item . Parent is AggregateFolder )
{
return user . RootFolder . GetChildren ( user , true ) . FirstOrDefault ( i = >
{
try
{
return i . LocationType = = LocationType . FileSystem & &
i . ResolveArgs . PhysicalLocations . Contains ( item . Path ) ;
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error getting ResolveArgs for {0}" , ex , i . Path ) ;
return false ;
}
} ) ;
}
return item ;
}
2013-05-11 04:10:58 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetCriticReviews request )
{
2013-09-17 15:14:22 +00:00
var result = GetCriticReviews ( request ) ;
2013-05-11 04:10:58 +00:00
return ToOptimizedResult ( result ) ;
}
2013-05-14 05:36:36 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetItemCounts request )
{
2013-10-26 22:01:21 +00:00
var items = GetAllLibraryItems ( request . UserId , _userManager , _libraryManager )
. Where ( i = > i . LocationType ! = LocationType . Virtual )
. ToList ( ) ;
2013-05-14 05:36:36 +00:00
2013-10-21 03:01:22 +00:00
var filteredItems = request . UserId . HasValue ? FilterItems ( items , request , request . UserId . Value ) . ToList ( ) : items ;
var albums = filteredItems . OfType < MusicAlbum > ( ) . ToList ( ) ;
var episodes = filteredItems . OfType < Episode > ( ) . ToList ( ) ;
var games = filteredItems . OfType < Game > ( ) . ToList ( ) ;
var movies = filteredItems . OfType < Movie > ( ) . ToList ( ) ;
var musicVideos = filteredItems . OfType < MusicVideo > ( ) . ToList ( ) ;
var adultVideos = filteredItems . OfType < AdultVideo > ( ) . ToList ( ) ;
var boxsets = filteredItems . OfType < BoxSet > ( ) . ToList ( ) ;
var books = filteredItems . OfType < Book > ( ) . ToList ( ) ;
var songs = filteredItems . OfType < Audio > ( ) . ToList ( ) ;
var series = filteredItems . OfType < Series > ( ) . ToList ( ) ;
2013-10-21 01:03:33 +00:00
2013-05-14 05:36:36 +00:00
var counts = new ItemCounts
2013-05-27 16:53:10 +00:00
{
2013-10-21 01:03:33 +00:00
AlbumCount = albums . Count ,
EpisodeCount = episodes . Count ,
GameCount = games . Count ,
2013-10-21 03:01:22 +00:00
GameSystemCount = filteredItems . OfType < GameSystem > ( ) . Count ( ) ,
2013-10-21 01:03:33 +00:00
MovieCount = movies . Count ,
SeriesCount = series . Count ,
SongCount = songs . Count ,
2013-10-21 03:01:22 +00:00
TrailerCount = filteredItems . OfType < Trailer > ( ) . Count ( ) ,
2013-10-21 01:03:33 +00:00
MusicVideoCount = musicVideos . Count ,
AdultVideoCount = adultVideos . Count ,
BoxSetCount = boxsets . Count ,
BookCount = books . Count ,
2013-10-02 14:08:30 +00:00
2013-11-21 20:48:26 +00:00
UniqueTypes = items . Select ( i = > i . GetClientTypeName ( ) ) . Distinct ( ) . ToList ( )
2013-05-27 16:53:10 +00:00
} ;
2013-05-14 05:36:36 +00:00
return ToOptimizedResult ( counts ) ;
}
2013-10-21 03:01:22 +00:00
private IEnumerable < T > FilterItems < T > ( IEnumerable < T > items , GetItemCounts request , Guid userId )
where T : BaseItem
{
if ( request . IsFavorite . HasValue )
{
var val = request . IsFavorite . Value ;
items = items . Where ( i = > _userDataManager . GetUserData ( userId , i . GetUserDataKey ( ) ) . IsFavorite = = val ) ;
}
return items ;
}
2013-10-21 01:03:33 +00:00
2013-05-13 01:46:36 +00:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2013-05-22 14:44:51 +00:00
public async void Post ( RefreshLibrary request )
2013-05-13 01:46:36 +00:00
{
2013-05-22 14:44:51 +00:00
try
{
2013-08-02 20:36:44 +00:00
await _libraryManager . ValidateMediaLibrary ( new Progress < double > ( ) , CancellationToken . None )
2013-05-22 19:49:12 +00:00
. ConfigureAwait ( false ) ;
2013-05-22 14:44:51 +00:00
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error refreshing library" , ex ) ;
}
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
2013-06-06 12:08:46 +00:00
public void Delete ( DeleteItem request )
{
var task = DeleteItem ( request ) ;
Task . WaitAll ( task ) ;
}
private async Task DeleteItem ( DeleteItem request )
2013-05-22 14:44:51 +00:00
{
2013-09-04 17:02:19 +00:00
var item = _dtoService . GetItemByDtoId ( request . Id ) ;
2013-05-22 14:44:51 +00:00
var parent = item . Parent ;
2013-07-06 23:08:32 +00:00
if ( item . LocationType = = LocationType . Offline )
{
throw new InvalidOperationException ( string . Format ( "{0} is currently offline." , item . Name ) ) ;
}
2013-07-10 20:06:11 +00:00
2013-05-22 14:44:51 +00:00
if ( item . LocationType = = LocationType . FileSystem )
{
2013-12-01 19:31:58 +00:00
foreach ( var path in item . GetDeletePaths ( ) . ToList ( ) )
2013-05-22 14:44:51 +00:00
{
2013-12-01 19:31:58 +00:00
if ( Directory . Exists ( path ) )
{
Directory . Delete ( path , true ) ;
}
else if ( File . Exists ( path ) )
{
File . Delete ( path ) ;
}
2013-05-22 14:44:51 +00:00
}
if ( parent ! = null )
{
try
{
2013-11-04 16:20:44 +00:00
await parent . ValidateChildren ( new Progress < double > ( ) , CancellationToken . None )
2013-05-22 19:49:12 +00:00
. ConfigureAwait ( false ) ;
2013-05-22 14:44:51 +00:00
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error refreshing item" , ex ) ;
}
}
}
else if ( parent ! = null )
{
try
{
await parent . RemoveChild ( item , CancellationToken . None ) . ConfigureAwait ( false ) ;
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error removing item" , ex ) ;
}
}
else
{
throw new InvalidOperationException ( "Don't know how to delete " + item . Name ) ;
}
2013-05-13 01:46:36 +00:00
}
2013-05-11 04:10:58 +00:00
/// <summary>
/// Gets the critic reviews async.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task{ItemReviewsResult}.</returns>
2013-11-25 20:39:23 +00:00
private QueryResult < ItemReview > GetCriticReviews ( GetCriticReviews request )
2013-05-11 04:10:58 +00:00
{
2013-09-17 15:14:22 +00:00
var reviews = _itemRepo . GetCriticReviews ( new Guid ( request . Id ) ) ;
2013-05-11 04:10:58 +00:00
var reviewsArray = reviews . ToArray ( ) ;
2013-11-25 20:39:23 +00:00
var result = new QueryResult < ItemReview >
{
TotalRecordCount = reviewsArray . Length
} ;
2013-05-11 04:10:58 +00:00
if ( request . StartIndex . HasValue )
{
reviewsArray = reviewsArray . Skip ( request . StartIndex . Value ) . ToArray ( ) ;
}
if ( request . Limit . HasValue )
{
reviewsArray = reviewsArray . Take ( request . Limit . Value ) . ToArray ( ) ;
}
2013-11-25 20:39:23 +00:00
result . Items = reviewsArray ;
2013-05-11 04:10:58 +00:00
return result ;
}
2013-05-15 16:56:38 +00:00
2013-07-10 20:06:11 +00:00
public object Get ( GetThemeMedia request )
{
var themeSongs = GetThemeSongs ( new GetThemeSongs
{
InheritFromParent = request . InheritFromParent ,
Id = request . Id ,
UserId = request . UserId
2013-09-20 00:53:18 +00:00
} ) ;
2013-07-10 20:06:11 +00:00
var themeVideos = GetThemeVideos ( new GetThemeVideos
{
InheritFromParent = request . InheritFromParent ,
Id = request . Id ,
UserId = request . UserId
2013-09-20 00:53:18 +00:00
} ) ;
2013-07-10 20:06:11 +00:00
return ToOptimizedResult ( new AllThemeMediaResult
{
ThemeSongsResult = themeSongs ,
2013-11-12 15:36:08 +00:00
ThemeVideosResult = themeVideos ,
SoundtrackSongsResult = GetSoundtrackSongs ( request . Id , request . UserId , request . InheritFromParent )
2013-07-10 20:06:11 +00:00
} ) ;
}
2013-05-15 16:56:38 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetThemeSongs request )
2013-07-10 20:06:11 +00:00
{
2013-09-20 00:53:18 +00:00
var result = GetThemeSongs ( request ) ;
2013-07-10 20:06:11 +00:00
return ToOptimizedResult ( result ) ;
}
2013-09-20 00:53:18 +00:00
private ThemeMediaResult GetThemeSongs ( GetThemeSongs request )
2013-05-15 16:56:38 +00:00
{
var user = request . UserId . HasValue ? _userManager . GetUserById ( request . UserId . Value ) : null ;
2013-05-22 19:49:12 +00:00
var item = string . IsNullOrEmpty ( request . Id )
? ( request . UserId . HasValue
? user . RootFolder
: ( Folder ) _libraryManager . RootFolder )
2013-09-04 17:02:19 +00:00
: _dtoService . GetItemByDtoId ( request . Id , request . UserId ) ;
2013-05-15 16:56:38 +00:00
2013-11-13 19:35:00 +00:00
var originalItem = item ;
2013-12-05 16:50:21 +00:00
while ( GetThemeSongIds ( item ) . Count = = 0 & & request . InheritFromParent & & item . Parent ! = null )
2013-07-10 20:06:11 +00:00
{
item = item . Parent ;
}
2013-05-15 16:56:38 +00:00
// Get everything
2013-06-12 21:46:50 +00:00
var fields = Enum . GetNames ( typeof ( ItemFields ) )
2013-05-22 19:49:12 +00:00
. Select ( i = > ( ItemFields ) Enum . Parse ( typeof ( ItemFields ) , i , true ) )
. ToList ( ) ;
2013-05-15 16:56:38 +00:00
2013-12-05 16:50:21 +00:00
var themeSongIds = GetThemeSongIds ( item ) ;
2013-11-13 19:35:00 +00:00
if ( themeSongIds . Count = = 0 & & request . InheritFromParent )
{
var album = originalItem as MusicAlbum ;
if ( album ! = null )
{
2013-11-13 19:45:20 +00:00
var linkedItemWithThemes = album . SoundtrackIds
. Select ( i = > _libraryManager . GetItemById ( i ) )
2013-12-05 16:50:21 +00:00
. FirstOrDefault ( i = > GetThemeSongIds ( i ) . Count > 0 ) ;
2013-11-13 19:45:20 +00:00
if ( linkedItemWithThemes ! = null )
{
2013-12-05 16:50:21 +00:00
themeSongIds = GetThemeSongIds ( linkedItemWithThemes ) ;
2013-11-13 19:45:20 +00:00
item = linkedItemWithThemes ;
}
2013-11-13 19:35:00 +00:00
}
}
var dtos = themeSongIds . Select ( _libraryManager . GetItemById )
2013-07-10 20:06:11 +00:00
. OrderBy ( i = > i . SortName )
2013-09-04 17:02:19 +00:00
. Select ( i = > _dtoService . GetBaseItemDto ( i , fields , user , item ) ) ;
2013-07-10 20:06:11 +00:00
2013-09-17 02:44:06 +00:00
var items = dtos . ToArray ( ) ;
2013-05-15 16:56:38 +00:00
2013-07-10 20:06:11 +00:00
return new ThemeMediaResult
2013-06-12 21:46:50 +00:00
{
Items = items ,
TotalRecordCount = items . Length ,
2013-09-04 17:02:19 +00:00
OwnerId = _dtoService . GetDtoId ( item )
2013-06-12 21:46:50 +00:00
} ;
2013-05-15 16:56:38 +00:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetThemeVideos request )
2013-07-10 20:06:11 +00:00
{
2013-09-20 00:53:18 +00:00
var result = GetThemeVideos ( request ) ;
2013-07-10 20:06:11 +00:00
return ToOptimizedResult ( result ) ;
}
2013-09-20 00:53:18 +00:00
public ThemeMediaResult GetThemeVideos ( GetThemeVideos request )
2013-05-15 16:56:38 +00:00
{
var user = request . UserId . HasValue ? _userManager . GetUserById ( request . UserId . Value ) : null ;
2013-05-22 19:49:12 +00:00
var item = string . IsNullOrEmpty ( request . Id )
? ( request . UserId . HasValue
? user . RootFolder
: ( Folder ) _libraryManager . RootFolder )
2013-09-04 17:02:19 +00:00
: _dtoService . GetItemByDtoId ( request . Id , request . UserId ) ;
2013-05-15 16:56:38 +00:00
2013-11-13 19:35:00 +00:00
var originalItem = item ;
2013-12-05 16:50:21 +00:00
while ( GetThemeVideoIds ( item ) . Count = = 0 & & request . InheritFromParent & & item . Parent ! = null )
2013-07-10 20:06:11 +00:00
{
item = item . Parent ;
}
2013-05-15 16:56:38 +00:00
// Get everything
2013-11-12 15:36:08 +00:00
var fields = Enum . GetNames ( typeof ( ItemFields ) )
2013-05-22 19:49:12 +00:00
. Select ( i = > ( ItemFields ) Enum . Parse ( typeof ( ItemFields ) , i , true ) )
. ToList ( ) ;
2013-05-15 16:56:38 +00:00
2013-12-05 16:50:21 +00:00
var themeVideoIds = GetThemeVideoIds ( item ) ;
2013-11-13 19:35:00 +00:00
if ( themeVideoIds . Count = = 0 & & request . InheritFromParent )
{
var album = originalItem as MusicAlbum ;
2013-11-20 03:15:48 +00:00
if ( album = = null )
{
album = originalItem . Parents . OfType < MusicAlbum > ( ) . FirstOrDefault ( ) ;
}
2013-11-13 19:35:00 +00:00
if ( album ! = null )
{
2013-11-13 19:45:20 +00:00
var linkedItemWithThemes = album . SoundtrackIds
. Select ( i = > _libraryManager . GetItemById ( i ) )
2013-12-05 16:50:21 +00:00
. FirstOrDefault ( i = > GetThemeVideoIds ( i ) . Count > 0 ) ;
2013-11-13 19:45:20 +00:00
if ( linkedItemWithThemes ! = null )
{
2013-12-05 16:50:21 +00:00
themeVideoIds = GetThemeVideoIds ( linkedItemWithThemes ) ;
2013-11-13 19:45:20 +00:00
item = linkedItemWithThemes ;
}
2013-11-13 19:35:00 +00:00
}
}
var dtos = themeVideoIds . Select ( _libraryManager . GetItemById )
2013-07-10 20:06:11 +00:00
. OrderBy ( i = > i . SortName )
2013-09-04 17:02:19 +00:00
. Select ( i = > _dtoService . GetBaseItemDto ( i , fields , user , item ) ) ;
2013-05-15 16:56:38 +00:00
2013-09-17 02:44:06 +00:00
var items = dtos . ToArray ( ) ;
2013-05-15 16:56:38 +00:00
2013-07-10 20:06:11 +00:00
return new ThemeMediaResult
{
Items = items ,
TotalRecordCount = items . Length ,
2013-09-04 17:02:19 +00:00
OwnerId = _dtoService . GetDtoId ( item )
2013-07-10 20:06:11 +00:00
} ;
2013-05-15 16:56:38 +00:00
}
2013-10-06 03:03:08 +00:00
2013-12-05 16:50:21 +00:00
private List < Guid > GetThemeVideoIds ( BaseItem item )
{
var i = item as IHasThemeMedia ;
if ( i ! = null )
{
return i . ThemeVideoIds ;
}
return new List < Guid > ( ) ;
}
private List < Guid > GetThemeSongIds ( BaseItem item )
{
var i = item as IHasThemeMedia ;
if ( i ! = null )
{
return i . ThemeSongIds ;
}
return new List < Guid > ( ) ;
}
2013-11-12 15:36:08 +00:00
private readonly CultureInfo _usCulture = new CultureInfo ( "en-US" ) ;
2013-10-21 01:51:57 +00:00
2013-10-06 03:03:08 +00:00
public object Get ( GetYearIndex request )
{
IEnumerable < BaseItem > items = GetAllLibraryItems ( request . UserId , _userManager , _libraryManager ) ;
if ( ! string . IsNullOrEmpty ( request . IncludeItemTypes ) )
{
var vals = request . IncludeItemTypes . Split ( ',' ) ;
items = items . Where ( f = > vals . Contains ( f . GetType ( ) . Name , StringComparer . OrdinalIgnoreCase ) ) ;
}
2013-10-21 01:51:57 +00:00
2013-10-06 03:03:08 +00:00
var lookup = items
. ToLookup ( i = > i . ProductionYear ? ? - 1 )
. OrderBy ( i = > i . Key )
. Select ( i = > new ItemIndex
{
ItemCount = i . Count ( ) ,
2013-11-12 15:36:08 +00:00
Name = i . Key = = - 1 ? string . Empty : i . Key . ToString ( _usCulture )
2013-10-06 03:03:08 +00:00
} )
. ToList ( ) ;
return ToOptimizedResult ( lookup ) ;
}
2013-11-12 15:36:08 +00:00
public ThemeMediaResult GetSoundtrackSongs ( string id , Guid ? userId , bool inheritFromParent )
{
var user = userId . HasValue ? _userManager . GetUserById ( userId . Value ) : null ;
var item = string . IsNullOrEmpty ( id )
? ( userId . HasValue
? user . RootFolder
: ( Folder ) _libraryManager . RootFolder )
: _dtoService . GetItemByDtoId ( id , userId ) ;
// Get everything
var fields = Enum . GetNames ( typeof ( ItemFields ) )
. Select ( i = > ( ItemFields ) Enum . Parse ( typeof ( ItemFields ) , i , true ) )
. ToList ( ) ;
2013-11-20 03:15:48 +00:00
var dtos = GetSoundtrackSongIds ( item , inheritFromParent )
2013-11-12 15:36:08 +00:00
. Select ( _libraryManager . GetItemById )
. OfType < MusicAlbum > ( )
. SelectMany ( i = > i . RecursiveChildren )
. OfType < Audio > ( )
. OrderBy ( i = > i . SortName )
. Select ( i = > _dtoService . GetBaseItemDto ( i , fields , user , item ) ) ;
var items = dtos . ToArray ( ) ;
return new ThemeMediaResult
{
Items = items ,
TotalRecordCount = items . Length ,
OwnerId = _dtoService . GetDtoId ( item )
} ;
}
2013-11-20 03:15:48 +00:00
private IEnumerable < Guid > GetSoundtrackSongIds ( BaseItem item , bool inherit )
2013-11-12 15:36:08 +00:00
{
var hasSoundtracks = item as IHasSoundtracks ;
if ( hasSoundtracks ! = null )
{
return hasSoundtracks . SoundtrackIds ;
}
2013-11-20 03:15:48 +00:00
if ( ! inherit )
{
return null ;
}
hasSoundtracks = item . Parents . OfType < IHasSoundtracks > ( ) . FirstOrDefault ( ) ;
return hasSoundtracks ! = null ? hasSoundtracks . SoundtrackIds : new List < Guid > ( ) ;
2013-11-12 15:36:08 +00:00
}
2013-05-11 04:10:58 +00:00
}
}