/// <summary> /// Determines whether <paramref name="destFolder"/> is inside this folder. /// </summary> /// <param name="destFolder">Folder to check.</param> /// <returns>Returns <c>true</c> if <paramref name="destFolder"/> is inside thid folder.</returns> private bool IsRecursive(DavFolder destFolder) { return(destFolder.Path.StartsWith(Path)); }
/// <summary> /// Called when this folder is being moved or renamed. /// </summary> /// <param name="destFolder">Destination folder.</param> /// <param name="destName">New name of this folder.</param> /// <param name="multistatus">Information about child items that failed to move.</param> public override async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { await RequireHasTokenAsync(); if (!(destFolder is DavFolder)) { throw new DavException("Target folder doesn't exist", DavStatus.CONFLICT); } DavFolder targetFolder = (DavFolder)destFolder; if (IsRecursive(targetFolder)) { throw new DavException("Cannot move folder to its subtree.", DavStatus.FORBIDDEN); } // string newDirPath = System.IO.Path.Combine(targetFolder.FullPath, destName); string targetPath = targetFolder.Path + EncodeUtil.EncodeUrlPart(destName); // try { // Remove item with the same name at destination if it exists. IHierarchyItemAsync item = await context.GetHierarchyItemAsync(targetPath); if (item != null) { await item.DeleteAsync(multistatus); } await targetFolder.CreateFolderAsync(destName); } catch (DavException ex) { // Continue the operation but report error with destination path to client. multistatus.AddInnerException(targetPath, ex); return; } // Move child items. bool movedSuccessfully = true; IFolderAsync createdFolder = (IFolderAsync)await context.GetHierarchyItemAsync(targetPath); foreach (DavHierarchyItem item in (await GetChildrenAsync(new PropertyName[0], null, null, new List <OrderProperty>())).Page) { try { await item.MoveToAsync(createdFolder, item.Name, multistatus); } catch (DavException ex) { // Continue the operation but report error with child item to client. multistatus.AddInnerException(item.Path, ex); movedSuccessfully = false; } } if (movedSuccessfully) { await DeleteAsync(multistatus); } // Refresh client UI. await context.socketService.NotifyDeleteAsync(Path); await context.socketService.NotifyRefreshAsync(GetParentPath(targetPath)); }