Пример #1
0
        /// <summary>
        /// Returns the bundle stream for given themes.
        /// </summary>
        /// <param name="themes">Themes.</param>
        /// <returns>Bundle stream.</returns>
        public Stream GetBundleStream(UserThemeResult themes = null)
        {
            int userId = GetUserId();

            return(userId > 0 ? ThemeSource.GetBundleStream <UserTheme, UserThemeResult>(
                       string.Format("~/App_Data/Themes/{0}", userId), GetImportedThemes()) : null);
        }
Пример #2
0
        /// <summary>
        /// Returns all themes that have been imported earlier from user hard drive.
        /// </summary>
        /// <returns>Themes.</returns>
        public UserThemeResult GetImportedThemes()
        {
            int userId = GetUserId();

            return(userId > 0 ? ThemeSource.GetImportedThemes <UserTheme, UserThemeResult>(
                       string.Format("~/App_Data/Themes/{0}", userId)) : new UserThemeResult());
        }
Пример #3
0
        /// <summary>
        /// Returns all themes that have been imported earlier from GitHub repository.
        /// </summary>
        /// <param name="ensureOnlyFiles">A list of theme file names (without directory information) to treat as white-list - all other files will be deleted.</param>
        /// <returns>Themes.</returns>
        private GitHubThemeResult GetImportedThemesInternal(IEnumerable <string> ensureOnlyFiles = null)
        {
            GitHubThemeResult   ret           = null;
            GitHubInMemoryTheme inMemoryTheme = null;

            inMemoryTheme = GetUrlSpecifiedTheme();

            if (inMemoryTheme != null)
            {
                ret = new GitHubThemeResult()
                {
                    Themes = new List <GitHubTheme>()
                    {
                        new GitHubTheme()
                        {
                            Id   = inMemoryTheme.Id,
                            Name = inMemoryTheme.Name
                        }
                    },
                    Checksum = CalculateChecksum(inMemoryTheme)
                };
            }
            else
            {
                ret = ThemeSource.GetImportedThemes <GitHubTheme, GitHubThemeResult>("~/Assets/css/Themes/", ensureOnlyFiles);
            }

            return(ret);
        }
Пример #4
0
        /// <summary>
        /// Returns the theme which is specified via URL.
        /// </summary>
        /// <returns>Theme.</returns>
        private GitHubInMemoryTheme GetUrlSpecifiedTheme()
        {
            GitHubInMemoryTheme ret = null;
            string inMemoryThemeUrl = ThemeSource.GetThemeUrlFromRequest(HttpContext.Current.Request);

            if (!string.IsNullOrEmpty(inMemoryThemeUrl))
            {
                ret = GitHubInMemoryThemeCache.Read();

                if (ret == null || string.Compare(inMemoryThemeUrl, ret.Uri, true) != 0)
                {
                    ret = ThemeSource.CreateThemeInstance <GitHubInMemoryTheme>(inMemoryThemeUrl, t =>
                    {
                        t.Uri     = inMemoryThemeUrl;
                        t.Content = Encoding.UTF8.GetBytes(ThemeSource.DownloadString(inMemoryThemeUrl));
                    });

                    if (ret.Content != null && ret.Content.Length > 0)
                    {
                        GitHubInMemoryThemeCache.Update(ret);
                    }
                    else
                    {
                        ret = null;
                    }
                }
            }

            return(ret);
        }
Пример #5
0
        /// <summary>
        /// Returns the bundle stream for given themes.
        /// </summary>
        /// <param name="themes">Themes.</param>
        /// <returns>Bundle stream.</returns>
        public Stream GetBundleStream(GitHubThemeResult themes = null)
        {
            Stream ret = null;
            GitHubInMemoryTheme inMemoryTheme = null;

            if (themes == null)
            {
                inMemoryTheme = GetUrlSpecifiedTheme();

                if (inMemoryTheme != null)
                {
                    ret = new MemoryStream(inMemoryTheme.Content);
                }
            }

            if (ret == null)
            {
                if (themes == null)
                {
                    themes = ImportThemes();
                }

                ret = ThemeSource.GetBundleStream <GitHubTheme, GitHubThemeResult>("~/Assets/css/Themes/", themes);
            }

            return(ret);
        }
