public void CreateUrlContent_Invoke_ReturnsExpected(Uri url, string mediaType)
        {
            UrlSyndicationContent syndicationContent = SyndicationContent.CreateUrlContent(url, mediaType);

            Assert.Empty(syndicationContent.AttributeExtensions);
            Assert.Equal(url, syndicationContent.Url);
            Assert.Equal(mediaType, syndicationContent.Type);
        }
        private static void _AddPackages(Uri root, string imageRoot, IEnumerable <IVsixPackage> packages, SyndicationFeed feed, Logger log)
        {
            // See https://msdn.microsoft.com/en-us/library/hh266717.aspx

            var items = new List <SyndicationItem>();

            feed.Items = items;

            var orderedPackages = packages
                                  .OrderBy(pkg => pkg.DisplayName)
                                  .ThenBy(pkg => pkg.Id);

            foreach (var pkg in orderedPackages)
            {
                log.Info($"Adding package {pkg.DisplayName} ({pkg.Id}) to feed");

                var item = new SyndicationItem();
                item.Id              = pkg.Id;
                item.Title           = new TextSyndicationContent(pkg.DisplayName);
                item.Summary         = new TextSyndicationContent(pkg.Description);
                item.PublishDate     = new DateTimeOffset(File.GetLastWriteTimeUtc(pkg.File));
                item.LastUpdatedTime = new DateTimeOffset(File.GetLastWriteTimeUtc(pkg.File));
                item.Authors.Add(new SyndicationPerson {
                    Name = pkg.Publisher
                });
                item.Content = SyndicationContent.CreateUrlContent(root.MakeRelativeUri(new Uri(pkg.File)), "application/octet-stream");

                _AddPreviewImages(root, imageRoot, item, pkg, log);

                var ns      = XNamespace.Get("http://schemas.microsoft.com/developer/vsx-syndication-schema/2010");
                var xsi     = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
                var content = new XElement(
                    ns + "Vsix",
                    new XAttribute(XNamespace.Xmlns + "xsi", xsi),
                    new XElement(ns + "Id", pkg.Id),
                    new XElement(ns + "Version", pkg.Version),
                    new XElement(ns + "References"),
                    new XElement(
                        ns + "Rating",
                        new XAttribute(xsi + "nil", "true")),
                    new XElement(
                        ns + "RatingCount",
                        new XAttribute(xsi + "nil", "true")),
                    new XElement(
                        ns + "DownloadCount",
                        new XAttribute(xsi + "nil", "true")));

                using (var stringReader = new StringReader(content.ToString()))
                    using (var reader = XmlReader.Create(stringReader))
                    {
                        item.ElementExtensions.Add(reader);
                    }

                items.Add(item);
            }
        }
Пример #3
0
        public static SyndicationItem Syndicate(this IPublicationMedia publicationMedia)
        {
            var item = new SyndicationItem {
                Id              = publicationMedia.Id,
                Title           = new TextSyndicationContent(publicationMedia.Title, TextSyndicationContentKind.Plaintext),
                LastUpdatedTime = publicationMedia.LastUpdated,
                Summary         = new TextSyndicationContent(publicationMedia.Summary, TextSyndicationContentKind.Plaintext),
                Content         = SyndicationContent.CreateUrlContent(publicationMedia.ImageUrl, publicationMedia.ContentType),
            };

            foreach (var link in publicationMedia.Links)
            {
                item.Links.Add(new SyndicationLink(new Uri(link.Href))
                {
                    RelationshipType = link.Rel, Title = link.Title
                });
            }

            return(item);
        }
