public static ThemeResult ExtractTheme(string zipPath, string themeId) { if (!File.Exists(zipPath)) { return(new ThemeResult(new FailedToFindLocation(themeId, zipPath))); } string themePath = Path.Combine("themes", themeId); Directory.CreateDirectory(themePath); try { using (ZipArchive archive = ZipFile.OpenRead(zipPath)) { try { ZipArchiveEntry themeJson = archive.Entries.Single( entry => Path.GetExtension(entry.Name) == ".json"); themeJson.ExtractToFile(Path.Combine(themePath, "theme.json"), true); } catch (InvalidOperationException) { return(RollbackInstall(new NoThemeJSONInZIP(themeId, zipPath))); } return(TryLoad(themeId).Match(RollbackInstall, theme => { ZipArchiveEntry[] imageEntries = archive.Entries.Where( entry => Path.GetDirectoryName(entry.FullName) == "" && Path.GetExtension(entry.Name) != ".json").ToArray(); if (imageEntries.Length == 0) { return RollbackInstall(new NoImagesInZIP(themeId, zipPath)); } foreach (ZipArchiveEntry imageEntry in imageEntries) { imageEntry.ExtractToFile(Path.Combine(themePath, imageEntry.Name), true); } return ThemeJsonValidator.ValidateFull(theme).Match(RollbackInstall, theme2 => new ThemeResult(theme2)); })); } } catch (InvalidDataException) { return(RollbackInstall(new InvalidZIP(themeId, zipPath))); } }
public static ThemeResult CopyLocalTheme(string jsonPath, string themeId) { if (!File.Exists(jsonPath)) { return(new ThemeResult(new FailedToFindLocation(themeId, jsonPath))); } string themePath = Path.Combine("themes", themeId); Directory.CreateDirectory(themePath); File.Copy(jsonPath, Path.Combine(themePath, "theme.json"), true); return(TryLoad(themeId).Match(RollbackInstall, theme => { string sourcePath = Path.GetDirectoryName(jsonPath); string[] imagePaths = Directory.GetFiles(sourcePath, theme.imageFilename); if (imagePaths.Length == 0) { return RollbackInstall(new NoImagesInFolder(themeId, sourcePath)); } foreach (string imagePath in imagePaths) { try { File.Copy(imagePath, Path.Combine(themePath, Path.GetFileName(imagePath)), true); } catch { return RollbackInstall(new FailedToCopyImage(themeId, imagePath)); } } return ThemeJsonValidator.ValidateFull(theme).Match(RollbackInstall, theme2 => new ThemeResult(theme2)); })); }