protected void PurgeCloudflareCache(IPublishingStrategy strategy, PublishEventArgs <IContent> e)
        {
            var umbracoFlareConfigModel = _configurationService.LoadConfigurationFile();

            if (!umbracoFlareConfigModel.PurgeCacheOn)
            {
                return;
            }

            var urls          = new List <string>();
            var currentDomain = UmbracoFlareUrlHelper.GetCurrentDomain();

            foreach (var content in e.PublishedEntities)
            {
                if (content.GetValue <bool>(ApplicationConstants.UmbracoFlareBackendProperties.CloudflareDisabledOnPublishPropertyAlias))
                {
                    continue;
                }

                urls.AddRange(_umbracoFlareDomainService.GetUrlsForNode(content.Id, currentDomain));
            }

            var result = _cloudflareService.PurgePages(urls);

            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. Please check the logs to find out more.",
                                   EventMessageType.Warning));
        }
Esempio n. 2
0
        public StatusWithMessage PurgeStaticFiles([FromBody] PurgeStaticFilesRequestModel model)
        {
            if (!model.StaticFiles.HasAny())
            {
                return(new StatusWithMessage(false, "There were not static files selected to purge"));
            }

            var currentDomainIsValid = _umbracoFlareDomainService.GetAllowedCloudflareDomains().Count(x => x.Equals(model.CurrentDomain)) > 0;

            if (!currentDomainIsValid)
            {
                return(new StatusWithMessage(false, "The current domain is not valid, please check if the domain is a valid zone in your cloudflare account."));
            }

            var fullUrlsToPurge = new List <string>();
            var allFilePaths    = _cloudflareService.GetFilePaths(model.StaticFiles);

            foreach (var filePath in allFilePaths)
            {
                var extension = Path.GetExtension(filePath);

                if (ApplicationConstants.AllowedFileExtensions.Contains(extension))
                {
                    var urls = UmbracoFlareUrlHelper.GetFullUrlForPurgeStaticFiles(filePath, model.CurrentDomain, true);
                    fullUrlsToPurge.AddRange(urls);
                }
            }

            var result = _cloudflareService.PurgePages(fullUrlsToPurge);

            return(!result.Success ? result : new StatusWithMessage(true, $"{fullUrlsToPurge.Count()} static files were purged successfully."));
        }