Example #1
0
        public ViewResult ImportResources(bool?previewImport, HttpPostedFileBase importFile, bool?showMenu)
        {
            var model = new ImportResourcesViewModel {
                ShowMenu = showMenu ?? false
            };

            if (importFile == null || importFile.ContentLength == 0)
            {
                return(View("ImportResources", model));
            }

            var fileInfo        = new FileInfo(importFile.FileName);
            var potentialParser = ConfigurationContext.Current.Import.Providers.FindByExtension(fileInfo.Extension);

            if (potentialParser == null)
            {
                ModelState.AddModelError("file", $"Unknown file extension - `{fileInfo.Extension}`");
                return(View("ImportResources", model));
            }

            var workflow     = new ResourceImportWorkflow();
            var streamReader = new StreamReader(importFile.InputStream);
            var fileContent  = streamReader.ReadToEnd();
            var allLanguages = new AvailableLanguages.Query().Execute();

            try
            {
                var parseResult        = potentialParser.Parse(fileContent);
                var differentLanguages = parseResult.DetectedLanguages.Except(allLanguages);
                if (differentLanguages.Any())
                {
                    ModelState.AddModelError("file", $"Importing language `{string.Join(", ", differentLanguages.Select(c => c.Name))}` is not available in current EPiServer installation");
                    return(View("ImportResources", model));
                }

                if (previewImport.HasValue && previewImport.Value)
                {
                    var changes          = workflow.DetectChanges(parseResult.Resources, new GetAllResources.Query(true).Execute());
                    var changedLanguages = changes.SelectMany(c => c.ChangedLanguages).Distinct().Select(l => new CultureInfo(l));

                    var previewModel = new PreviewImportResourcesViewModel(changes, showMenu ?? false, changedLanguages);

                    return(View("ImportPreview", previewModel));
                }

                var result = workflow.Import(parseResult.Resources, previewImport ?? true);
                ViewData["LocalizationProvider_ImportResult"] = result;
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }
Example #2
0
        public ViewResult ImportResources(bool?previewImport, HttpPostedFileBase importFile, bool?showMenu)
        {
            var model = new ImportResourcesViewModel {
                ShowMenu = showMenu ?? false
            };

            if (importFile == null || importFile.ContentLength == 0)
            {
                return(View("ImportResources", model));
            }

            var fileInfo = new FileInfo(importFile.FileName);

            if (fileInfo.Extension.ToLower() != ".json")
            {
                ModelState.AddModelError("file", "Uploaded file has different extension. Json file expected");
                return(View("ImportResources", model));
            }

            var importer     = new ResourceImporter();
            var serializer   = new JsonDataSerializer();
            var streamReader = new StreamReader(importFile.InputStream);
            var fileContent  = streamReader.ReadToEnd();

            try
            {
                var newResources = serializer.Deserialize <IEnumerable <LocalizationResource> >(fileContent);

                if (previewImport.HasValue && previewImport.Value)
                {
                    var changes = importer.DetectChanges(newResources, new GetAllResources.Query().Execute());

                    var availableLanguagesQuery = new AvailableLanguages.Query();
                    var languages = availableLanguagesQuery.Execute();

                    var previewModel = new PreviewImportResourcesViewModel(changes, showMenu ?? false, languages);

                    return(View("ImportPreview", previewModel));
                }

                var result = importer.Import(newResources, previewImport ?? true);

                ViewData["LocalizationProvider_ImportResult"] = result;
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }