/// <summary>
        /// Checks whether the <paramref name="action"/> has any aspects and if not
        /// tries to restore them from the media library.
        /// </summary>
        /// <param name="action">The action to check.</param>
        /// <returns>True if the aspects were present or successfully restored.</returns>
        protected async Task <bool> TryRestoreAspects(FanArtManagerAction action)
        {
            //Already has aspects, nothing to do
            if (action.Aspects != null)
            {
                return(true);
            }

            //No aspects, this resource was restored from disk, try and restore the aspects from the media library.
            //Throttle the number of concurrent queries To avoid a spike during startup.
            await _loadItemThrottle.WaitAsync(_cts.Token);

            try
            {
                IMediaLibrary  mediaLibrary = ServiceRegistration.Get <IMediaLibrary>();
                MediaItemQuery query        = new MediaItemQuery(null,
                                                                 mediaLibrary.GetManagedMediaItemAspectMetadata().Keys,
                                                                 new MediaItemIdFilter(action.MediaItemId));
                var items = mediaLibrary.Search(query, false, null, true);
                if (items != null && items.Count > 0)
                {
                    action.Aspects = items[0].Aspects;
                    return(true);
                }
                ServiceRegistration.Get <ILogger>().Warn("FanArtActionBlock: Unable to restore FanArtAction, media item with id {0} was not found in the media library", action.MediaItemId);
                return(false);
            }
            finally
            {
                _loadItemThrottle.Release();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Checks whether the <paramref name="actions"/> has any aspects and if not
        /// tries to load them from the media library.
        /// </summary>
        /// <param name="actions">The action to check.</param>
        /// <returns>True if the aspects were present or successfully loaded.</returns>
        protected async Task LoadAspects(FanArtManagerAction[] actions)
        {
            var mediaItemIds = actions.Where(a => a.Type == ActionType.Collect && (a.Aspects == null || a.Aspects.Count == 0))
                               .Select(a => a.MediaItemId).Distinct();

            var queryFilter = new MediaItemIdFilter(mediaItemIds);

            if (queryFilter.MediaItemIds.Count == 0)
            {
                return;
            }

            IMediaLibrary mediaLibrary = ServiceRegistration.Get <IMediaLibrary>();

            MediaItemQuery query = new MediaItemQuery(null,
                                                      mediaLibrary.GetManagedMediaItemAspectMetadata().Keys,
                                                      queryFilter);

            // Throttle the number of concurrent queries To avoid spikes when loading aspects.
            await _loadItemThrottle.WaitAsync(_cts.Token);

            try
            {
                var items = mediaLibrary.Search(query, false, null, true);
                if (items != null && items.Count > 0)
                {
                    foreach (var item in items)
                    {
                        foreach (var action in actions)
                        {
                            if (action.MediaItemId == item.MediaItemId)
                            {
                                action.Aspects = item.Aspects;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    ServiceRegistration.Get <ILogger>().Warn("FanArtActionBlock: Unable to load aspects, no matching media items were found in the media library");
                }
            }
            catch (Exception ex)
            {
                ServiceRegistration.Get <ILogger>().Error("FanArtActionBlock: Error loading aspects from the media library", ex);
            }
            finally
            {
                _loadItemThrottle.Release();
            }
        }