public static void MediaFile_Insert_Before(object sender, ObjectEventArgs e) { MediaOptimizer optimizer = new MediaOptimizer(); if (e.Object != null) { if (e.Object is MediaFileInfo) { var image = (MediaFileInfo)e.Object; if (image != null) { image.FileTitle = image.FileName.ToUpper(); IOptimizerResult result = null; result = optimizer.Process(image); if (result != null && result.OptimizedBytes != null && result.OptimizedBytes.Length > 0) { image.FileBinary = result.OptimizedBytes; image.FileSize = result.OptimizedBytes.Length; } } return; } if (e.Object is AttachmentInfo) { var image = (AttachmentInfo)e.Object; if (image != null) { image.AttachmentTitle = image.AttachmentName.ToUpper(); IOptimizerResult result = null; result = optimizer.Process(image); if (result != null && result.OptimizedBytes != null && result.OptimizedBytes.Length > 0) { image.AttachmentBinary = result.OptimizedBytes; image.AttachmentSize = result.OptimizedBytes.Length; } } return; } } }
public static void MediaFile_Insert_Before(object sender, ObjectEventArgs e) { MediaOptimizer optimizer = new MediaOptimizer(); if (e.Object != null) { if (e.Object is MediaFileInfo) { var image = (MediaFileInfo) e.Object; if (image != null) { image.FileTitle = image.FileName.ToUpper(); IOptimizerResult result = null; result = optimizer.Process(image); if (result != null && result.OptimizedBytes != null && result.OptimizedBytes.Length > 0) { image.FileBinary = result.OptimizedBytes; image.FileSize = result.OptimizedBytes.Length; } } return; } if (e.Object is AttachmentInfo) { var image = (AttachmentInfo)e.Object; if (image != null) { image.AttachmentTitle = image.AttachmentName.ToUpper(); IOptimizerResult result = null; result = optimizer.Process(image); if (result != null && result.OptimizedBytes != null && result.OptimizedBytes.Length > 0) { image.AttachmentBinary = result.OptimizedBytes; image.AttachmentSize = result.OptimizedBytes.Length; } } return; } } }
protected virtual void OnAfterPersist(MediaCacheRecord record, SiteContext originalSiteContext) { string mediaPath = string.Empty; try { RemoveFromActiveList(record); // Housekeeping: since we call AddStream() to insert the optimized version, we have to keep AddStream() from calling OnAfterPersist() from that call, causing an optimization loop var id = record.Media.MediaData.MediaId; if (StreamsInOptimization.Contains(id)) { lock (OptimizeLock) { if (StreamsInOptimization.Contains(id)) { StreamsInOptimization.Remove(id); return; } } } lock (OptimizeLock) { StreamsInOptimization.Add(id); } // grab the stream from cache and optimize it if (!record.HasStream) { return; } var stream = record.GetStream(); if (!_optimizer.CanOptimize(stream)) { return; } var optimizedStream = _optimizer.Process(stream, record.Options); mediaPath = stream.MediaItem.MediaPath; if (optimizedStream == null) { Log.Info("Dianoga: async optimazation result was null, not doing any optimizing for {0}".FormatWith(mediaPath), this); return; } // we have to switch the site context because we're on a background thread, and Sitecore.Context is lost (so the site is always null) // if the site is null the media cache ignores all added entries using (new SiteContextSwitcher(originalSiteContext)) { for (int i = 0; i < 5; i++) { try { // only here to satisfy out param MediaStream dgafStream; bool success = AddStream(record.Media, record.Options, optimizedStream, out dgafStream); if (dgafStream != null) { dgafStream.Dispose(); } if (!success) { Log.Warn("Dianoga: The media cache rejected adding {0}. This is unexpected!".FormatWith(mediaPath), this); } } catch (Exception ex) { Log.Error("Dianoga: Error occurred adding stream to media cache. Will retry in 10 sec.", ex, this); Thread.Sleep(10000); continue; } break; } } } catch (Exception ex) { // this runs in a background thread, and an exception here would cause IIS to terminate the app pool. Bad! So we catch/log, just in case. Log.Error("Dianoga: Exception occurred on the background thread when optimizing: {0}".FormatWith(mediaPath), ex, this); } }