/// <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();
            }
        }
 /// <summary>
 /// Schedules a new <see cref="FanArtManagerAction"/> for processing.
 /// </summary>
 /// <param name="action">The action to process.</param>
 /// <returns>True if the action was scheduled.</returns>
 public bool Post(FanArtManagerAction action)
 {
     _pendingFanArtDownloads.TryAdd(action.ActionId, action);
     //Persist the newly pending action
     _persistBlock.Post(null);
     //Post the action for processing
     return(_innerBlock.Post(action));
 }
Example #3
0
 /// <summary>
 /// Schedules a new <see cref="FanArtManagerAction"/> for processing.
 /// </summary>
 /// <param name="action">The action to process.</param>
 /// <returns>True if the action was scheduled.</returns>
 public bool Post(FanArtManagerAction action)
 {
     if (AddPendingAction(action.ActionId, action.Type, action.MediaItemId))
     {
         // Post the action for processing
         return(_inputBlock.Post(action));
     }
     //We return success because the action is already scheduled
     return(true);
 }
        /// <summary>
        /// Removes the <paramref name="action"/> from the list of pending actions
        /// so it is not restored on next server startup.
        /// </summary>
        /// <param name="action"></param>
        private void OutputBlockMethod(FanArtManagerAction action)
        {
            FanArtManagerAction removedAction;

            if (_pendingFanArtDownloads.TryRemove(action.ActionId, out removedAction))
            {
                removedAction.Aspects?.Clear();
                //Remove the completed action from the persisted list of pending actions
                _persistBlock.Post(null);
            }
        }
 /// <summary>
 /// Processes the <paramref name="action"/>.
 /// </summary>
 /// <param name="action">The action to process.</param>
 /// <returns>Task that completes when the action has been processed.</returns>
 private async Task <FanArtManagerAction> InnerBlockMethod(FanArtManagerAction action)
 {
     if (action.Type == ActionType.Collect)
     {
         //This action might have been restored in which case we need to reload the aspects
         if (await TryRestoreAspects(action))
         {
             await CollectFanArt(action.MediaItemId, action.Aspects);
         }
     }
     else if (action.Type == ActionType.Delete)
     {
         await DeleteFanArt(action.MediaItemId);
     }
     return(action);
 }
 /// <summary>
 /// Schedules a new <see cref="FanArtManagerAction"/> for processing.
 /// </summary>
 /// <param name="action">The action to process.</param>
 /// <returns>True if the action was scheduled.</returns>
 public bool Post(FanArtManagerAction action)
 {
     AddPendingAction(action.ActionId, action.Type, action.MediaItemId);
     // Post the action for processing
     return(_inputBlock.Post(action));
 }