firedigger

Scoped

Jun 15th, 2023
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.95 KB | Source Code | 0 0
  1. [Microsoft.AspNetCore.Mvc.HttpPost, Microsoft.AspNetCore.Mvc.Route("restore/sharepoint/azure-blob-storage/o365")]
  2.         [Authorize(Roles = "O365User,O365Admin,NexeticAdmin")]
  3.         public override Task<bool> PostRestoreSharepointAzureBlobStorageO365([FromQuery] int? userId, [FromQuery] bool? toRestoreFolder, [FromQuery] bool? fromTeams, [FromBody] IEnumerable<int> body)
  4.         {
  5.             VerifyUsersOrganization(userId.Value);
  6.             return TaskUtility.Run<bool>(() =>
  7.             {
  8.                 try
  9.                 {
  10.                     List<int> itemsIds = body.ToList();
  11.                     if (userId.HasValue && itemsIds.Count > 0 && fromTeams.HasValue)
  12.                     {
  13.                         User user = _userService.Get(userId.Value);
  14.                         if (user != null)
  15.                         {
  16.                             List<SharepointListItemVersion> finalList = new List<SharepointListItemVersion>();
  17.  
  18.                             foreach (int itemId in itemsIds)
  19.                             {
  20.                                 SharepointListItemVersion version = _sharePointService.GetListItemVersionById(itemId);
  21.  
  22.                                 if (version != null)
  23.                                 {
  24.                                     OneDriveItem oneDriveItem = _sharePointService.GetDriveItemBySharePointItemId(version.SharepointListItemId ?? 0);
  25.  
  26.                                     if (oneDriveItem != null)
  27.                                     {
  28.                                         if (oneDriveItem.IsFolder)
  29.                                         {
  30.                                             OneDriveItemVersion oneDriveItemVersion = _sharePointService.GetDriveItemVersionByDriveItemId(oneDriveItem.Id);
  31.                                             if (oneDriveItemVersion != null)
  32.                                             {
  33.                                                 List<OneDriveItem> childDriveItems = _driveService.GetByParentPath(oneDriveItemVersion.Path, -1, fromTeams.Value, true);
  34.                                                 if (childDriveItems != null && childDriveItems.Count > 0)
  35.                                                 {
  36.                                                     foreach (OneDriveItem childItem in childDriveItems)
  37.                                                     {
  38.                                                         SharepointListItem toAddItem = _sharePointService.GetSharePointItemByDriveId(childItem.Id);
  39.                                                         if (toAddItem != null)
  40.                                                         {
  41.                                                             SharepointListItemVersion versionToAdd = _sharePointService.GetLastVersionByItemId(toAddItem.Id);
  42.                                                             if (versionToAdd != null)
  43.                                                             {
  44.                                                                 if (!finalList.Contains(versionToAdd))
  45.                                                                 {
  46.                                                                     finalList.Add(versionToAdd);
  47.                                                                 }
  48.                                                             }
  49.                                                         }
  50.                                                     }
  51.                                                 }
  52.                                             }
  53.                                         }
  54.                                         else
  55.                                         {
  56.                                             if (!finalList.Contains(version))
  57.                                             {
  58.                                                 finalList.Add(version);
  59.                                             }
  60.                                         }
  61.                                     }
  62.                                 }
  63.                             }
  64.  
  65.                             if (finalList.Count > 0)
  66.                             {
  67.                                 List<string> selectedIds = finalList.Select(x => x.Id + "from" + x.InitialExternalId).ToList();
  68.  
  69.                                 _backupRestoreService.RestoreSharePoint(
  70.                                     user.ExternalServiceOrganizationId,
  71.                                     toRestoreFolder ?? false,
  72.                                     selectedIds
  73.                                 );
  74.                                 return true;
  75.                             }
  76.                         }
  77.                     }
  78.                     return false;
  79.                 }
  80.                 catch (Exception ex)
  81.                 {
  82.                     _logger.LogError($"Error initiating sharepoint restore: {ex.ToString()}");
  83.                     return false;
  84.                 }
  85.             });
  86.         }
Advertisement
Add Comment
Please, Sign In to add comment