Ejemplo n.º 1
0
        private static byte[] UpdatePoliticianImages(string politicianKey,
                                                     Stream imageStream, DateTime uploadTime, bool includeAllProfileImages,
                                                     bool includeHeadshotOriginal, bool includeHeadshotResizedImages,
                                                     bool checkForDuplicates, out Size originalSize, out byte[] originalBlob)
        {
            var memoryStream = new MemoryStream();

            imageStream.Position = 0;
            imageStream.CopyTo(memoryStream);
            imageStream.Position = 0;
            var image = Image.FromStream(imageStream);

            originalSize = image.Size;
            var blob = memoryStream.ToArray();

            if (!image.RawFormat.Equals(ImageFormat.Jpeg)) // could be transparent
            {
                // draw on a white background
                var b = new Bitmap(image.Width, image.Height);
                b.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                using (var g = Graphics.FromImage(b))
                {
                    g.Clear(Color.White);
                    g.DrawImageUnscaled(image, 0, 0);
                    //b.Save(@"c:\temp\zz.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                image = b;
            }

            // This is so all subsequent db operations can be updates
            PoliticiansImagesData.GuaranteePoliticianKeyExists(politicianKey);
            PoliticiansImagesBlobs.GuaranteePoliticianKeyExists(politicianKey);

            originalBlob = null;
            if (checkForDuplicates)
            {
                // We fetch this image to see if its a duplicate
                var duplicateTester = GetImageToUseForDuplicateTesting();
                if (duplicateTester != null)
                {
                    var duplicateTesterBlob = GetResizedImageBlobAsJpg(image,
                                                                       duplicateTester.MaxWidth, duplicateTester.MaxHeight);
                    originalBlob = duplicateTesterBlob;
                    var dbBlob =
                        PoliticiansImagesBlobs.GetColumn(duplicateTester.BlobsColumn,
                                                         politicianKey) as Byte[];
                    if (dbBlob != null && dbBlob.Length == duplicateTesterBlob.Length)
                    {
                        var isDuplicate =
                            !dbBlob.Where((t, inx) => t != duplicateTesterBlob[inx])
                            .Any();
                        if (isDuplicate)
                        {
                            return(null); // signals duplicate
                        }
                    }
                }
            }

            if (includeAllProfileImages)
            {
                PoliticiansImagesData.UpdateProfileOriginalDate(uploadTime, politicianKey);
                //PoliticiansImagesBlobs.UpdateProfileOriginal(blob, politicianKey);

                foreach (var info in GetAllProfilePoliticianImageInfos())
                {
                    var resizedBlob = GetResizedImageBlobAsJpg(image, info.MaxWidth,
                                                               info.MaxHeight);
                    PoliticiansImagesBlobs.UpdateColumn(info.BlobsColumn, resizedBlob,
                                                        politicianKey);
                }
            }

            if (includeHeadshotOriginal)
            {
                PoliticiansImagesData.UpdateHeadshotDate(uploadTime, politicianKey);
            }

            if (includeHeadshotResizedImages)
            {
                foreach (var info in GetResizedHeadshotPoliticianImageInfos())
                {
                    var resizedBlob = GetResizedImageBlobAsJpg(image, info.MaxWidth,
                                                               info.MaxHeight);
                    PoliticiansImagesBlobs.UpdateColumn(info.BlobsColumn, resizedBlob,
                                                        politicianKey);
                }
                PoliticiansImagesData.UpdateHeadshotResizeDate(uploadTime, politicianKey);
            }

            // Don't do this here. It belongs as a separate call.
            //CommonCacheInvalidation.ScheduleInvalidation("politicianimage", politicianKey);

            // We return the blob for logging purposes
            return(blob);
        }