public ActionResult ImportThemeFromFile(ImportThemeViewModel vm) { if (!ModelState.IsValid) { return(SiteErrorHandler.GetBadRequestActionResult(ModelState)); } // Check if the theme name is already in use. if (_themeRepository.IsThemeNameAlreadyInUse(0, vm.Name)) { return(SiteErrorHandler.GetBadRequestActionResult($"The theme name {vm.Name} is already in use.", nameof(vm.Name))); } // Check if the file type is correct. if (vm.FileToUse.ContentType != "application/json") { return(SiteErrorHandler.GetBadRequestActionResult("File must be .json", "")); } // Check if the file has any content. if (vm.FileToUse.ContentLength == 0) { return(SiteErrorHandler.GetBadRequestActionResult("File is empty", "")); } List <ThemeVariableValueModel> variables; // Read the file and convert the json to a theme variable value collection. try { var b = new System.IO.BinaryReader(vm.FileToUse.InputStream); byte[] binData = b.ReadBytes(vm.FileToUse.ContentLength); string result = System.Text.Encoding.UTF8.GetString(binData); variables = _themeService.ConvertJsonToThemeVariableValues(result); } catch (Exception ex) { return(SiteErrorHandler.GetBadRequestActionResult(ex.Message, "")); } // Check if there are any values inside the file. if (variables == null || !variables.Any()) { return(SiteErrorHandler.GetBadRequestActionResult("Imported theme did not have any values", "")); } // Import the theme. int themeId = _themeRepository.ImportTheme(vm.Name, variables); return(Json(new { message = "<strong>Success</strong>: Theme has been created.", themeId })); }
// Import theme view. public ActionResult ImportTheme() { var vm = new ImportThemeViewModel(); return(View(vm)); }