Esempio n. 1
0
        public async Task <BackupResponseModel> CreateBackupFile(BackupRequestModel model)
        {
            Directory.CreateDirectory(_backupOptions.CurrentValue.BackupFolderPath);
            // Ensure a total number of objects less than MaximumObjects
            var appCount = model.Apps != null?model.Apps.Count() : 0;

            var standardCount = model.Standards != null?model.Standards.Count() : 0;

            var treeCount = model.Tree != null?model.Tree.Count() : 0;

            var arrayCount = model.Array != null?model.Array.Count() : 0;

            var chartCount = model.Charts != null?model.Charts.Count() : 0;

            var dynamicCount = model.DynamicLists != null?model.DynamicLists.Count() : 0;

            var databaseCount = model.Databases != null?model.Databases.Count() : 0;

            var pageCount = model.Pages != null?model.Pages.Count() : 0;

            var compositeControlCount = model.CompositeControls != null?model.CompositeControls.Count() : 0;

            var totalBackupCount =
                appCount
                + standardCount
                + treeCount
                + arrayCount
                + chartCount
                + dynamicCount
                + databaseCount
                + pageCount
                + compositeControlCount;

            if (totalBackupCount > _backupOptions.CurrentValue.MaximumObjects)
            {
                throw new BackupException(BackupErrorCodes.ReachMaximumBackupObjects);
            }
            var collectApp               = _appServiceProvider.GetAppsByIds(model.Apps);
            var collectStandards         = _standardServiceProvider.GetStandardComponentsByIds(model.Standards);
            var collectTree              = _standardServiceProvider.GetStandardComponentsByIds(model.Tree);
            var collectArray             = _standardServiceProvider.GetStandardComponentsByIds(model.Array);
            var collectCharts            = _chartServiceProvider.GetChartsByIds(model.Charts);
            var collectDynamicLists      = _dynamicListServiceProvider.GetDynamicListsByIds(model.DynamicLists);
            var collectDatabases         = _databaseServiceProvider.GetDatabaseConnectionsByIds(model.Databases);
            var collectPages             = _pageServiceProvider.GetPagesByIds(model.Pages);
            var collectCompositeControls = _compositeControlServiceProvider.GetByIds(model.CompositeControls);

            await Task.WhenAll(
                collectApp,
                collectStandards,
                collectTree,
                collectArray,
                collectCharts,
                collectDatabases,
                collectDynamicLists,
                collectPages,
                collectCompositeControls);

            var backupFileModel = new BackupFlatternFileModel
            {
                TotalObjects  = totalBackupCount,
                ChainingFiles = new List <string>()
            };

            var backup = new Backup
            {
                Id             = DataUtil.GenerateUniqueId(),
                Name           = model.Name,
                Description    = model.Description,
                CreatedDate    = DateTime.UtcNow,
                Creator        = model.Creator,
                BackupElements = new BackupElements
                {
                    Apps              = model.Apps,
                    Charts            = model.Charts,
                    Databases         = model.Databases,
                    DynamicLists      = model.DynamicLists,
                    Pages             = model.Pages,
                    Standards         = model.Standards,
                    Array             = model.Array,
                    Tree              = model.Tree,
                    CompositeControls = model.CompositeControls
                }
            };

            backupFileModel.Backup = backup;

            // Write to file
            var fileName     = DateTime.UtcNow.Ticks.ToString();
            var jsonFilePath = !string.IsNullOrEmpty(_backupOptions.CurrentValue.BackupFolderPath) ? _backupOptions.CurrentValue.BackupFolderPath : Environment.CurrentDirectory;
            var jsonFileName = fileName + ".json";

            jsonFilePath = Path.Combine(jsonFilePath, fileName);
            Directory.CreateDirectory(jsonFilePath);

            if (collectApp.Result != null)
            {
                var jsonApps = ConvertUtil.SerializeObject(collectApp.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, APP_FILE)))
                {
                    sw.Write(jsonApps);
                }

                backupFileModel.ChainingFiles.Add(APP_FILE);
            }

            if (collectStandards.Result != null)
            {
                var jsonStandards = ConvertUtil.SerializeObject(collectStandards.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, STANDARD_FILE)))
                {
                    sw.Write(jsonStandards);
                }
                backupFileModel.ChainingFiles.Add(STANDARD_FILE);
            }

            if (collectTree.Result != null)
            {
                var jsonTree = ConvertUtil.SerializeObject(collectTree.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, TREE_FILE)))
                {
                    sw.Write(jsonTree);
                }
                backupFileModel.ChainingFiles.Add(TREE_FILE);
            }

            if (collectArray.Result != null)
            {
                var jsonArray = ConvertUtil.SerializeObject(collectArray.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, ARRAY_FILE)))
                {
                    sw.Write(jsonArray);
                }
                backupFileModel.ChainingFiles.Add(ARRAY_FILE);
            }

            if (collectDynamicLists.Result != null)
            {
                var jsonDynamicLists = ConvertUtil.SerializeObject(collectDynamicLists.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, DYNAMICLIST_FILE)))
                {
                    sw.Write(jsonDynamicLists);
                }
                backupFileModel.ChainingFiles.Add(DYNAMICLIST_FILE);
            }

            if (collectDatabases.Result != null)
            {
                var jsonDatabases = ConvertUtil.SerializeObject(collectDatabases.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, DATABASE_FILE)))
                {
                    sw.Write(jsonDatabases);
                }
                backupFileModel.ChainingFiles.Add(DATABASE_FILE);
            }

            if (collectCharts.Result != null)
            {
                var jsonCharts = ConvertUtil.SerializeObject(collectCharts.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, CHART_FILE)))
                {
                    sw.Write(jsonCharts);
                }
                backupFileModel.ChainingFiles.Add(CHART_FILE);
            }

            if (collectPages.Result != null)
            {
                var jsonPages = ConvertUtil.SerializeObject(collectPages.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, PAGE_FILE)))
                {
                    sw.Write(jsonPages);
                }
                backupFileModel.ChainingFiles.Add(PAGE_FILE);
            }

            if (collectCompositeControls.Result != null)
            {
                var jsonPages = ConvertUtil.SerializeObject(collectCompositeControls.Result, true);
                using (var sw = new StreamWriter(
                           Path.Combine(jsonFilePath, COMPOSITE_CONTROL_FILE)))
                {
                    sw.Write(jsonPages);
                }
                backupFileModel.ChainingFiles.Add(COMPOSITE_CONTROL_FILE);
            }

            var jsonFlattern = ConvertUtil.SerializeObject(backupFileModel, true);

            using (var sw = new StreamWriter(Path.Combine(jsonFilePath, jsonFileName)))
            {
                sw.Write(jsonFlattern);
            }

            ZipFile.CreateFromDirectory(jsonFilePath, Path.Combine(_backupOptions.CurrentValue.BackupFolderPath, fileName + ".zip"));

            // Store zip file into file server, allow to create zip file when downloading
            var uploadResponse = await _fileSeviceProvider
                                 .UploadFileAsync(
                Path.Combine(_backupOptions.CurrentValue.BackupFolderPath, fileName + ".zip"),
                model.Creator,
                true);

            backup.FileId          = uploadResponse.FileId;
            backup.DownloadableUrl = uploadResponse.DownloadableUrl;
            await _backupRepository.AddAsync(backup);

            return(new BackupResponseModel {
                DownloadableUrl = uploadResponse.DownloadableUrl
            });
        }