/// <summary> /// Requires the following field to be patched in: <field fieldName="_templates" returnType="string" type="Sitecore.ContentSearch.ComputedFields.AllTemplates, Sitecore.ContentSearch" deep="true" includeStandardTemplate="false" /> /// </summary> /// <param name="index">The search index to search within.</param> /// <returns></returns> protected IEnumerable <Item> GetUnprocessedImages(string marker) { using (var searchContext = ContentSearchManager.GetIndex(ImageCompressionConstants.GlobalSettings.Index.Master).CreateSearchContext()) { var imageResults = searchContext.GetQueryable <AllTemplatesSearchResultItem>().Where(x => x.TemplateId == ImageCompressionConstants.TemplateIDs.UnversionedImageTemplateId || x.TemplateId == (ImageCompressionConstants.TemplateIDs.UnversionedJpegImageTemplateId) || x.TemplateId == (ImageCompressionConstants.TemplateIDs.VersionedImageTemplateId) || x.TemplateId == (ImageCompressionConstants.TemplateIDs.VersionedJpegImageTemplateId) ); if (!imageResults.Any()) { return(new List <Item>()); } var sitecoreItems = imageResults.Select(x => x.GetItem()).ToList() .Where(y => !y.Fields[ImageCompressionSettings.GetInformationField()].Value.Contains(marker)); var results = sitecoreItems.ToList(); Diagnostics.Log.Info($"Found {results.Count} images", this); return(results); } }
public static void RecordError(MediaItem currentItem, string message) { currentItem.InnerItem.Editing.BeginEdit(); currentItem.InnerItem.Fields[ImageCompressionSettings.GetInformationField()].Value = message; currentItem.InnerItem.Editing.EndEdit(); }
public virtual string DownloadImage(MediaItem currentItem, ImageUpload img) { var client = new RestClient(img.Location); client.Authenticator = new HttpBasicAuthenticator("Api", ImageCompressionSettings.GetApiEndpointKey()); var request = new RestRequest(Method.GET); request.AddHeader("Cache-Control", "no-cache"); request.AddHeader("Accept", "*/*"); client.Timeout = 300000; try { byte [] responseData = client.DownloadData(request); string sizeBefore = currentItem.InnerItem.Fields["Size"].Value; UpdateImageFile(currentItem, responseData); string sizeAfter = currentItem.InnerItem.Fields["Size"].Value; UpdateImageInformation(currentItem, sizeBefore, sizeAfter, ImageCompressionConstants.Messages.OPTIMISED_BY, ImageCompressionSettings.GetInformationField()); } catch (Exception ex) { Diagnostics.Log.Error(TIMY_CONNETION_ERROR, ex); RecordError(currentItem, ex.Message); } return("API ISSUE"); }