Пример #4
0
        private void AddPackages(SyndicationFeed feed,
                                 Uri root,
                                 string rootStoragePath,
                                 IEnumerable <IVsixPackage> packages)
        {
            // See https://msdn.microsoft.com/en-us/library/hh266717.aspx
            var items = new List <SyndicationItem>();

            var orderedPackages = packages
                                  .OrderBy(pkg => pkg.DisplayName)
                                  .ThenBy(pkg => pkg.Id)
                                  .ToArray();

            OnBackgroundProgress(new VsixFeedBuilderProgressArgs(0, orderedPackages.Length, "Starting"));

            for (var pkgIdx = 0; pkgIdx < orderedPackages.Length; pkgIdx++)
            {
                var pkg = orderedPackages[pkgIdx];

                // Possibly normalize the file name for the package,
                // visual studio does not like special characters in the file download urls so it is best to clear these out
                pkg.File = FileCleaner.NormalizePackageFileName(pkg.File);
                var pkgFileName = Path.GetFileName(pkg.File);

                OnBackgroundProgress(new VsixFeedBuilderProgressArgs(pkgIdx + 1, orderedPackages.Length, pkgFileName));

                // Schema: https://msdn.microsoft.com/en-us/library/dd393754(v=vs.100).aspx
                // Extension Schema: https://msdn.microsoft.com/en-us/library/dd393700(v=vs.100).aspx
                var item = new SyndicationItem
                {
                    Id              = pkg.Id,
                    Title           = new TextSyndicationContent(pkg.DisplayName),
                    Summary         = new TextSyndicationContent(pkg.Description),
                    PublishDate     = new DateTimeOffset(File.GetLastWriteTimeUtc(pkg.File)),
                    LastUpdatedTime = new DateTimeOffset(File.GetLastWriteTimeUtc(pkg.File))
                };

                // Create the subfolder for the package contents on the local system, ensure that the path exists
                var packageFolderPath = Path.Combine(rootStoragePath, pkg.Id);
                Directory.CreateDirectory(packageFolderPath);

                // Create a mapping between the vsix id and the file name by placing a simple file in the root
                FileCounter.SetIdToVsixFile(pkg.Id, pkgFileName, _configStorage);

                item.Authors.Add(new SyndicationPerson {
                    Name = pkg.Publisher
                });

                // If the configuration specifies that the downloads should be tracked then route the downloads
                // through the DownloadModule for tracking, if tracking is off then serve the static files directly
                // from the file-system
                item.Content = SyndicationContent.CreateUrlContent(_configGallery.TrackDownloads
                                                                   ? new Uri($"../api/download/{pkg.Id}/{pkgFileName}", UriKind.Relative)
                                                                   : root.MakeRelativeUri(new Uri(pkg.File)), "application/octet-stream");

                // Only use the first category found, the developer should put the most important category first anyways
                var category = pkg.Categories.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
                if (category != null)
                {
                    item.Categories.Add(new SyndicationCategory(category.Trim().ToTitleCase()));
                }

                if (pkg.TrySaveIcon(packageFolderPath, out Uri iconUri))
                {
                    item.Links.Add(new SyndicationLink(root.MakeRelativeUri(iconUri), "icon", "", "", 0));
                }

                if (pkg.TrySavePreviewImage(packageFolderPath, out Uri previewUri))
                {
                    item.Links.Add(new SyndicationLink(root.MakeRelativeUri(previewUri), "previewimage", "", "", 0));
                }

                // Displays the "More Info" link if set
                if (pkg.MoreInfoUrl.IsUrl())
                {
                    item.Links.Add(new SyndicationLink(new Uri(pkg.MoreInfoUrl), "alternate", "", "", 0)); // Must be set to "alternate", rest does not need to be set
                }
                // Displays the "Release Notes" link if set
                if (pkg.ReleaseNotes.IsUrl())
                {
                    item.Links.Add(new SyndicationLink(new Uri(pkg.ReleaseNotes), "releasenotes", "", "", 0));
                }

                // Add the extensions to the item
                AddExtensions(item, pkg);

                // Now save the item
                items.Add(item);
            }
            OnBackgroundProgress(new VsixFeedBuilderProgressArgs(orderedPackages.Length, orderedPackages.Length, "All done"));

            // Save all the items into the feed object
            feed.Items = items;
        }
 public void CreateUrlContent_NullUrl_ThrowsArgumentNullException()
 {
     AssertExtensions.Throws <ArgumentNullException>("url", () => SyndicationContent.CreateUrlContent(null, "mediaType"));
 }
        /// <summary>
        /// Gets the content.
        /// </summary>
        /// <typeparam name="T">Type of content.</typeparam>
        /// <param name="content">The content.</param>
        /// <param name="properties">The properties.</param>
        /// <param name="typeProperty">The type property.</param>
        /// <param name="isRequired">if set to <c>true</c> [is required].</param>
        /// <returns>Content of the required type.</returns>
        private static T GetContent <T>(
            string content,
            IEnumerable <ResourceProperty> properties,
            string typeProperty,
            bool isRequired) where T : SyndicationContent
        {
            if (string.IsNullOrEmpty(content))
            {
                if (isRequired)
                {
                    return(new TextSyndicationContent(string.Empty) as T);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                string           propertyUri         = AtomPubHelper.GetPropertyUri(typeProperty);
                ResourceProperty contentTypeProperty = properties
                                                       .Where(tuple => null != tuple.Property && tuple.Property.Uri == propertyUri)
                                                       .FirstOrDefault();

                SyndicationContent syndicationContent = null;

                if (null == contentTypeProperty)
                {
                    syndicationContent = new TextSyndicationContent(content, TextSyndicationContentKind.Plaintext);
                }
                else
                {
                    switch (contentTypeProperty.Value.ToUpperInvariant())
                    {
                    case "HTML":
                        syndicationContent = new TextSyndicationContent(content, TextSyndicationContentKind.Html);
                        break;

                    case "XHTML":
                        syndicationContent = new TextSyndicationContent(content, TextSyndicationContentKind.XHtml);
                        break;

                    case "TEXT":
                        syndicationContent = new TextSyndicationContent(content, TextSyndicationContentKind.Plaintext);
                        break;

                    default:

                        if (typeof(T) == typeof(UrlSyndicationContent))
                        {
                            Uri contentUrl = new Uri(content);
                            syndicationContent = SyndicationContent.CreateUrlContent(contentUrl, contentTypeProperty.Value);
                        }
                        else
                        {
                            syndicationContent = new XmlSyndicationContent(contentTypeProperty.Value, content, new NetDataContractSerializer());
                        }
                        break;
                    }
                }

                return(syndicationContent as T);
            }
        }