/// <summary> /// Move this folder to folder <paramref name="destFolder"/>. /// </summary> /// <param name="destFolder">Destination folder.</param> /// <param name="destName">Name for this folder at destination.</param> /// <param name="multistatus">Instance of <see cref="MultistatusException"/> /// to fill with errors ocurred while moving child items.</param> /// <returns></returns> public override async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { // Here we support only addressbooks renaming. Check that user has permissions to write. string sql = @"UPDATE [card_AddressbookFolder] SET Name=@Name WHERE [AddressbookFolderId]=@AddressbookFolderId AND [AddressbookFolderId] IN (SELECT [AddressbookFolderId] FROM [card_Access] WHERE [UserId]=@UserId AND [Write] = 1)"; if (await Context.ExecuteNonQueryAsync(sql, "@Name", destName , "@UserId", Context.UserId , "@AddressbookFolderId", addressbookFolderId) < 1) { throw new DavException("Item not found or you do not have enough permissions to complete this operation.", DavStatus.FORBIDDEN); } }
/// <summary> /// Renames principal. /// </summary> /// <param name="destFolder">We don't use it as moving groups to different folder is not supported.</param> /// <param name="destName">New name.</param> /// <param name="multistatus">We don't use it as there're no child objects.</param> public async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { if (destFolder.Path != parentPath) { throw new DavException("Moving principals is only allowed into the same folder", DavStatus.CONFLICT); } if (!IsValidUserName(destName)) { throw new DavException("Principal name contains invalid characters", DavStatus.FORBIDDEN); } Context.PrincipalOperation( () => ((DirectoryEntry)Principal.GetUnderlyingObject()).Rename(destName)); }
/// <summary> /// Called when this file is being moved or renamed. /// </summary> /// <param name="destFolder">Destination folder.</param> /// <param name="destName">New name of this file.</param> /// <param name="multistatus">Information about items that failed to move.</param> public override async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { DavFolder targetFolder = (DavFolder)destFolder; if (targetFolder == null || !Directory.Exists(targetFolder.FullPath)) { throw new DavException("Target directory doesn't exist", DavStatus.CONFLICT); } string newDirPath = System.IO.Path.Combine(targetFolder.FullPath, destName); string targetPath = targetFolder.Path + EncodeUtil.EncodeUrlPart(destName); // If an item with the same name exists in target directory - remove it. try { IHierarchyItemAsync item = await context.GetHierarchyItemAsync(targetPath); if (item != null) { await item.DeleteAsync(multistatus); } } catch (DavException ex) { // Report exception to client and continue with other items by returning from recursion. multistatus.AddInnerException(targetPath, ex); return; } // Move the file. try { File.Move(fileSystemInfo.FullName, newDirPath); } catch (UnauthorizedAccessException) { // Exception occurred with the item for which MoveTo was called - fail the operation. NeedPrivilegesException ex = new NeedPrivilegesException("Not enough privileges"); ex.AddRequiredPrivilege(targetPath, Privilege.Bind); string parentPath = System.IO.Path.GetDirectoryName(Path); ex.AddRequiredPrivilege(parentPath, Privilege.Unbind); throw ex; } }
/// <summary> /// Moves this file to different folder and renames it. /// </summary> /// <param name="destFolder">Destination folder.</param> /// <param name="destName">New file name.</param> /// <param name="multistatus">Container for errors with items other than this file.</param> public override async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { DavFolder destDavFolder = destFolder as DavFolder; if (destFolder == null) { throw new DavException("Destination folder doesn't exist.", DavStatus.CONFLICT); } DavFolder parent = await GetParentAsync(); if (parent == null) { throw new DavException("Cannot move root.", DavStatus.CONFLICT); } if (!await ClientHasTokenAsync() || !await destDavFolder.ClientHasTokenAsync() || !await parent.ClientHasTokenAsync()) { throw new LockedException(); } DavHierarchyItem destItem = await destDavFolder.FindChildAsync(destName); if (destItem != null) { try { await destItem.DeleteAsync(multistatus); } catch (DavException ex) { multistatus.AddInnerException(destItem.Path, ex); return; } } await MoveThisItemAsync(destDavFolder, destName, parent); // Refresh client UI. await Context.socketService.NotifyRefreshAsync(parent.Path); await Context.socketService.NotifyRefreshAsync(destDavFolder.Path); }
/// <summary> /// Creates new user as copy of this one. /// </summary> /// <param name="destFolder">Is not used as there's no more locations a user can be copied.</param> /// <param name="destName">New user name.</param> /// <param name="deep">Whether to copy children - is not user.</param> /// <param name="multistatus">Is not used as there's no children.</param> public override async Task CopyToAsync(IItemCollectionAsync destFolder, string destName, bool deep, MultistatusException multistatus) { if (destFolder.Path != new UserFolder(Context).Path) { throw new DavException("Copying users is only allowed into the same folder", DavStatus.CONFLICT); } if (!IsValidUserName(destName)) { throw new DavException("User name contains invalid characters", DavStatus.FORBIDDEN); } UserPrincipal newUser = new UserPrincipal(userPrincipal.Context) { Name = destName, Description = userPrincipal.Description }; Context.PrincipalOperation(newUser.Save); }
/// <summary> /// Copies this file to another folder. /// </summary> /// <param name="destFolder">Destination folder.</param> /// <param name="destName">New file name in destination folder.</param> /// <param name="deep">Is not used.</param> /// <param name="multistatus">Container for errors with items other than this file.</param> public override async Task CopyToAsync( IItemCollectionAsync destFolder, string destName, bool deep, MultistatusException multistatus) { DavFolder destDavFolder = destFolder as DavFolder; if (destFolder == null) { throw new DavException("Destination folder doesn't exist.", DavStatus.CONFLICT); } if (!await destDavFolder.ClientHasTokenAsync()) { throw new LockedException("Doesn't have token for destination folder."); } DavHierarchyItem destItem = await destDavFolder.FindChildAsync(destName); if (destItem != null) { try { await destItem.DeleteAsync(multistatus); } catch (DavException ex) { multistatus.AddInnerException(destItem.Path, ex); return; } } await CopyThisItemAsync(destDavFolder, null, destName); await Context.socketService.NotifyRefreshAsync(destDavFolder.Path); }
/// <summary> /// Called when this file is being moved or renamed. /// </summary> /// <param name="destFolder">Destination folder.</param> /// <param name="destName">New name of this file.</param> /// <param name="multistatus">Information about items that failed to move.</param> public override async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { await RequireHasTokenAsync(); DavFolder targetFolder = (DavFolder)destFolder; if (!await context.DataLakeStoreService.ExistsAsync(targetFolder.Path)) { throw new DavException("Target directory doesn't exist", DavStatus.CONFLICT); } string targetPath = targetFolder.Path + EncodeUtil.EncodeUrlPart(destName); // If an item with the same name exists in target directory - remove it. try { if (await context.GetHierarchyItemAsync(targetPath) is { } item) { await item.DeleteAsync(multistatus); } } catch (DavException ex) { // Report exception to client and continue with other items by returning from recursion. multistatus.AddInnerException(targetPath, ex); return; } await context.DataLakeStoreService.CopyItemAsync(Path, targetFolder.Path, destName, ContentLength, dataCloudItem.Properties); await DeleteAsync(multistatus); // Refresh client UI. await context.socketService.NotifyRefreshAsync(GetParentPath(Path)); await context.socketService.NotifyRefreshAsync(targetFolder.Path); }
/// <summary> /// Moves this item to the destination folder under a new name. /// </summary> /// <param name="destFolder">Destination folder.</param> /// <param name="destName">Name of the destination item.</param> /// <param name="multistatus">If some items fail to copy but operation in whole shall be continued, add /// information about the error into <paramref name="multistatus"/> using /// <see cref="MultistatusException.AddInnerException(string,ITHit.WebDAV.Server.DavException)"/>. /// </param> public abstract Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus);
/// <summary> /// Renames principal. /// </summary> /// <param name="destFolder">We don't use it as moving groups to different folder is not supported.</param> /// <param name="destName">New name.</param> /// <param name="multistatus">We don't use it as there're no child objects.</param> public async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { throw new DavException("Not implemented.", DavStatus.NOT_IMPLEMENTED); }
/// <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(); DavFolder targetFolder = destFolder as DavFolder; if (targetFolder == null) { throw new DavException("Target folder doesn't exist", DavStatus.CONFLICT); } 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])) { 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)); }
/* If required you can appy some rules, for example prohibit creating files in this folder * * /// <summary> * /// Prohibit creating files in this folder. * /// </summary> * override public async Task<IFileAsync> CreateFileAsync(string name) * { * throw new DavException("Creating files in this folder is not implemented.", DavStatus.NOT_IMPLEMENTED); * } * * /// <summary> * /// Prohibit creating folders via WebDAV in this folder. * /// </summary> * /// <remarks> * /// New user folders are created during first log-in. * /// </remarks> * override public async Task CreateFolderAsync(string name) * { * throw new DavException("Creating sub-folders in this folder is not implemented.", DavStatus.NOT_IMPLEMENTED); * } * * /// <summary> * /// Prohibit copying this folder. * /// </summary> * override public async Task CopyToAsync(IItemCollection destFolder, string destName, bool deep, MultistatusException multistatus) * { * throw new DavException("Copying this folder is not allowed.", DavStatus.NOT_ALLOWED); * } */ /// <summary> /// Prohibit moving or renaming this folder /// </summary> override public async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { throw new DavException("Moving or renaming this folder is not allowed.", DavStatus.NOT_ALLOWED); }
/// <summary> /// Called when this file is being moved or renamed. /// </summary> /// <param name="destFolder">Destination folder.</param> /// <param name="destName">New name of this file.</param> /// <param name="multistatus">Information about items that failed to move.</param> public override async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { await RequireHasTokenAsync(); DavFolder targetFolder = (DavFolder)destFolder; if (targetFolder == null || !Directory.Exists(targetFolder.FullPath)) { throw new DavException("Target directory doesn't exist", DavStatus.CONFLICT); } string newDirPath = System.IO.Path.Combine(targetFolder.FullPath, destName); string targetPath = targetFolder.Path + EncodeUtil.EncodeUrlPart(destName); // If an item with the same name exists in target directory - remove it. try { IHierarchyItemAsync item = await context.GetHierarchyItemAsync(targetPath) as IHierarchyItemAsync; if (item != null) { await item.DeleteAsync(multistatus); } } catch (DavException ex) { // Report exception to client and continue with other items by returning from recursion. multistatus.AddInnerException(targetPath, ex); return; } // Move the file. try { File.Move(fileSystemInfo.FullName, newDirPath); FileInfo newFileInfo = new FileInfo(newDirPath); if (FileSystemInfoExtension.IsUsingFileSystemAttribute) { await fileSystemInfo.MoveExtendedAttributes(newFileInfo); } // Locks should not be copied, delete them. if (await newFileInfo.HasExtendedAttributeAsync("Locks")) { await newFileInfo.DeleteExtendedAttributeAsync("Locks"); } } catch (UnauthorizedAccessException) { // Exception occurred with the item for which MoveTo was called - fail the operation. NeedPrivilegesException ex = new NeedPrivilegesException("Not enough privileges"); ex.AddRequiredPrivilege(targetPath, Privilege.Bind); string parentPath = System.IO.Path.GetDirectoryName(Path); ex.AddRequiredPrivilege(parentPath, Privilege.Unbind); throw ex; } // Refresh client UI. await context.socketService.NotifyRefreshAsync(GetParentPath(Path)); await context.socketService.NotifyRefreshAsync(targetFolder.Path); }
/// <summary> /// Moves this folder to destination folder with option to rename. /// </summary> /// <param name="destFolder">Folder to copy this folder to.</param> /// <param name="destName">New name of this folder.</param> /// <param name="multistatus">Container for errors. We put here errors occurring while moving /// individual files/folders.</param> public override async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { DavFolder destDavFolder = destFolder as DavFolder; if (destFolder == null) { throw new DavException("Destination folder doesn't exist", DavStatus.CONFLICT); } if (isRecursive(destDavFolder)) { throw new DavException("Cannot move folder to its subtree", DavStatus.FORBIDDEN); } DavFolder parent = await GetParentAsync(); if (parent == null) { throw new DavException("Cannot move root", DavStatus.CONFLICT); } if (!await ClientHasTokenAsync() || !await destDavFolder.ClientHasTokenAsync() || !await parent.ClientHasTokenAsync()) { throw new LockedException(); } DavHierarchyItem destItem = await destDavFolder.FindChildAsync(destName); DavFolder newDestFolder; // copy this folder if (destItem != null) { if (destItem is IFileAsync) { try { await destItem.DeleteAsync(multistatus); } catch (DavException ex) { multistatus.AddInnerException(destItem.Path, ex); return; } newDestFolder = await CopyThisItemAsync(destDavFolder, null, destName); } else { newDestFolder = destItem as DavFolder; if (newDestFolder == null) { multistatus.AddInnerException( destItem.Path, new DavException("Destionation item is not folder", DavStatus.CONFLICT)); } } } else { newDestFolder = await CopyThisItemAsync(destDavFolder, null, destName); } // move children bool movedAllChildren = true; foreach (IHierarchyItemAsync child in (await GetChildrenAsync(new PropertyName[0], null, null, null)).Page) { DavHierarchyItem dbchild = child as DavHierarchyItem; try { await dbchild.MoveToAsync(newDestFolder, child.Name, multistatus); } catch (DavException ex) { multistatus.AddInnerException(dbchild.Path, ex); movedAllChildren = false; } } if (movedAllChildren) { await DeleteThisItemAsync(parent); } // Refresh client UI. await Context.socketService.NotifyDeleteAsync(Path); await Context.socketService.NotifyRefreshAsync(GetParentPath(newDestFolder.Path)); }
public async Task MoveToAsync(IItemCollectionAsync destFolder, string destName, MultistatusException multistatus) { throw new NotImplementedException(); }