private void MediaFileOnBeforeSave(object sender, ObjectEventArgs e)
        {
            if (e.Object == null)
            {
                return;
            }

            if (e.Object is MediaFileInfo image)
            {
                var optimizer = new ImageMagickImageOptimizer(SiteContext.CurrentSiteName);
                optimizer.Optimize(image);
            }
        }
        private void AttachmentOnBeforeSave(object sender, ObjectEventArgs e)
        {
            if (e.Object == null)
            {
                return;
            }

            // If workflow enabled
            if (e.Object is AttachmentHistoryInfo attachmentVersion)
            {
                var latestAttachmentVersion = AttachmentHistoryInfoProvider.GetAttachmentHistories()
                                              .WhereEquals("AttachmentGUID", attachmentVersion.AttachmentGUID)
                                              .OrderByDescending("AttachmentLastModified")
                                              .TopN(1)
                                              .FirstOrDefault();

                if (latestAttachmentVersion == null ||
                    latestAttachmentVersion.AttachmentSize != attachmentVersion.AttachmentSize)
                {
                    var optimizer = new ImageMagickImageOptimizer(SiteContext.CurrentSiteName);
                    optimizer.Optimize(attachmentVersion);
                }
            }

            // If workflow disabled
            if (e.Object is AttachmentInfo attachment)
            {
                var document = DocumentHelper.GetDocument(attachment.AttachmentDocumentID, new TreeProvider());

                if (document.WorkflowStep == null)
                {
                    var currentAttachment = AttachmentInfoProvider.GetAttachmentInfo(attachment.AttachmentID, true);

                    if (currentAttachment == null || currentAttachment.AttachmentSize != attachment.AttachmentSize)
                    {
                        var optimizer = new ImageMagickImageOptimizer(SiteContext.CurrentSiteName);
                        optimizer.Optimize(attachment);
                    }
                }
            }
        }