Beispiel #1
0
        public override void ExportData(ExportImportJob exportJob, ExportDto exportDto)
        {
            if (CheckCancelled(exportJob))
            {
                return;
            }
            //Skip the export if all the folders have been processed already.
            if (CheckPoint.Stage >= 1)
            {
                return;
            }

            //Create Zip File to hold files
            var skip                           = GetCurrentSkip();
            var currentIndex                   = skip;
            var totalFolderExported            = 0;
            var totalFolderPermissionsExported = 0;
            var totalFilesExported             = 0;
            var portalId                       = exportJob.PortalId;

            try
            {
                var assetsFile = string.Format(_assetsFolder, exportJob.Directory.TrimEnd('\\').TrimEnd('/'));

                if (CheckPoint.Stage == 0)
                {
                    var fromDate = (exportDto.FromDateUtc ?? Constants.MinDbTime).ToLocalTime();
                    var toDate   = exportDto.ToDateUtc.ToLocalTime();
                    var portal   = PortalController.Instance.GetPortal(portalId);

                    var folders =
                        CBO.FillCollection <ExportFolder>(DataProvider.Instance()
                                                          .GetFolders(portalId, toDate, fromDate)).ToList();
                    var totalFolders = folders.Any() ? folders.Count : 0;
                    folders = folders.Skip(skip).ToList();


                    //Update the total items count in the check points. This should be updated only once.
                    CheckPoint.TotalItems     = CheckPoint.TotalItems <= 0 ? totalFolders : CheckPoint.TotalItems;
                    CheckPoint.ProcessedItems = skip;
                    CheckPoint.Progress       = CheckPoint.TotalItems > 0 ? skip * 100.0 / CheckPoint.TotalItems : 0;
                    if (CheckPointStageCallback(this))
                    {
                        return;
                    }
                    using (var zipArchive = CompressionUtil.OpenCreate(assetsFile))
                    {
                        foreach (var folder in folders)
                        {
                            if (CheckCancelled(exportJob))
                            {
                                break;
                            }
                            var isUserFolder = false;

                            var files =
                                CBO.FillCollection <ExportFile>(
                                    DataProvider.Instance()
                                    .GetFiles(portalId, folder.FolderId, toDate, fromDate)).Where(x => x.Extension != Constants.TemplatesExtension).ToList();
                            int?userId;
                            if (IsUserFolder(folder.FolderPath, out userId))
                            {
                                isUserFolder    = true;
                                folder.UserId   = userId;
                                folder.Username =
                                    UserController.GetUserById(portalId, Convert.ToInt32(userId))?.Username;
                            }
                            if (folder.ParentId != null && folder.ParentId > 0)
                            {
                                //If parent id exists then change the parent folder id to parent id.
                                folder.ParentId =
                                    Repository.GetItem <ExportFolder>(
                                        x => x.FolderId == Convert.ToInt32(folder.ParentId))?.Id;
                            }

                            Repository.CreateItem(folder, null);
                            totalFolderExported++;
                            //Include permissions only if IncludePermissions=true
                            if (exportDto.IncludePermissions)
                            {
                                var permissions =
                                    CBO.FillCollection <ExportFolderPermission>(DataProvider.Instance()
                                                                                .GetFolderPermissionsByPath(portalId, folder.FolderPath, toDate, fromDate));
                                Repository.CreateItems(permissions, folder.Id);
                                totalFolderPermissionsExported += permissions.Count;
                            }
                            Repository.CreateItems(files, folder.Id);
                            totalFilesExported += files.Count;
                            var folderOffset = portal.HomeDirectoryMapPath.Length +
                                               (portal.HomeDirectoryMapPath.EndsWith("\\") ? 0 : 1);

                            if (folder.StorageLocation != (int)FolderController.StorageLocationTypes.DatabaseSecure)
                            {
                                CompressionUtil.AddFilesToArchive(zipArchive, files.Select(file => portal.HomeDirectoryMapPath + folder.FolderPath + GetActualFileName(file)),
                                                                  folderOffset, isUserFolder ? "TempUsers" : null);
                            }
                            CheckPoint.ProcessedItems++;
                            CheckPoint.Progress  = CheckPoint.ProcessedItems * 100.0 / totalFolders;
                            CheckPoint.StageData = null;
                            currentIndex++;
                            //After every 10 items, call the checkpoint stage. This is to avoid too many frequent updates to DB.
                            if (currentIndex % 10 == 0 && CheckPointStageCallback(this))
                            {
                                return;
                            }
                            Repository.RebuildIndex <ExportFolder>(x => x.Id, true);
                            Repository.RebuildIndex <ExportFolder>(x => x.UserId);
                            Repository.RebuildIndex <ExportFile>(x => x.ReferenceId);
                        }
                    }
                    CheckPoint.Completed = true;
                    CheckPoint.Stage++;
                    currentIndex        = 0;
                    CheckPoint.Progress = 100;
                }
            }
            finally
            {
                CheckPoint.StageData = currentIndex > 0 ? JsonConvert.SerializeObject(new { skip = currentIndex }) : null;
                CheckPointStageCallback(this);
                Result.AddSummary("Exported Folders", totalFolderExported.ToString());
                Result.AddSummary("Exported Folder Permissions", totalFolderPermissionsExported.ToString());
                Result.AddSummary("Exported Files", totalFilesExported.ToString());
            }
        }