Json wrapper for package object
Exemple #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="packages"></param>
        public static void Load(List<JsonPackage> packages)
        {
            try
            {
                foreach (var pkg in GetAllPublishedPackages().ToList())
                {
                    //System.Diagnostics.Debug.WriteLine(string.Format("{0}|{1}|{2}|{3}", pkg.Id, pkg.Version, pkg.IsLatestVersion, pkg.IconUrl));

                    var jp = new JsonPackage
                    {
                        Id = pkg.Id,
                        PackageType = pkg.PackageType,
                        Authors = string.IsNullOrEmpty(pkg.Authors) ? "unknown" : pkg.Authors,
                        Description = pkg.Description.Length > 140 ? string.Format("{0}...", pkg.Description.Substring(0, 140)) : pkg.Description,
                        DownloadCount = pkg.DownloadCount,
                        LastUpdated = pkg.LastUpdated.ToString(Utils.GetDefaultCulture().DateTimeFormat.ShortDatePattern),
                        Title = pkg.Title,
                        OnlineVersion = pkg.Version,
                        Website = pkg.ProjectUrl,
                        Tags = pkg.Tags,
                        IconUrl = pkg.IconUrl,
                        Location = "G"
                    };

                    // for themes or widgets, get screenshot instead of icon
                    // also get screenshot if icon is missing for package
                    if(pkg.Screenshots != null && pkg.Screenshots.Count > 0)
                    {
                        if ((pkg.PackageType == Constants.Theme || pkg.PackageType == Constants.Widget) || string.IsNullOrEmpty(pkg.IconUrl))
                        {
                            jp.IconUrl = pkg.Screenshots[0].ScreenshotUri;
                        }
                    }

                    // if both icon and screenshot missing, get default image for package type
                    if (string.IsNullOrEmpty(jp.IconUrl))
                        jp.IconUrl = DefaultThumbnail(pkg.PackageType);

                    if (!string.IsNullOrEmpty(jp.IconUrl) && !jp.IconUrl.StartsWith("http:"))
                        jp.IconUrl = Constants.GalleryUrl + jp.IconUrl;

                    if (!string.IsNullOrWhiteSpace(pkg.GalleryDetailsUrl))
                        jp.PackageUrl = PackageUrl(pkg.PackageType, pkg.Id);

                    //System.Diagnostics.Debug.WriteLine(string.Format("{0}|{1}|{2}|{3}", jp.Id, jp.OnlineVersion, jp.PackageType, jp.IconUrl));

                    packages.Add(jp);
                }
            }
            catch (Exception ex)
            {
                Utils.Log("BlogEngine.Core.Packaging.Load", ex);
            }   
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pkgType"></param>
        /// <param name="page"></param>
        /// <param name="sortOrder"></param>
        /// <param name="searchVal"></param>
        /// <returns></returns>
        public static List <JsonPackage> GetPage(string pkgType, int page = 1, PackageManager.OrderType sortOrder = PackageManager.OrderType.Newest, string searchVal = "")
        {
            var retPkgs = new List <JsonPackage>();

            var packages = PackageManager.GetPackages(pkgType, page, sortOrder, searchVal);

            if (packages == null)
            {
                return(null);
            }

            Count = packages.Count();

            foreach (var p in packages)
            {
                var jp = new JsonPackage
                {
                    Id            = p.Id,
                    PackageType   = pkgType,
                    Authors       = string.IsNullOrEmpty(p.Authors) ? "unknown" : p.Authors,
                    Description   = p.Description,
                    DownloadCount = p.DownloadCount,
                    LastUpdated   = p.LastUpdated.ToString("dd MMM yyyy"),
                    Title         = p.Title,
                    Version       = p.Version,
                    Website       = p.ProjectUrl,
                    Tags          = p.Tags,
                    IconUrl       = p.IconUrl
                };

                if (!string.IsNullOrWhiteSpace(p.GalleryDetailsUrl))
                {
                    switch (p.PackageType)
                    {
                    case "Theme":
                        jp.PackageUrl = "http://dnbegallery.org/cms/List/Themes/" + p.Id;
                        break;

                    case "Extension":
                        jp.PackageUrl = "http://dnbegallery.org/cms/List/Extensions/" + p.Id;
                        break;

                    case "Widget":
                        jp.PackageUrl = "http://dnbegallery.org/cms/List/Widgets/" + p.Id;
                        break;
                    }
                }

                retPkgs.Add(jp);
            }

            return(retPkgs);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pkgType"></param>
        /// <param name="page"></param>
        /// <param name="sortOrder"></param>
        /// <param name="searchVal"></param>
        /// <returns></returns>
        public static List<JsonPackage> GetPage(string pkgType, int page = 1, PackageManager.OrderType sortOrder = PackageManager.OrderType.Newest, string searchVal = "")
        {
            var retPkgs = new List<JsonPackage>();

            var packages = PackageManager.GetPackages(pkgType, page, sortOrder, searchVal);

            if(packages == null)
            {
                return null;
            }

            Count = packages.Count();

            foreach (var p in packages)
            {
                var jp = new JsonPackage
                {
                    Id = p.Id,
                    PackageType = pkgType,
                    Authors = string.IsNullOrEmpty(p.Authors) ? "unknown" : p.Authors,
                    Description = p.Description,
                    DownloadCount = p.DownloadCount,
                    LastUpdated = p.LastUpdated.ToString("dd MMM yyyy"),
                    Title = p.Title,
                    Version = p.Version,
                    Website = p.ProjectUrl,
                    Tags = p.Tags,
                    IconUrl = p.IconUrl
                };

                if (!string.IsNullOrWhiteSpace(p.GalleryDetailsUrl))
                {
                    switch (p.PackageType)
                    {
                        case "Theme":
                            jp.PackageUrl = "http://dnbegallery.org/cms/List/Themes/" + p.Id;
                            break;
                        case "Extension":
                            jp.PackageUrl = "http://dnbegallery.org/cms/List/Extensions/" + p.Id;
                            break;
                        case "Widget":
                            jp.PackageUrl = "http://dnbegallery.org/cms/List/Widgets/" + p.Id;
                            break;
                    }
                }

                retPkgs.Add(jp);
            }

            return retPkgs;
        }
        /// <summary>
        /// get theme manifest from xml file
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static JsonPackage GetThemeManifest(string id)
        {
            var jp = new JsonPackage { Id = id };
            var themeUrl = string.Format("{0}themes/{1}/theme.xml", Utils.ApplicationRelativeWebRoot, id);
            var themePath = HttpContext.Current.Server.MapPath(themeUrl);
            try
            {
                if(File.Exists(themePath))
                {
                    var textReader = new XmlTextReader(themePath);
                    textReader.Read();

                    while (textReader.Read())
                    {
                        textReader.MoveToElement();

                        if (textReader.Name == "description")
                            jp.Description = textReader.ReadString();

                        if (textReader.Name == "authors")
                            jp.Authors = textReader.ReadString();

                        if (textReader.Name == "website")
                            jp.Website = textReader.ReadString();

                        if (textReader.Name == "version")
                            jp.Version = textReader.ReadString();

                        if (textReader.Name == "iconurl")
                            jp.IconUrl = textReader.ReadString();
                    }
                    return jp;
                }
            }
            catch (Exception ex)
            {
                Utils.Log("Packaging.FileSystem.GetThemeManifest", ex);
            }
            return null;
        }
        static JsonPackage GetPackageManifest(string id, string pkgType)
        {
            var jp = new JsonPackage { Id = id, PackageType = pkgType };

            var pkgUrl = pkgType == "Theme" ?
                string.Format("{0}themes/{1}/theme.xml", Utils.ApplicationRelativeWebRoot, id) :
                string.Format("{0}widgets/{1}/widget.xml", Utils.ApplicationRelativeWebRoot, id);

            var pkgPath = HttpContext.Current.Server.MapPath(pkgUrl);
            try
            {
                if (File.Exists(pkgPath))
                {
                    using (var textReader = new XmlTextReader(pkgPath))
                    {
                        textReader.Read();

                        while (textReader.Read())
                        {
                            textReader.MoveToElement();

                            if (textReader.Name == "description")
                                jp.Description = textReader.ReadString();

                            if (textReader.Name == "authors")
                                jp.Authors = textReader.ReadString();

                            if (textReader.Name == "website")
                                jp.Website = textReader.ReadString();

                            if (textReader.Name == "version")
                                jp.LocalVersion = textReader.ReadString();

                            if (textReader.Name == "iconurl")
                                jp.IconUrl = textReader.ReadString();
                        }
                        textReader.Close();
                    }
                    return jp;
                }
            }
            catch (Exception ex)
            {
                Utils.Log("Packaging.FileSystem.GetPackageManifest", ex);
            }
            return null;
        }
        static string DefaultIconUrl(JsonPackage pkg)
        {
            var validImages = new List<string> {"screenshot.jpg", "screenshot.png", "theme.jpg", "theme.png"};
            var pkgDir = pkg.PackageType == "Widget" ? "widgets" : "themes";

            foreach (var img in validImages)
            {
                var url = string.Format("{0}{1}/{2}/{3}",
                Utils.ApplicationRelativeWebRoot, pkgDir, pkg.Id, img);

                var path = HttpContext.Current.Server.MapPath(url);

                if (File.Exists(path)) return url;
            }

            if (pkg.PackageType == "Widget")
                return Utils.ApplicationRelativeWebRoot + "pics/Widget.png";

            return Utils.ApplicationRelativeWebRoot + "pics/Theme.png";
        }
        /// <summary>
        /// Write theme manifest to xml file
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="description">description</param>
        /// <param name="authors">authors</param>
        /// <param name="url">project url</param>
        /// <returns></returns>
        public static JsonPackage WriteThemeManifest(string id, string description = "", string authors = "", string url = "", string version = "", string iconUrl = "")
        {
            if (string.IsNullOrEmpty(authors)) authors = "Unknown";

            var jp = new JsonPackage {Id = id, Description = description, Authors = authors, Website = url};
            var themeUrl = string.Format("{0}themes/{1}/theme.xml", Utils.ApplicationRelativeWebRoot, id);
            var themePath = HttpContext.Current.Server.MapPath(themeUrl);
            try
            {
                var textWriter = new XmlTextWriter(themePath, null)
                    {Formatting = Formatting.Indented, Indentation = 4};

                textWriter.WriteStartDocument();
                textWriter.WriteStartElement("metadata");

                textWriter.WriteElementString("id", id);
                textWriter.WriteElementString("description", description);
                textWriter.WriteElementString("authors", authors);
                textWriter.WriteElementString("website", url);
                textWriter.WriteElementString("version", version);

                #region Thumbnail

                var thumbnail = Utils.ApplicationRelativeWebRoot + "pics/Theme.png";
                var customPng = string.Format("{0}themes/{1}/theme.png", Utils.ApplicationRelativeWebRoot, id);

                if (File.Exists(HttpContext.Current.Server.MapPath(customPng)))
                    thumbnail = customPng;

                if (!string.IsNullOrEmpty(iconUrl))
                    thumbnail = iconUrl;

                textWriter.WriteElementString("iconurl", thumbnail);
                jp.IconUrl = thumbnail;

                #endregion

                textWriter.WriteEndDocument();
                textWriter.Close();
            }
            catch (Exception ex)
            {
                Utils.Log("Packaging.FileSystem.WriteThemeManifest", ex);
                return null;
            }

            return jp;
        }
        /// <summary>
        /// Json package representing current selected theme
        /// </summary>
        /// <param name="themeId">Theme id</param>
        /// <returns>Json package</returns>
        public static JsonPackage GetCurrentTheme(string themeId)
        {
            // first try to pull theme metadata from manifest file
            var jp = FileSystem.GetThemeManifest(themeId);

            if (jp != null)
            {
                if (string.IsNullOrEmpty(jp.IconUrl))
                    jp.IconUrl = DefaultIconUrl(jp);
                return jp;
            }

            // if no manifest file, check themes in online gallery
            // and if found create manifest file using package info
            var srs = new PackagingSource { FeedUrl = _feedUrl };
            var gPkg = GetPublishedPackages(srs).ToList().Where(p => p.Id == themeId).FirstOrDefault();
            if(gPkg != null)
            {
                if (gPkg.Screenshots != null && gPkg.Screenshots.Count > 0)
                    gPkg.IconUrl = string.Format("http://dnbegallery.org{0}", gPkg.Screenshots[0].ScreenshotUri);

                jp = FileSystem.WriteThemeManifest(gPkg.Id, gPkg.Description, gPkg.Authors, gPkg.ProjectUrl, gPkg.Version, gPkg.IconUrl);
                if (jp != null)
                    return jp;
            }

            // generate default blank manifest if both methods failed
            var jpw = FileSystem.WriteThemeManifest(themeId);
            if (jpw != null) jp = jpw;

            // return default values if theme folder read only
            if (jp == null)
                jp = new JsonPackage {Id = themeId, Authors = "Unknown", IconUrl = Utils.ApplicationRelativeWebRoot + "pics/Theme.png" };

            return jp;
        }
        static string DefaultIconUrl(JsonPackage pkg)
        {
            var url = string.Format("{0}themes/{1}/theme.png",
                Utils.ApplicationRelativeWebRoot, pkg.Id);

            var path = HttpContext.Current.Server.MapPath(url);
            return File.Exists(path) ? url : Utils.ApplicationRelativeWebRoot + "pics/Theme.png";
        }
        /// <summary>
        /// Installed packages
        /// </summary>
        /// <param name="pkgType">Package type</param>
        /// <returns>List of installed packages</returns>
        public static List<JsonPackage> InstalledPackages(string pkgType)
        {
            var installedThemes = new Dictionary<string, JsonPackage>();
            var path = HttpContext.Current.Server.MapPath(string.Format("{0}themes/", Utils.ApplicationRelativeWebRoot));

            // read themes directory
            foreach (var d in Directory.GetDirectories(path))
            {
                var index = d.LastIndexOf(Path.DirectorySeparatorChar) + 1;
                var themeId = d.Substring(index);

                // first try to pull theme metadata from manifest file
                var p = FileSystem.GetThemeManifest(themeId);
                if(p == null)
                {
                    p = new JsonPackage();
                    p.Id = themeId;
                }

                if (p.Id != BlogSettings.Instance.Theme &&
                    p.Id != BlogSettings.Instance.MobileTheme &&
                    p.Id != "RazorHost")
                {
                    if(string.IsNullOrEmpty(p.IconUrl))
                        p.IconUrl = DefaultIconUrl(p);
                    installedThemes.Add(p.Id, p);
                }
            }

            // add package metadata from online repository
            var srs = new PackagingSource { FeedUrl = _feedUrl };
            try
            {
                var pkgList = GetPublishedPackages(srs);

                foreach (var pkg in pkgList.ToList())
                {
                    if (pkg.PackageType != pkgType || !pkg.IsLatestVersion) continue;

                    if(installedThemes.ContainsKey(pkg.Id))
                    {
                        installedThemes[pkg.Id].Title = pkg.Title;
                        installedThemes[pkg.Id].Description = pkg.Description;
                        installedThemes[pkg.Id].Tags = pkg.Tags;
                        installedThemes[pkg.Id].Authors = pkg.Authors;
                        installedThemes[pkg.Id].Website = pkg.ProjectUrl;
                        installedThemes[pkg.Id].LastUpdated = pkg.LastUpdated.ToString("dd MMM yyyy");
                        installedThemes[pkg.Id].Version = pkg.Version;

                        if (pkg.PackageType == "Theme" && pkg.Screenshots != null && pkg.Screenshots.Count > 0)
                            installedThemes[pkg.Id].IconUrl = string.Format("http://dnbegallery.org{0}", pkg.Screenshots[0].ScreenshotUri);
                    }
                }

                // return combined result
                return installedThemes.Select(t => t.Value).ToList();
            }
            catch (Exception)
            {
                // no connection - return local
                return installedThemes.Select(t => t.Value).ToList();
            }
        }