Пример #1
0
        public static string GetPoliticianImageColumnNameByWidth(int width)
        {
            var politicianImageInfo =
                PoliticianImageInfoList.SingleOrDefault(info => info.MaxWidth == width);

            return(politicianImageInfo == null
        ? string.Empty
        : PoliticiansImagesBlobs.GetColumnName(politicianImageInfo.BlobsColumn));
        }
Пример #2
0
 private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
 {
     try
     {
         var politicianKey = PoliticianKeyTextBox.Text.Trim();
         if (IsNullOrEmpty(politicianKey))
         {
             throw new VoteException("Politician Key is missing");
         }
         if (IsNullOrEmpty(FolderTextBox.Text.Trim()))
         {
             throw new VoteException("Save Folder is missing");
         }
         var folder = new DirectoryInfo(FolderTextBox.Text.Trim());
         if (!folder.Exists)
         {
             throw new VoteException("Save Folder does not exist");
         }
         var newOriginalsTable =
             LogDataChange.GetDataByTableNameColumnNameKeyValues(
                 PoliticiansImagesBlobs.TableName,
                 PoliticiansImagesBlobs.GetColumnName(
                     PoliticiansImagesBlobs.Column.ProfileOriginal), politicianKey, 0);
         var newHeadshotsTable =
             LogDataChange.GetDataByTableNameColumnNameKeyValues(
                 PoliticiansImagesBlobs.TableName,
                 PoliticiansImagesBlobs.GetColumnName(
                     PoliticiansImagesBlobs.Column.Headshot100), politicianKey, 0);
         var images = new List <ImageInfo>();
         images.AddRange(newOriginalsTable.Where(row => row.NewValue != null)
                         .Select(row => new ImageInfo
         {
             ImageType = "Profile",
             Date      = row.DateStamp,
             User      = row.UserName,
             Blob      = Convert.FromBase64String(row.NewValue)
         }));
         images.AddRange(newHeadshotsTable.Where(row => row.NewValue != null)
                         .Select(row => new ImageInfo
         {
             ImageType = "Headshot",
             Date      = row.DateStamp,
             User      = row.UserName,
             Blob      = Convert.FromBase64String(row.NewValue)
         }));
         if (images.Count == 0)
         {
             throw new VoteException("No logged images found for this Politician Key");
         }
         folder.CreateSubdirectory(politicianKey);
         AppendStatusText("Found {0} images:", images.Count);
         foreach (var imageInfo in images)
         {
             var memoryStream = new MemoryStream(imageInfo.Blob);
             var image        = Image.FromStream(memoryStream);
             var contentType  = ImageManager.GetContentType(image);
             var extension    = $".{contentType.Replace("image/", "")}";
             var filename     =
                 $"{imageInfo.ImageType}_{imageInfo.Date:yyyy-MM-dd-HH-mm-ss}_{imageInfo.User}{extension}";
             var path = Path.Combine(folder.FullName, politicianKey, filename);
             File.WriteAllBytes(path, imageInfo.Blob);
             AppendStatusText(filename);
         }
         AppendStatusText(Empty);
     }
     catch (VoteException ex)
     {
         AppendStatusText(ex.Message);
     }
     catch (Exception ex)
     {
         AppendStatusText(ex.ToString());
     }
 }
Пример #3
0
        private static void ServeImageContent(string politicianKey, string column,
                                              string defaultColumn, DateTime lastModDate, bool isModified, bool noCache)
        {
            var response = HttpContext.Current.Response;
            //var maxAge = new TimeSpan(24, 0, 0); // 24 hours -- used in headers
            var maxAge            = new TimeSpan(0, 0, 0); // force a server query always
            var expiration        = DateTime.UtcNow + maxAge;
            var isProfileOriginal =
                column.IsEqIgnoreCase(
                    PoliticiansImagesBlobs.GetColumnName(
                        PoliticiansImagesBlobs.Column.ProfileOriginal));

            if (!isModified)
            {
                response.StatusCode        = 304;
                response.StatusDescription = "Not Modified";
            }

            response.Cache.SetCacheability(HttpCacheability.Public);
            response.Cache.SetETag('"' +
                                   lastModDate.Ticks.ToString(CultureInfo.InvariantCulture) + '"');
            response.Cache.SetLastModified(lastModDate);
            response.Cache.SetMaxAge(maxAge);
            response.Cache.SetExpires(expiration);
            response.Cache.SetSlidingExpiration(false);
            response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            if (isModified) // serve actual image
            {
                var cache    = HttpContext.Current.Cache;
                var cacheKey = "politicianImage." + politicianKey + "." + column;

                ApplicationCacheEntry cacheEntry = null;
                // We don't use memory cache for profile originals
                if (!noCache && !isProfileOriginal)
                {
                    cacheEntry = cache.Get(cacheKey) as ApplicationCacheEntry;
                }
                if (cacheEntry != null && cacheEntry.Expiration > DateTime.UtcNow)
                {
                    response.ContentType = cacheEntry.ContentType;
                    response.BinaryWrite(cacheEntry.Blob);
                    Interlocked.Increment(ref CurrentStatistics.ImagesServedFromMemoryCache);
                }
                else
                {
                    var blob = GetPoliticianImage(politicianKey, column, defaultColumn,
                                                  noCache);
                    if (blob != null)
                    {
                        // we get the content type from the actual image
                        var contentType = GetContentType(blob);
                        response.ContentType = contentType;
                        response.BinaryWrite(blob);

                        if (!isProfileOriginal)
                        {
                            var cacheExpiration =
                                DateTime.UtcNow.AddMinutes(MemCache.CacheExpiration);
                            cacheEntry = new ApplicationCacheEntry
                            {
                                Expiration  = cacheExpiration,
                                Blob        = blob,
                                ContentType = contentType
                            };
                            cache.Add(cacheKey, cacheEntry, null, cacheExpiration,
                                      Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                        }
                        Interlocked.Increment(ref CurrentStatistics.ImagesServedFromDisc);
                    }
                }
            }
            else // tell browser to use cached version
            {
                response.AddHeader("Content-Length", "0");
                Interlocked.Increment(ref CurrentStatistics.ImagesServedFromBrowserCache);
            }

            if (_LastStatisticsSnapshot.SnapshotTime + StatisticsSnapshotInterval <
                DateTime.UtcNow)
            {
                // May need to snapshot statistics, but make sure only one thread does it
                if (Interlocked.Exchange(ref _SnapshottingStatistics, 1) == 0)
                // it's our job
                {
                    var previousSnapshot = _LastStatisticsSnapshot;
                    _LastStatisticsSnapshot = CurrentStatistics.Clone();
                    _LastStatisticsSnapshot.SnapshotTime = DateTime.UtcNow;
                    VotePage.LogInfo("ImageCaching",
                                     $"Image caching from {previousSnapshot.SnapshotTime} to {_LastStatisticsSnapshot.SnapshotTime}:" +
                                     $" {_LastStatisticsSnapshot.ImagesServedFromBrowserCache - previousSnapshot.ImagesServedFromBrowserCache} from browser cache," +
                                     $" {_LastStatisticsSnapshot.ImagesServedFromMemoryCache - previousSnapshot.ImagesServedFromMemoryCache} from memory cache," +
                                     $" {_LastStatisticsSnapshot.ImagesServedFromDisc - previousSnapshot.ImagesServedFromDisc} from disc");
                    Interlocked.Exchange(ref _SnapshottingStatistics, 0);
                }
            }

            response.End();
        }