/// <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); }
/// <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); }