2014-04-26 23:42:05 -04:00
using MediaBrowser.Controller.Entities ;
2013-09-05 15:00:50 -04:00
using MediaBrowser.Controller.Entities.Audio ;
2013-06-27 15:29:58 -04:00
using MediaBrowser.Controller.Library ;
2014-07-02 14:34:08 -04:00
using MediaBrowser.Controller.Net ;
2014-01-28 13:37:01 -05:00
using MediaBrowser.Controller.Providers ;
2013-12-07 10:52:38 -05:00
using ServiceStack ;
2013-06-27 15:29:58 -04:00
using System ;
2013-09-04 13:02:19 -04:00
using System.Linq ;
2013-06-27 15:29:58 -04:00
using System.Threading ;
using System.Threading.Tasks ;
namespace MediaBrowser.Api
{
2014-02-04 15:19:29 -05:00
public class BaseRefreshRequest : IReturnVoid
2013-06-27 15:29:58 -04:00
{
2014-07-03 22:22:57 -04:00
[ApiMember(Name = "MetadataRefreshMode", Description = "Specifies the metadata refresh mode", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
public MetadataRefreshMode MetadataRefreshMode { get ; set ; }
2014-06-14 22:24:04 -04:00
2014-07-03 22:22:57 -04:00
[ApiMember(Name = "ImageRefreshMode", Description = "Specifies the image refresh mode", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
public ImageRefreshMode ImageRefreshMode { get ; set ; }
[ApiMember(Name = "ReplaceAllMetadata", Description = "Determines if metadata should be replaced. Only applicable if mode is FullRefresh", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
public bool ReplaceAllMetadata { get ; set ; }
[ApiMember(Name = "ReplaceAllImages", Description = "Determines if images should be replaced. Only applicable if mode is FullRefresh", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "POST")]
2014-06-14 22:24:04 -04:00
public bool ReplaceAllImages { get ; set ; }
2014-02-04 15:19:29 -05:00
}
2013-06-27 15:29:58 -04:00
2014-11-14 21:31:03 -05:00
[Route("/Items/{Id}/Refresh", "POST", Summary = "Refreshes metadata for an item")]
2014-02-04 15:19:29 -05:00
public class RefreshItem : BaseRefreshRequest
{
2013-06-27 15:29:58 -04:00
[ApiMember(Name = "Recursive", Description = "Indicates if the refresh should occur recursively.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
public bool Recursive { get ; set ; }
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get ; set ; }
}
2014-07-02 14:34:08 -04:00
[Authenticated]
2013-06-27 15:29:58 -04:00
public class ItemRefreshService : BaseApiService
{
private readonly ILibraryManager _libraryManager ;
2014-04-26 23:42:05 -04:00
public ItemRefreshService ( ILibraryManager libraryManager )
2013-06-27 15:29:58 -04:00
{
_libraryManager = libraryManager ;
}
2014-04-26 23:42:05 -04:00
private async Task RefreshArtist ( RefreshItem request , MusicArtist item )
2013-06-27 15:46:34 -04:00
{
2013-09-05 15:00:50 -04:00
var cancellationToken = CancellationToken . None ;
2013-09-09 14:24:18 -04:00
var albums = _libraryManager . RootFolder
2015-01-25 01:34:50 -05:00
. GetRecursiveChildren ( )
2013-09-09 14:24:18 -04:00
. OfType < MusicAlbum > ( )
2015-03-13 13:25:28 -04:00
. Where ( i = > i . HasAnyArtist ( item . Name ) )
2013-09-09 14:24:18 -04:00
. ToList ( ) ;
var musicArtists = albums
. Select ( i = > i . Parent )
. OfType < MusicArtist > ( )
. ToList ( ) ;
2014-02-09 02:27:44 -05:00
var options = GetRefreshOptions ( request ) ;
var musicArtistRefreshTasks = musicArtists . Select ( i = > i . ValidateChildren ( new Progress < double > ( ) , cancellationToken , options , true ) ) ;
2013-09-09 14:24:18 -04:00
await Task . WhenAll ( musicArtistRefreshTasks ) . ConfigureAwait ( false ) ;
2013-06-27 15:46:34 -04:00
try
{
2014-02-09 02:27:44 -05:00
await item . RefreshMetadata ( options , CancellationToken . None ) . ConfigureAwait ( false ) ;
2013-06-27 15:46:34 -04:00
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error refreshing library" , ex ) ;
}
}
2013-06-27 15:29:58 -04:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post ( RefreshItem request )
{
2014-04-26 23:42:05 -04:00
var item = _libraryManager . GetItemById ( request . Id ) ;
var task = item is MusicArtist ? RefreshArtist ( request , ( MusicArtist ) item ) : RefreshItem ( request , item ) ;
2013-06-27 15:29:58 -04:00
Task . WaitAll ( task ) ;
}
2013-09-16 22:08:18 -04:00
2013-06-27 15:29:58 -04:00
/// <summary>
/// Refreshes the item.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task.</returns>
2014-04-26 23:42:05 -04:00
private async Task RefreshItem ( RefreshItem request , BaseItem item )
2013-06-27 15:29:58 -04:00
{
2014-02-09 02:27:44 -05:00
var options = GetRefreshOptions ( request ) ;
2014-07-03 22:22:57 -04:00
2013-06-27 15:29:58 -04:00
try
{
2014-02-09 02:27:44 -05:00
await item . RefreshMetadata ( options , CancellationToken . None ) . ConfigureAwait ( false ) ;
2013-06-27 15:29:58 -04:00
2013-11-21 15:48:26 -05:00
if ( item . IsFolder )
2013-06-27 15:29:58 -04:00
{
2013-06-28 20:49:44 -04:00
// Collection folders don't validate their children so we'll have to simulate that here
2013-11-21 15:48:26 -05:00
var collectionFolder = item as CollectionFolder ;
2013-06-28 20:49:44 -04:00
if ( collectionFolder ! = null )
{
await RefreshCollectionFolderChildren ( request , collectionFolder ) . ConfigureAwait ( false ) ;
}
else
{
2013-11-21 15:48:26 -05:00
var folder = ( Folder ) item ;
2014-02-09 02:27:44 -05:00
await folder . ValidateChildren ( new Progress < double > ( ) , CancellationToken . None , options , request . Recursive ) . ConfigureAwait ( false ) ;
2013-06-28 20:49:44 -04:00
}
2013-06-27 15:29:58 -04:00
}
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error refreshing library" , ex ) ;
}
}
2013-06-28 20:49:44 -04:00
/// <summary>
/// Refreshes the collection folder children.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="collectionFolder">The collection folder.</param>
/// <returns>Task.</returns>
private async Task RefreshCollectionFolderChildren ( RefreshItem request , CollectionFolder collectionFolder )
{
2014-02-04 15:19:29 -05:00
var options = GetRefreshOptions ( request ) ;
2013-06-28 20:49:44 -04:00
foreach ( var child in collectionFolder . Children . ToList ( ) )
{
2014-02-04 15:19:29 -05:00
await child . RefreshMetadata ( options , CancellationToken . None ) . ConfigureAwait ( false ) ;
2013-06-28 20:49:44 -04:00
2013-11-21 15:48:26 -05:00
if ( child . IsFolder )
2013-06-28 20:49:44 -04:00
{
2013-11-21 15:48:26 -05:00
var folder = ( Folder ) child ;
2014-02-09 02:27:44 -05:00
await folder . ValidateChildren ( new Progress < double > ( ) , CancellationToken . None , options , request . Recursive ) . ConfigureAwait ( false ) ;
2013-06-28 20:49:44 -04:00
}
}
}
2014-02-04 15:19:29 -05:00
private MetadataRefreshOptions GetRefreshOptions ( BaseRefreshRequest request )
{
2014-10-25 14:32:58 -04:00
return new MetadataRefreshOptions ( new DirectoryService ( ) )
2014-02-04 15:19:29 -05:00
{
2014-07-03 22:22:57 -04:00
MetadataRefreshMode = request . MetadataRefreshMode ,
ImageRefreshMode = request . ImageRefreshMode ,
ReplaceAllImages = request . ReplaceAllImages ,
2014-07-20 00:46:29 -04:00
ReplaceAllMetadata = request . ReplaceAllMetadata ,
ForceSave = true
2014-02-04 15:19:29 -05:00
} ;
}
2013-06-27 15:29:58 -04:00
}
}