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.")); }
public StatusWithMessage PurgeStaticFiles([FromBody] PurgeStaticFilesRequestModel model) { List <string> allowedFileExtensions = new List <string>() { ".css", ".js", ".jpg", ".png", ".gif", ".aspx", ".html" }; string generalSuccessMessage = "Successfully purged the cache for the selected static files."; string generalErrorMessage = "Sorry, we could not purge the cache for the static files."; if (model.StaticFiles == null) { return(new StatusWithMessage(false, generalErrorMessage)); } if (!model.StaticFiles.Any()) { return(new StatusWithMessage(true, generalSuccessMessage)); } List <StatusWithMessage> errors; IEnumerable <string> allFilePaths = GetAllFilePaths(model.StaticFiles, out errors); //Save a list of each individual file if it errors so we can give detailed errors to the user. List <StatusWithMessage> results = new List <StatusWithMessage>(); List <string> fullUrlsToPurge = new List <string>(); //build the urls with the domain we are on now foreach (string filePath in allFilePaths) { string extension = Path.GetExtension(filePath); if (!allowedFileExtensions.Contains(extension)) { //results.Add(new StatusWithMessage(false, String.Format("You cannot purge the file {0} because its extension is not allowed.", filePath))); } else { fullUrlsToPurge.AddRange(UrlHelper.MakeFullUrlWithDomain(filePath, model.Hosts, true)); } } results.AddRange(CloudflareManager.Instance.PurgePages(fullUrlsToPurge)); if (results.Any(x => !x.Success)) { return(new StatusWithMessage(false, CloudflareManager.PrintResultsSummary(results))); } else { return(new StatusWithMessage(true, String.Format("{0} static files purged successfully.", results.Where(x => x.Success).Count()))); } }