private void SaveFeed(CloudBlob blob, AtomFeed feed) {
     feed.Save(_localfeedLocation);
     blob.CopyFromFile(_localfeedLocation);
 }
        private void InsertIntoFeed(CanonicalName pkgCanonicalName, FourPartVersion pkgVersion, Uri location, AtomItem item = null) {
            FeedBlob.Lock(blob => {
                // update the feed
                var feed = new AtomFeed();

                //load the feed from the _canonicalFeedUrl if we can
                try {
                    var originalFeed = LoadFeed(blob);

                    foreach (AtomItem i in originalFeed.Items.Where(each => each is AtomItem)) {
                        if (_feedName == "current") {
                            // if an older version of this package is in the current feed, 
                            if (i.Model.CanonicalName.DiffersOnlyByVersion(pkgCanonicalName) && i.Model.CanonicalName.Version < pkgVersion) {
                                // push it to the archive feed.
                                try {
                                    if (FeedHandlers["archive"] != null) {
                                        FeedHandlers["archive"].InsertIntoFeed(i.Model.CanonicalName, i.Model.Version, i.Model.Locations[0]);
                                        // and skip it
                                        continue;
                                    }
                                } catch {
                                    
                                }
                            }
                        }
                        feed.Add(i);
                    }
                } catch {
                }
                item = item ?? _packageManager.GetAtomItem(pkgCanonicalName).Result;

                if (item != null) {
                    // first, make sure that the feeds contains the intended feed location.

                    item.Model.Feeds = item.Model.Feeds ?? new XList<Uri>();
                    if (!item.Model.Feeds.Contains(_canonicalFeedUrl)) {
                        item.Model.Feeds.Insert(0, _canonicalFeedUrl);
                    }

                    item.Model.Locations = item.Model.Locations ?? new XList<Uri>();
                    if (!item.Model.Locations.Contains(location)) {
                        item.Model.Locations.Insert(0, location);
                    }

                    // drop dead urls
                    item.Model.Feeds = item.Model.Feeds.Distinct().Where(Peek).ToXList();
                    item.Model.Locations = item.Model.Locations.Distinct().Where(Peek).ToXList();
                    foreach (var l in item.Links.ToArray().Where(each => !Peek(each.Uri))) {
                        item.Links.Remove(l);
                    }

                    if (item.Model.Locations.Any()) {
                        // if we've got at least one valid location, add the item to the feed.
                        feed.Add(item);
                    }
                }
                SaveFeed(blob, feed);

                // regenerate the webpi feed based on the items in this feed
                SaveWebPiFeed(feed);
            });
        }
        private string SaveWebPiFeed(AtomFeed feed) {
            var webPiFeed = new WebPIFeed(_canonicalFeedUrl.AbsoluteUri, _feedName, feed.Items.Select(each => each as AtomItem));

            WebPiFeedBlob.WriteText(webPiFeed.ToString());
            return webPiFeed.ToString();
        }
 private Task<int> Validate() {
     return Task.Factory.StartNew(() => {
         var feed = new AtomFeed();
         //load the feed from the _canonicalFeedUrl if we can
         try {
             FeedBlob.Lock(blob => {
                 var originalFeed = LoadFeed(blob);
                 foreach (AtomItem i in originalFeed.Items.Where(each => each is AtomItem)) {
                     // drop dead urls
                     i.Model.Feeds = i.Model.Feeds.Distinct().Where(Peek).ToXList();
                     i.Model.Locations = i.Model.Locations.Distinct().Where(Peek).ToXList();
                     foreach (var l in i.Links.ToArray().Where(each => !Peek(each.Uri))) {
                         i.Links.Remove(l);
                     }
                     if (i.Model.Locations.Any()) {
                         feed.Add(i);
                     }
                 }
                 SaveFeed(blob, feed);
             });
         } catch {
             return 500;
         }
         return 200;
     });
 }