示例#1
0
        /// <summary>
        /// Gets a model for the media with the specified identifier.
        /// </summary>
        /// <typeparam name="T">The media model type.</typeparam>
        /// <param name="id">The media identifier.</param>
        /// <returns>A model for the media with the specified identifier.</returns>
        /// <exception cref="ArgumentException">Thrown if <typeparamref name="T" /> is not a media model type.</exception>
        public T GetMedia <T>(int id)
            where T : class, new()
        {
            if (!typeof(T).IsModelType <MediaTypeAttribute>())
            {
                throw new ArgumentException($"Type {typeof(T)} is not a media model type.");
            }

            return(GetMedia <T>(_umbracoHelperWrapper.TypedMedia(id)));
        }
        protected void PurgeCloudflareCacheForMedia(IMediaService sender, SaveEventArgs <IMedia> e)
        {
            var umbracoFlareConfigModel = _configurationService.LoadConfigurationFile();

            if (!umbracoFlareConfigModel.PurgeCacheOn)
            {
                return;
            }

            var imageCropSizes = _imageCropperService.GetAllCrops().ToList();
            var urls           = new List <string>();

            var currentDomain = UmbracoFlareUrlHelper.GetCurrentDomain();

            foreach (var media in e.SavedEntities)
            {
                if (media.IsNewEntity() || media.GetValue <bool>(ApplicationConstants.UmbracoFlareBackendProperties.CloudflareDisabledOnPublishPropertyAlias))
                {
                    continue;
                }

                var publishedMedia = _umbracoHelperWrapper.TypedMedia(media.Id);

                if (publishedMedia == null)
                {
                    e.Messages.Add(new EventMessage("Cloudflare Caching", "We could not find the IPublishedContent version of the media: " + media.Id + " you are trying to save.", EventMessageType.Error));
                    continue;
                }

                urls.AddRange(imageCropSizes.Select(crop => publishedMedia.GetCropUrl(crop.Alias)));
                urls.Add(publishedMedia.Url);
            }

            var fullUrls = UmbracoFlareUrlHelper.MakeFullUrlsWithDomain(urls, currentDomain, true);
            var result   = _cloudflareService.PurgePages(fullUrls);

            e.Messages.Add(result.Success
                ? new EventMessage(ApplicationConstants.EventMessageCategory.CloudflareCaching,
                                   "Successfully purged the cloudflare cache.", EventMessageType.Success)
                : new EventMessage(ApplicationConstants.EventMessageCategory.CloudflareCaching,
                                   "We could not purge the Cloudflare cache.", EventMessageType.Warning));
        }