示例#1
0
        private string MakePublic()
        {
            // Reset Variants
            ImageVariants = new List <ImageVariant>(); // zFWx@-djeG6

            var delay = 0;

            while (PreviewImage == null && delay < 13)
            {
                // Implement waiting function when image should be generated first
                Thread.Sleep(250);
                delay++;
            }

            if (PreviewImage != null)
            {
                // Save image to temporary location
                foreach (ImageOutputConfiguration output in Config.OutputConfiguration.ImageOutputConfigurations)
                {
                    BitmapSource image = PreviewImage;
                    if (image.PixelWidth != output.Width || image.PixelHeight != output.Height)
                    {
                        // Resize original first for this output
                        image = ResizeBitmapImage(PreviewImage, output.Width, output.Height);
                    }

                    var imagePath = "";

                    if (output.Encoder == "PNG")
                    {
                        BitmapEncoder encoder = new PngBitmapEncoder();

                        imagePath = Path.Combine(Config.SlidesConfiguration.TemplatePath, Guid.NewGuid() + ".png");

                        encoder.Frames.Add(BitmapFrame.Create(image));
                        using (var filestream = new FileStream(imagePath, FileMode.Create))
                        {
                            encoder.Save(filestream);
                        }
                    }
                    if (output.Encoder == "JPEG")
                    {
                        //encoder = new JpegBitmapEncoder();
                        //((JpegBitmapEncoder)encoder).QualityLevel = output.Quality;
                        //extention = ".jpg";
                        // Compress IMAGE
                        var encoder = GetEncoder(ImageFormat.Jpeg);

                        // Create an Encoder object based on the GUID
                        // for the Quality parameter category.
                        Encoder myEncoder  = Encoder.Compression;
                        Encoder myEncoder2 = Encoder.Quality;


                        // Create an EncoderParameters object.
                        // An EncoderParameters object has an array of EncoderParameter
                        // objects. In this case, there is only one
                        // EncoderParameter object in the array.
                        var myEncoderParameters = new EncoderParameters(2);

                        var myEncoderParameter  = new EncoderParameter(myEncoder, 0L);
                        var myEncoderParameter2 = new EncoderParameter(myEncoder2, output.Quality);
                        myEncoderParameters.Param[0] = myEncoderParameter;
                        myEncoderParameters.Param[1] = myEncoderParameter2;

                        imagePath = Path.Combine(Config.SlidesConfiguration.TemplatePath, Guid.NewGuid().ToString() + ".jpg");

                        using (var outStream = new MemoryStream())
                        {
                            BitmapEncoder enc = new BmpBitmapEncoder();
                            enc.Frames.Add(BitmapFrame.Create(image));
                            enc.Save(outStream);
                            var bitmap = new Bitmap(outStream);

                            bitmap.Save(imagePath, encoder, myEncoderParameters);
                        }
                    }

                    // Upload Image to Amazon S3 and other Destination Uploads
                    var publicUrl = "";
                    foreach (UploadConfiguration outConf in Config.OutputConfiguration.UploadConfigurations)
                    {
                        try
                        {
                            var subFolder = outConf.Subfolder;
                            if (!output.IsDefault)
                            {
                                // Since not default, push to variant folder
                                subFolder = $"{subFolder}/{output.Name.ToLower()}";
                            }
                            switch (outConf.Type.ToUpper())
                            {
                            case "S3":
                            {
                                publicUrl = AWSS3Uploader.Upload(imagePath, outConf.AWSAccessKey,
                                                                 outConf.AWSSecretKey, outConf.S3BucketName, subFolder,
                                                                 outConf.PublicUriBase);

                                if (!string.IsNullOrEmpty(outConf.LatestStaticImageName))
                                {
                                    // We need a static image as well
                                    try
                                    {
                                        var latestStaticUrl = AWSS3Uploader.Upload(imagePath, outConf.AWSAccessKey,
                                                                                   outConf.AWSSecretKey, outConf.S3BucketName, subFolder,
                                                                                   outConf.PublicUriBase, outConf.LatestStaticImageName);
                                    }
                                    catch (Exception ex)
                                    {
                                        using (EventLog eventLog = new EventLog("Application"))
                                        {
                                            eventLog.Source = "Application";
                                            eventLog.WriteEntry($"EIS Content Manager failed to publish the image to AWS S3 static url.\n{ex.Message}\n\n{ex.StackTrace}", EventLogEntryType.Error, 101, 1);
                                        }
                                    }
                                }
                            }
                            break;

                            case "FILE":
                            {
                                publicUrl = SystemFileUploader.Upload(imagePath, outConf.DestinationPath,
                                                                      outConf.PublicUriBase);
                            }
                            break;

                            case "FTP":
                            {
                                publicUrl = FtpFileUploader.Upload(imagePath, outConf.FtpServer,
                                                                   outConf.FtpUsername, outConf.FtpPassword, subFolder,
                                                                   outConf.UniqueFilename, outConf.PublicUriBase);
                            }
                            break;

                            default:
                            {
                                using (EventLog eventLog = new EventLog("Application"))
                                {
                                    eventLog.Source = "Application";
                                    eventLog.WriteEntry($"EIS Content Manager failed to publish the image to an unknown destination type {outConf.Type}.", EventLogEntryType.Warning, 101, 1);
                                }
                            }
                            break;
                            }
                        }
                        catch (Exception e)
                        {
                            // Catch all exception to avoid crash and continue operations
                            using (EventLog eventLog = new EventLog("Application"))
                            {
                                eventLog.Source = "Application";
                                eventLog.WriteEntry($"EIS Content Manager failed to publish the image with a general error.\n{e.Message}\n\n{e.StackTrace}", EventLogEntryType.Error, 101, 1);
                            }
                        }
                    }

                    // TODO Handle multiple Output PublicUrls, last one wins
                    // Add to variants
                    ImageVariants.Add(new ImageVariant()
                    {
                        Name = output.Name, Url = publicUrl
                    });

                    // Save the Url of the IsDefault Image
                    if (output.IsDefault)
                    {
                        PublicImageUrl = publicUrl;
                    }

                    // Delete temporary Image
                    File.Delete(imagePath);
                }
            }
            if (string.IsNullOrEmpty(_publicImageUrl))
            {
                // TODO No default thus choose one
            }

            return(_publicImageUrl);
        }
        private string MakePublic()
        {
            // Reset Variants
            ImageVariants = new List <ImageVariant>(); // zFWx@-djeG6

            if (PreviewImage != null)
            {
                // Save image to temporary location
                foreach (ImageOutputConfiguration output in Config.OutputConfiguration.ImageOutputConfigurations)
                {
                    var imagePath = "";

                    if (output.Encoder == "PNG")
                    {
                        BitmapEncoder encoder = new PngBitmapEncoder();

                        imagePath = Path.Combine(Config.SlidesConfiguration.TemplatePath, Guid.NewGuid() + ".png");

                        encoder.Frames.Add(BitmapFrame.Create(PreviewImage));
                        using (var filestream = new FileStream(imagePath, FileMode.Create))
                        {
                            encoder.Save(filestream);
                        }
                    }
                    if (output.Encoder == "JPEG")
                    {
                        //encoder = new JpegBitmapEncoder();
                        //((JpegBitmapEncoder)encoder).QualityLevel = output.Quality;
                        //extention = ".jpg";
                        // Compress IMAGE
                        var encoder = GetEncoder(ImageFormat.Jpeg);

                        // Create an Encoder object based on the GUID
                        // for the Quality parameter category.
                        Encoder myEncoder  = Encoder.Compression;
                        Encoder myEncoder2 = Encoder.Quality;


                        // Create an EncoderParameters object.
                        // An EncoderParameters object has an array of EncoderParameter
                        // objects. In this case, there is only one
                        // EncoderParameter object in the array.
                        var myEncoderParameters = new EncoderParameters(2);

                        var myEncoderParameter  = new EncoderParameter(myEncoder, 0L);
                        var myEncoderParameter2 = new EncoderParameter(myEncoder2, output.Quality);
                        myEncoderParameters.Param[0] = myEncoderParameter;
                        myEncoderParameters.Param[1] = myEncoderParameter2;

                        imagePath = Path.Combine(Config.SlidesConfiguration.TemplatePath, Guid.NewGuid().ToString() + ".jpg");

                        using (var outStream = new MemoryStream())
                        {
                            BitmapEncoder enc = new BmpBitmapEncoder();
                            enc.Frames.Add(BitmapFrame.Create(PreviewImage));
                            enc.Save(outStream);
                            var bitmap = new Bitmap(outStream);

                            bitmap.Save(imagePath, encoder, myEncoderParameters);
                        }
                    }

                    // Upload Image to Amazon S3 and other Destination Uploads
                    var publicUrl = "";
                    foreach (UploadConfiguration outConf in Config.OutputConfiguration.UploadConfigurations)
                    {
                        try
                        {
                            switch (outConf.Type.ToUpper())
                            {
                            case "S3":
                            {
                                publicUrl = AWSS3Uploader.Upload(imagePath, outConf.AWSAccessKey,
                                                                 outConf.AWSSecretKey, outConf.S3BucketName, outConf.Subfolder,
                                                                 outConf.PublicUriBase);
                            }
                            break;

                            case "FILE":
                            {
                                publicUrl = SystemFileUploader.Upload(imagePath, outConf.DestinationPath,
                                                                      outConf.PublicUriBase);
                            }
                            break;

                            case "FTP":
                            {
                                publicUrl = FtpFileUploader.Upload(imagePath, outConf.FtpServer,
                                                                   outConf.FtpUsername, outConf.FtpPassword, outConf.Subfolder,
                                                                   outConf.UniqueFilename, outConf.PublicUriBase);
                            }
                            break;
                                // TODO Log error on default
                            }
                        }
                        catch (Exception)
                        {
                            // Catch all exception to avoid crash and continue operations
                            // TODO Log and handle
                        }
                    }

                    // TODO Handle multiple Output PublicUrls, last one wins
                    // Add to variants
                    ImageVariants.Add(new ImageVariant()
                    {
                        Name = output.Name, Url = publicUrl
                    });

                    // Save the Url of the IsDefault Image
                    if (output.IsDefault)
                    {
                        PublicImageUrl = publicUrl;
                    }

                    // Delete temporary Image
                    File.Delete(imagePath);
                }
            }
            if (string.IsNullOrEmpty(_publicImageUrl))
            {
                // TODO No default thus choose one
            }

            return(_publicImageUrl);
        }