Exemple #1
0
        private void ProcessReplacedImageTranslations(LibrariesManager librariesManager, Image image)
        {
            Guid defaultFileId = image.FileId;
            IEnumerable <MediaFileLink> links = image.MediaFileLinks.Where(mfl => mfl.FileId != defaultFileId);

            // process image translations that have replaced image for translation
            foreach (var linkItem in links)
            {
                IList <int> proccessedCultures = new List <int>();

                if (!proccessedCultures.Contains(linkItem.Culture))
                {
                    using (new CultureRegion(linkItem.Culture))
                    {
                        CultureInfo currentCultureInfo = new CultureInfo(linkItem.Culture);

                        try
                        {
                            this.BuildTrace(string.Format("{0} - Attempting to optimize image {1} ({2}) in culture {3}", DateTime.UtcNow.ToString("yyyy-MM-dd-T-HH:mm:ss"), image.Title, image.Id, currentCultureInfo.Name));

                            Image translatedMaster = (linkItem.MediaContent.OriginalContentId == Guid.Empty ? linkItem.MediaContent : librariesManager.GetMediaItem(linkItem.MediaContent.OriginalContentId)) as Image;

                            Image translatedTemp = librariesManager.Lifecycle.CheckOut(translatedMaster) as Image;

                            Stream translationSourceImageStream = librariesManager.Download(linkItem.MediaContentId);
                            librariesManager.Upload(translatedTemp, translationSourceImageStream, image.Extension, false);
                            translatedTemp.SetValue(ImageOptimizationConstants.IsOptimizedFieldName, true);

                            translatedMaster = librariesManager.Lifecycle.CheckIn(translatedTemp) as Image;

                            if (translatedMaster.ApprovalWorkflowState.GetString(currentCultureInfo, false) == "Published")
                            {
                                librariesManager.Lifecycle.Publish(translatedMaster, currentCultureInfo);
                            }

                            proccessedCultures.Add(linkItem.Culture);

                            this.BuildTrace(string.Format("{0} - Image {1} ({2}) in culture {3} has been optimized", DateTime.UtcNow.ToString("yyyy-MM-dd-T-HH:mm:ss"), image.Title, image.Id, currentCultureInfo.Name));
                        }
                        catch (Exception ex)
                        {
                            this.BuildTrace(string.Format("Optimization of image {0} ({1}) in culture {2} failed with exception {3}", image.Title, image.Id, currentCultureInfo.Name, ex.Message), true);
                        }
                    }
                }
            }
        }