Пример #6
0
        /// <summary>
        /// Clones the given theme.
        /// </summary>
        /// <param name="sourceId">An Id of the theme to clone.</param>
        /// <param name="name">The name for a new theme.</param>
        /// <param name="overrides">Theme overrides.</param>
        /// <returns>New theme.</returns>
        public UserTheme CloneTheme(string sourceId, string name, ThemeMetadata overrides)
        {
            UserTheme ret              = null;
            int       userId           = GetUserId();
            string    cssText          = string.Empty;
            string    fullPhysicalPath = string.Empty;
            string    newId            = ThemeSource.MakeId(name);

            ThemeMetadata.ExCSSStylesheet stylesheet = null;
            string fullPhysicalPathDirectoryName     = string.Empty;
            string fullPath = string.Format("~/App_Data/Themes/{0}/{1}.css", userId, name);

            if (userId > 0)
            {
                stylesheet = new ThemeMetadata.ExCSSStylesheet(sourceId, ThemeMetadata.ResolveThemeContents(sourceId));

                if (overrides != null)
                {
                    stylesheet.FontFamily      = overrides.FontFamily;
                    stylesheet.FontColor       = overrides.FontColor;
                    stylesheet.AccentColor1    = overrides.AccentColor1;
                    stylesheet.AccentColor2    = overrides.AccentColor2;
                    stylesheet.AccentColor3    = overrides.AccentColor3;
                    stylesheet.AccentColor4    = overrides.AccentColor4;
                    stylesheet.BackgroundColor = overrides.BackgroundColor;
                    stylesheet.BackgroundImage = overrides.BackgroundImage ?? string.Empty;
                    stylesheet.Logo            = overrides.Logo;
                }

                cssText = stylesheet.ToString();
                cssText = Regex.Replace(cssText, string.Concat("\\.theme-", sourceId), string.Concat(".theme-", newId), RegexOptions.IgnoreCase);

                fullPhysicalPath = HttpContext.Current.Server.MapPath(fullPath);
                fullPhysicalPathDirectoryName = System.IO.Path.GetDirectoryName(fullPhysicalPath);

                if (!System.IO.Directory.Exists(fullPhysicalPathDirectoryName))
                {
                    System.IO.Directory.CreateDirectory(fullPhysicalPathDirectoryName);
                }

                System.IO.File.WriteAllText(fullPhysicalPath, cssText, System.Text.Encoding.UTF8);

                ret = ThemeSource.CreateThemeInstance <UserTheme>(fullPhysicalPath, t => t.PhysicalPath = fullPhysicalPath, true);
            }

            return(ret);
        }
