Exemple #1
0
        private S3DownloadedFile DownloadFileFromS3(string providerName, string libraryName, string fileName)
        {
            LibrariesManager librariesManager = LibrariesManager.GetManager(providerName);
            var docLibs = librariesManager.GetDocumentLibraries();

            MediaContent document   = null;
            Stream       tempStream = null;
            MemoryStream fileStream = new MemoryStream();

            foreach (var lib in docLibs)
            {
                if (lib.Title.ToLower() == libraryName)
                {
                    document   = lib.Items().Where(item => item.Title == fileName).FirstOrDefault();
                    tempStream = librariesManager.Download(document);
                }
            }

            if (tempStream != null)
            {
                tempStream.CopyTo(fileStream);
                fileStream.Position = 0;
            }

            return(new S3DownloadedFile {
                Document = document, FileStream = fileStream
            });
        }
Exemple #2
0
        private bool ProcessImagesInternal(string providerName)
        {
            string           transactionName  = string.Format("imageoptimization_{0}", providerName);
            LibrariesManager librariesManager = LibrariesManager.GetManager(providerName, transactionName);
            bool             itemsProcessed   = false;
            int processedImages = 0;

            IEnumerable <Image> images = librariesManager.GetImages().Where(i => i.Status == ContentLifecycleStatus.Master && ((i.GetValue <object>(ImageOptimizationConstants.IsOptimizedFieldName) == null) || !i.GetValue <bool>(ImageOptimizationConstants.IsOptimizedFieldName))).Take(this.batchSize);

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

                    Image master = image;
                    Image temp   = librariesManager.Lifecycle.CheckOut(image) as Image;

                    Stream sourceImageStream = librariesManager.Download(image.Id);
                    librariesManager.Upload(temp, sourceImageStream, image.Extension, true);

                    temp.SetValue(ImageOptimizationConstants.IsOptimizedFieldName, true);

                    master = librariesManager.Lifecycle.CheckIn(temp) as Image;

                    ProcessReplacedImageTranslations(librariesManager, master);

                    if (master.ApprovalWorkflowState == "Published")
                    {
                        librariesManager.Lifecycle.Publish(master);
                    }

                    this.BuildTrace(string.Format("{0} - Image {1} ({2}) has been optimized", DateTime.UtcNow.ToString("yyyy-MM-dd-T-HH:mm:ss"), image.Title, image.Id));

                    if (processedImages % 5 == 0)
                    {
                        TransactionManager.CommitTransaction(transactionName);
                    }

                    processedImages += 1;
                }
                catch (Exception ex)
                {
                    this.BuildTrace(string.Format("Optimization of image {0} ({1}) failed with exception {2}", image.Title, image.Id, ex.Message), true);
                }

                this.WriteTraceLog();

                itemsProcessed = true;
            }

            TransactionManager.CommitTransaction(transactionName);

            return(itemsProcessed);
        }
Exemple #3
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);
                        }
                    }
                }
            }
        }