2013-09-04 17:02:19 +00:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2013-06-27 19:29:58 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-09-05 19:00:50 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2013-06-27 19:29:58 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-12-07 15:52:38 +00:00
|
|
|
|
using ServiceStack;
|
2013-06-27 19:29:58 +00:00
|
|
|
|
using System;
|
2013-09-04 17:02:19 +00:00
|
|
|
|
using System.Linq;
|
2013-06-27 19:29:58 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
|
|
|
|
[Route("/Items/{Id}/Refresh", "POST")]
|
|
|
|
|
[Api(Description = "Refreshes metadata for an item")]
|
|
|
|
|
public class RefreshItem : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Forced", Description = "Indicates if a normal or forced refresh should occur.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
|
|
|
|
|
public bool Forced { get; set; }
|
|
|
|
|
|
|
|
|
|
[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; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:46:34 +00:00
|
|
|
|
[Route("/Artists/{Name}/Refresh", "POST")]
|
|
|
|
|
[Api(Description = "Refreshes metadata for an artist")]
|
|
|
|
|
public class RefreshArtist : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Forced", Description = "Indicates if a normal or forced refresh should occur.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
|
|
|
|
|
public bool Forced { get; set; }
|
|
|
|
|
|
|
|
|
|
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("/Genres/{Name}/Refresh", "POST")]
|
|
|
|
|
[Api(Description = "Refreshes metadata for a genre")]
|
|
|
|
|
public class RefreshGenre : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Forced", Description = "Indicates if a normal or forced refresh should occur.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
|
|
|
|
|
public bool Forced { get; set; }
|
|
|
|
|
|
|
|
|
|
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("/MusicGenres/{Name}/Refresh", "POST")]
|
|
|
|
|
[Api(Description = "Refreshes metadata for a music genre")]
|
|
|
|
|
public class RefreshMusicGenre : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Forced", Description = "Indicates if a normal or forced refresh should occur.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
|
|
|
|
|
public bool Forced { get; set; }
|
|
|
|
|
|
|
|
|
|
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-01 17:17:33 +00:00
|
|
|
|
[Route("/GameGenres/{Name}/Refresh", "POST")]
|
|
|
|
|
[Api(Description = "Refreshes metadata for a game genre")]
|
|
|
|
|
public class RefreshGameGenre : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Forced", Description = "Indicates if a normal or forced refresh should occur.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
|
|
|
|
|
public bool Forced { get; set; }
|
|
|
|
|
|
|
|
|
|
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:46:34 +00:00
|
|
|
|
[Route("/Persons/{Name}/Refresh", "POST")]
|
|
|
|
|
[Api(Description = "Refreshes metadata for a person")]
|
|
|
|
|
public class RefreshPerson : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Forced", Description = "Indicates if a normal or forced refresh should occur.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
|
|
|
|
|
public bool Forced { get; set; }
|
|
|
|
|
|
|
|
|
|
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("/Studios/{Name}/Refresh", "POST")]
|
|
|
|
|
[Api(Description = "Refreshes metadata for a studio")]
|
|
|
|
|
public class RefreshStudio : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Forced", Description = "Indicates if a normal or forced refresh should occur.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
|
|
|
|
|
public bool Forced { get; set; }
|
|
|
|
|
|
|
|
|
|
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
2013-09-17 02:08:18 +00:00
|
|
|
|
|
2013-06-27 19:29:58 +00:00
|
|
|
|
public class ItemRefreshService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-09-04 17:02:19 +00:00
|
|
|
|
private readonly IDtoService _dtoService;
|
2013-06-27 19:29:58 +00:00
|
|
|
|
|
2013-09-04 17:02:19 +00:00
|
|
|
|
public ItemRefreshService(ILibraryManager libraryManager, IDtoService dtoService)
|
2013-06-27 19:29:58 +00:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
2013-09-04 17:02:19 +00:00
|
|
|
|
_dtoService = dtoService;
|
2013-06-27 19:29:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:46:34 +00:00
|
|
|
|
public void Post(RefreshArtist request)
|
|
|
|
|
{
|
|
|
|
|
var task = RefreshArtist(request);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RefreshArtist(RefreshArtist request)
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var item = GetArtist(request.Name, _libraryManager);
|
2013-06-27 19:46:34 +00:00
|
|
|
|
|
2013-09-05 19:00:50 +00:00
|
|
|
|
var cancellationToken = CancellationToken.None;
|
|
|
|
|
|
2013-09-09 18:24:18 +00:00
|
|
|
|
var albums = _libraryManager.RootFolder
|
|
|
|
|
.RecursiveChildren
|
|
|
|
|
.OfType<MusicAlbum>()
|
|
|
|
|
.Where(i => i.HasArtist(item.Name))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var musicArtists = albums
|
|
|
|
|
.Select(i => i.Parent)
|
|
|
|
|
.OfType<MusicArtist>()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var musicArtistRefreshTasks = musicArtists.Select(i => i.ValidateChildren(new Progress<double>(), cancellationToken, true, request.Forced));
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(musicArtistRefreshTasks).ConfigureAwait(false);
|
|
|
|
|
|
2013-06-27 19:46:34 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-05 19:00:50 +00:00
|
|
|
|
await item.RefreshMetadata(cancellationToken, forceRefresh: request.Forced).ConfigureAwait(false);
|
2013-06-27 19:46:34 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error refreshing library", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Post(RefreshGenre request)
|
|
|
|
|
{
|
|
|
|
|
var task = RefreshGenre(request);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RefreshGenre(RefreshGenre request)
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var item = GetGenre(request.Name, _libraryManager);
|
2013-06-27 19:46:34 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await item.RefreshMetadata(CancellationToken.None, forceRefresh: request.Forced).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error refreshing library", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Post(RefreshMusicGenre request)
|
|
|
|
|
{
|
|
|
|
|
var task = RefreshMusicGenre(request);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RefreshMusicGenre(RefreshMusicGenre request)
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var item = GetMusicGenre(request.Name, _libraryManager);
|
2013-06-27 19:46:34 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-07-01 17:17:33 +00:00
|
|
|
|
await item.RefreshMetadata(CancellationToken.None, forceRefresh: request.Forced).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error refreshing library", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Post(RefreshGameGenre request)
|
|
|
|
|
{
|
|
|
|
|
var task = RefreshGameGenre(request);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RefreshGameGenre(RefreshGameGenre request)
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var item = GetGameGenre(request.Name, _libraryManager);
|
2013-07-01 17:17:33 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-06-27 19:46:34 +00:00
|
|
|
|
await item.RefreshMetadata(CancellationToken.None, forceRefresh: request.Forced).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error refreshing library", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Post(RefreshPerson request)
|
|
|
|
|
{
|
|
|
|
|
var task = RefreshPerson(request);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RefreshPerson(RefreshPerson request)
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var item = GetPerson(request.Name, _libraryManager);
|
2013-06-27 19:46:34 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await item.RefreshMetadata(CancellationToken.None, forceRefresh: request.Forced).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error refreshing library", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Post(RefreshStudio request)
|
|
|
|
|
{
|
|
|
|
|
var task = RefreshStudio(request);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RefreshStudio(RefreshStudio request)
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var item = GetStudio(request.Name, _libraryManager);
|
2013-06-27 19:46:34 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await item.RefreshMetadata(CancellationToken.None, forceRefresh: request.Forced).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error refreshing library", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:29:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
public void Post(RefreshItem request)
|
|
|
|
|
{
|
|
|
|
|
var task = RefreshItem(request);
|
|
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
}
|
2013-09-17 02:08:18 +00:00
|
|
|
|
|
2013-06-27 19:29:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refreshes the item.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private async Task RefreshItem(RefreshItem request)
|
|
|
|
|
{
|
2013-09-04 17:02:19 +00:00
|
|
|
|
var item = _dtoService.GetItemByDtoId(request.Id);
|
2013-06-27 19:29:58 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await item.RefreshMetadata(CancellationToken.None, forceRefresh: request.Forced).ConfigureAwait(false);
|
|
|
|
|
|
2013-11-21 20:48:26 +00:00
|
|
|
|
if (item.IsFolder)
|
2013-06-27 19:29:58 +00:00
|
|
|
|
{
|
2013-06-29 00:49:44 +00:00
|
|
|
|
// Collection folders don't validate their children so we'll have to simulate that here
|
2013-11-21 20:48:26 +00:00
|
|
|
|
var collectionFolder = item as CollectionFolder;
|
2013-06-29 00:49:44 +00:00
|
|
|
|
|
|
|
|
|
if (collectionFolder != null)
|
|
|
|
|
{
|
|
|
|
|
await RefreshCollectionFolderChildren(request, collectionFolder).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-11-21 20:48:26 +00:00
|
|
|
|
var folder = (Folder)item;
|
|
|
|
|
|
2013-06-29 00:49:44 +00:00
|
|
|
|
await folder.ValidateChildren(new Progress<double>(), CancellationToken.None, request.Recursive, request.Forced).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-06-27 19:29:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error refreshing library", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-29 00:49:44 +00: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)
|
|
|
|
|
{
|
|
|
|
|
foreach (var child in collectionFolder.Children.ToList())
|
|
|
|
|
{
|
|
|
|
|
await child.RefreshMetadata(CancellationToken.None, forceRefresh: request.Forced).ConfigureAwait(false);
|
|
|
|
|
|
2013-11-21 20:48:26 +00:00
|
|
|
|
if (child.IsFolder)
|
2013-06-29 00:49:44 +00:00
|
|
|
|
{
|
2013-11-21 20:48:26 +00:00
|
|
|
|
var folder = (Folder)child;
|
|
|
|
|
|
2013-06-29 00:49:44 +00:00
|
|
|
|
await folder.ValidateChildren(new Progress<double>(), CancellationToken.None, request.Recursive, request.Forced).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-27 19:29:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|