Beispiel #1
0
        /// <summary>
        /// To avoid completely re-scanning the whole media library after a change, we could simply update the current JSON storage
        /// with the items that were cleaned up by our module. But take in mind that external changes are still being ignored by this module.
        /// So for an accurate rendition of the media library data usage, a re-scan is necessary once in a while.
        /// </summary>
        /// <remarks>
        /// The backlog of this module contains an idea to create a pipeline processor for the onSave handler of all items,
        /// so the JSON storage could be updated continuously, never requiring a re-scan after the initial media library scan!
        /// </remarks>
        /// <param name="itemIDs">A list of Sitecore item ID strings of the items to update in the JSON storage.</param>
        /// <param name="actionType">An ActionType enum object to indicate whether to delete the items in the list, or to remove the old versions from them.</param>
        private void UpdateStorageAfterCleanUp(List <string> itemIDs, ActionType actionType)
        {
            MediaItemReport mediaItemRoot          = null;
            JsonStorage     mediaItemReportStorage = null;

            // read the media item report object from the JSON storage
            var mediaItemPath = Settings.GetSetting("Shrink.MediaItemReportPath");

            if (!string.IsNullOrEmpty(mediaItemPath))
            {
                mediaItemReportStorage = new JsonStorage(mediaItemPath);
                mediaItemRoot          = mediaItemReportStorage.Deserialize <MediaItemReport>();
            }

            if (mediaItemRoot != null)
            {
                // remove any children as supplied in the list of Sitecore item IDs
                this.RemoveChildren(mediaItemRoot, itemIDs, actionType);

                // write the updated JSON file to disk
                mediaItemReportStorage.Serialize(mediaItemRoot);

                // update the corresponding media library report JSON storage file with the updated info
                var libraryReportPath = Settings.GetSetting("Shrink.MediaLibraryReportPath");
                if (!string.IsNullOrEmpty(libraryReportPath))
                {
                    var json = new JsonStorage(libraryReportPath);
                    json.Serialize(new MediaLibraryReport(mediaItemRoot));
                }
            }
        }
        /// <summary>
        /// Returns a subset of the total media library based on the given category (see MediaConstants class for all categories)
        /// </summary>
        /// <param name="category">The category to get the items off.</param>
        /// <returns>A pipe separated list of Sitecore IDs representing all items that fall within the given category.</returns>
        public ActionResult SelectSubset(string category)
        {
            var subset = string.Empty;

            // reload the JSON report file to reconstruct the media item report
            var mediaItemPath = Settings.GetSetting("Shrink.MediaItemReportPath");

            if (!string.IsNullOrEmpty(mediaItemPath))
            {
                var json          = new JsonStorage(mediaItemPath);
                var mediaItemRoot = json.Deserialize <MediaItemReport>();
                var libraryReport = new MediaLibraryReport(mediaItemRoot);

                List <MediaItemReport> items = null;

                switch (category)
                {
                case MediaConstants.CategoryInUse:
                    items = libraryReport.ItemsByReferenceState(true);
                    break;

                case MediaConstants.CategoryNotReferenced:
                    items = libraryReport.ItemsByReferenceState(false);
                    break;

                case MediaConstants.CategoryReferencedUnknown:
                    items = libraryReport.ItemsReferencesUnknown();
                    break;

                case MediaConstants.CategoryPublished:
                    items = libraryReport.ItemsByPublishingState(true);
                    break;

                case MediaConstants.CategoryUnpublished:
                    items = libraryReport.ItemsByPublishingState(false);
                    break;

                case MediaConstants.CategoryPublishedUnknown:
                    items = libraryReport.ItemsPublishingStateUnknown();
                    break;

                case MediaConstants.CategoryItemsWithOldVersions:
                    items = libraryReport.ItemsByVersionState(true);
                    break;

                case MediaConstants.CategoryItemsUsingAllVersions:
                    items = libraryReport.ItemsByVersionState(false);
                    break;

                case MediaConstants.CategoryVersionsUnknown:
                    items = libraryReport.ItemsVersionsUnknown();
                    break;
                }

                if (items != null)
                {
                    subset = ItemHelper.ItemListToPipedString(items);
                }
            }

            return(Json(subset, JsonRequestBehavior.AllowGet));
        }