Пример #7
0
        /// <summary>
        /// Imports all themes from the underlying context and returns the imported themes.
        /// </summary>
        /// <returns>Themes.</returns>
        public GitHubThemeResult ImportThemes()
        {
            int modifierIndex = -1;

            string[]          export        = null;
            string[]          modifiers     = null;
            GitHubThemeResult ret           = null;
            string            themeFilePath = string.Empty;
            string            themeContents = string.Empty;
            string            directoryPath = string.Empty;
            HashSet <string>  exported      = new HashSet <string>();
            string            baseUrl       = "https://raw.githubusercontent.com/spritesapp/sprites-themes/master";

            ret = GitHubThemeCache.Read();

            if (ret == null)
            {
                directoryPath = HttpContext.Current.Server.MapPath("~/Assets/css/Themes/");
                export        = ThemeSource.DownloadString(string.Format("{0}/EXPORT", baseUrl)).Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

                if (export.Any())
                {
                    if (!Directory.Exists(directoryPath))
                    {
                        Directory.CreateDirectory(directoryPath);
                    }

                    foreach (string themeFileName in export.Select(e => e.Trim()))
                    {
                        if (!string.IsNullOrWhiteSpace(themeFileName) && !themeFileName.StartsWith("#"))
                        {
                            modifierIndex = themeFileName.IndexOf('!');

                            modifiers = modifierIndex > 0 ?
                                        themeFileName.Substring(modifierIndex)
                                        .TrimStart('!')
                                        .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                        .Select(m => m.Trim().ToLowerInvariant())
                                        .ToArray() : null;

                            themeFilePath = Path.Combine(directoryPath, modifierIndex > 0 ?
                                                         themeFileName.Substring(0, modifierIndex).Trim() : themeFileName);

                            if (!File.Exists(themeFilePath) || (modifiers != null && modifiers.Any(m => string.Compare(m, "force", true) == 0)))
                            {
                                themeContents = ThemeSource.DownloadString(string.Format("{0}/lib/{1}", baseUrl, themeFileName));
                                if (!string.IsNullOrWhiteSpace(themeContents))
                                {
                                    if (!exported.Contains(themeFileName))
                                    {
                                        exported.Add(themeFileName);
                                    }

                                    if (File.Exists(themeFilePath))
                                    {
                                        File.Delete(themeFilePath);
                                    }

                                    File.WriteAllText(themeFilePath, themeContents, Encoding.UTF8);
                                }
                            }
                        }
                    }
                }

                ret = GetImportedThemesInternal(exported);

                GitHubThemeCache.Update(ret);
            }

            return(ret);
        }
Пример #8
0
        /// <summary>
        /// Returns the bundle stream for given themes.
        /// </summary>
        /// <typeparam name="TTheme">Theme type.</typeparam>
        /// <typeparam name="TThemeResult">Theme result type.</typeparam>
        /// <param name="basePath">Base theme path.</param>
        /// <param name="themes">Themes.</param>
        /// <returns>Bundle stream.</returns>
        public static Stream GetBundleStream <TTheme, TThemeResult>(string basePath, TThemeResult themes)
            where TThemeResult : ThemeResultBase <TTheme>
            where TTheme : ThemeBase
        {
            Stream        ret                = null;
            string        checksum           = null;
            string        bundlePath         = null;
            Stream        themeStream        = null;
            string        directoryPath      = null;
            string        existingBundle     = null;
            string        bundlePhysicalPath = null;
            StringBuilder bundleContent      = null;

            bundleContent = new StringBuilder();
            directoryPath = HttpContext.Current.Server.MapPath(basePath);

            if (themes != null && themes.Themes != null && themes.Themes.Any())
            {
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                checksum = themes.Checksum;

                if (string.IsNullOrEmpty(checksum))
                {
                    checksum = ThemeSource.CalculateChecksum(themes.Themes);
                }

                if (!string.IsNullOrEmpty(checksum))
                {
                    bundlePhysicalPath = Path.Combine(directoryPath, string.Format("{0}.bundle", checksum));

                    if (!File.Exists(bundlePhysicalPath))
                    {
                        lock (_lock)
                        {
                            existingBundle = Directory.EnumerateFiles(directoryPath, "*.bundle").FirstOrDefault();

                            if (!string.IsNullOrEmpty(existingBundle))
                            {
                                File.Delete(existingBundle);
                            }

                            foreach (TTheme theme in themes.Themes)
                            {
                                themeStream = theme.OpenRead();

                                if (themeStream != null)
                                {
                                    using (themeStream)
                                    {
                                        using (StreamReader reader = new StreamReader(themeStream))
                                            bundleContent.AppendLine(reader.ReadToEnd()).AppendLine();
                                    }
                                }
                            }

                            if (bundleContent.Length > 0)
                            {
                                bundlePath = bundlePhysicalPath;
                                File.WriteAllText(bundlePhysicalPath, SecureContent(bundleContent.ToString()), Encoding.UTF8);
                            }
                        }
                    }
                    else
                    {
                        bundlePath = bundlePhysicalPath;
                    }
                }
            }

            ret = !string.IsNullOrEmpty(bundlePath) ? new FileStream(bundlePath, FileMode.Open, FileAccess.Read) : null;

            return(ret);
        }