/// <summary> /// Determines whether the currently logged on user has edit permissions on the specific item. /// </summary> /// <param name="item">The item.</param> /// <returns> /// <c>true</c> if the currently logged on user has edit permissions on the specific item; otherwise, <c>false</c>. /// </returns> public static bool CanEdit(VirtualFileBase item) { if (item == null) { return(false); } UnifiedDirectory dir = item as UnifiedDirectory; if (dir != null) { return(PrincipalInfo.HasAdminAccess || dir.QueryDistinctAccess(AccessLevel.Edit)); } UnifiedFile file = item as UnifiedFile; if (file != null && (PrincipalInfo.HasAdminAccess || file.QueryDistinctAccess(AccessLevel.Edit))) { IVersioningFile versioningFile = file as IVersioningFile; return(versioningFile == null || !IsCheckedOutBySomeoneElse(versioningFile)); } else { return(false); } }
/// <summary> /// Creates a temporary file in the supplied directory and writes stream content to file. /// </summary> /// <param name="baseDirectory">The directory where the temporary file is created.</param> /// <param name="fileContent">Content of the file.</param> /// <returns>A Unified File</returns> public static UnifiedFile CreateTempFile(UnifiedDirectory baseDirectory, Stream fileContent) { UnifiedFile file = CreateTempFile(baseDirectory); WriteToFile(fileContent, file, null); return(file); }
/// <summary> /// Copies a file to the target folder, optionally with a new file name. /// </summary> /// <param name="sourceFile">The source file.</param> /// <param name="targetDirectory">The target directory.</param> /// <param name="newName">The new name. Supply <c>null</c> to use the name of the source file.</param> public static void CopyFile(VirtualFile sourceFile, UnifiedDirectory targetDirectory, string newName) { if (String.IsNullOrEmpty(newName)) { newName = sourceFile.Name; } string targetVirtualFilePath = VirtualPathUtility.Combine(targetDirectory.VirtualPath, newName); targetVirtualFilePath = FindAvailableVirtualPath(targetVirtualFilePath); UnifiedFile unifiedSourceFile = sourceFile as UnifiedFile; if (unifiedSourceFile != null) { // If the source file is a UnifiedFile use its copy implementation unifiedSourceFile.CopyTo(targetVirtualFilePath); } else { // If the source is unknown. i.e. implementing VirtualFile but not UnifiedFile. // Use the support methods of UnifiedFile to copy the source VirtualFile. UnifiedFile.CopyTo(sourceFile, targetVirtualFilePath); } //TODO: Implement copying of VersioningFile to a non-versioning provider. // The versioning providers overrides the [Copy/Move]To methods of UnifiedFile, // but throws an exception if trying to move or copy a file FROM a versioning // provider to another provider }
/// <summary> /// Deletes the items supplied in the <see cref="VirtualFileBaseCollection"/>. /// </summary> /// <param name="items">The items to delete.</param> public static void DeleteItems(VirtualFileBaseCollection items) { //Can only delete items inheriting from Unified[Directory/File] foreach (VirtualFileBase fileItem in items) { if (CanEdit(fileItem)) { UnifiedDirectory directory = fileItem as UnifiedDirectory; if (directory != null) { directory.Delete(); continue; } UnifiedFile file = fileItem as UnifiedFile; if (file != null) { file.Delete(); continue; } } else { //TODO: What do we do with delete operation on unknown items. // Throw exception? Silently ignore? Logging! } } }
/// <summary> /// Moves a virtual file to a new directory, optioanlly renaming it in the process. /// </summary> /// <param name="sourceFile">The source file to be moved.</param> /// <param name="targetDirectory">The destination directory.</param> /// <param name="newName">An optional new name. Supply <c>null</c> to use the original name of the source file.</param> public static void MoveFile(VirtualFile sourceFile, UnifiedDirectory targetDirectory, string newName) { if (String.IsNullOrEmpty(newName)) { newName = sourceFile.Name; } string targetVirtualFilePath = VirtualPathUtility.Combine(targetDirectory.VirtualPath, newName); UnifiedFile unifiedSourceFile = sourceFile as UnifiedFile; if (unifiedSourceFile != null) { IVersioningFile versioningFile = sourceFile as IVersioningFile; if (versioningFile != null) { ThrowIfCheckedOut(versioningFile); } if (!IsPathAvailable(targetVirtualFilePath)) { throw new ArgumentException("Moving a file with the same name as a file or a folder in the target directory is not allowed"); } unifiedSourceFile.MoveTo(targetVirtualFilePath); } // Can't move a file unless its a UnifiedFile. VirtualFile does not specify any remove methods. }
/// <summary> /// Creates a file using the contents of the supplied stream. /// </summary> /// <param name="directory">The directory in which to create the file.</param> /// <param name="fileName">Name of the file.</param> /// <param name="checkInComment">An optional check in comment.</param> /// <param name="fileContent">Contents of the file.</param> /// <returns>The newly created file.</returns> public static UnifiedFile CreateFile(UnifiedDirectory directory, string fileName, string checkInComment, Stream fileContent) { UnifiedFile file = directory.CreateFile(fileName); WriteToFile(fileContent, file, checkInComment); return(file); }
/// <summary> /// Moves a virtual directopry to a new location, optioanlly renaming it in the process. /// </summary> /// <param name="sourceDirectory">The source directory to be moved.</param> /// <param name="targetDirectory">The destination directory.</param> /// <param name="newName">An optional new name. Supply <c>null</c> to use the original name of the source file.</param> public static void MoveDirectory(VirtualDirectory sourceDirectory, UnifiedDirectory targetDirectory, string newName) { if (String.IsNullOrEmpty(newName)) { newName = sourceDirectory.Name; } string targetVirtualDirectoryPath = VirtualPathUtility.Combine(targetDirectory.VirtualPath, newName); // TODO: Move this logic into UnifiedDirectoey it shouldn't be possible to orphan directories. if (!ValidatePathStructureForMove(sourceDirectory.VirtualPath, targetVirtualDirectoryPath)) { throw new ArgumentException("Moving a folder below itself is not allowed."); } UnifiedDirectory unifiedSourceDirectory = sourceDirectory as UnifiedDirectory; if (unifiedSourceDirectory != null) { if (unifiedSourceDirectory.Parent.VirtualPath != targetDirectory.VirtualPath) { VersioningDirectory versioningDirectory = sourceDirectory as VersioningDirectory; if (versioningDirectory != null) { ThrowIfCheckedOut(versioningDirectory); } } unifiedSourceDirectory.MoveTo(targetVirtualDirectoryPath); } // If source directory isn't a UnifiedDirectory we can't move it. }
private List<VppFiles> ParseFiles(UnifiedDirectory directory) { var vppFiles = new List<VppFiles>(); try { foreach (UnifiedFile file in directory.Files) { var vppFile = new VppFiles { VirtualPath = file.VirtualPath, VppPath = file.LocalPath }; vppFiles.Add(vppFile); } } catch (Exception ex) { Logger.Error(ex); } return vppFiles; }
/// <summary> /// Renames a virtual file or folder. /// </summary> /// <param name="sourceItem">The item to rename.</param> /// <param name="newName">The new name.</param> public static void RenameItem(VirtualFileBase sourceItem, string newName) { // Get the parent directory of the source item. string sourceItemParentPath = VirtualPathUtility.GetDirectory(sourceItem.VirtualPath); UnifiedDirectory targetDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory(sourceItemParentPath) as UnifiedDirectory; MoveItem(sourceItem, targetDirectory, newName); }
/// <summary> /// Finds the last modified files. /// </summary> protected IList <UnifiedFile> FindLastModifiedFiles(string rootFolder, int maxCount) { var files = new List <UnifiedFile>(); UnifiedDirectory directory = null; try { directory = HostingEnvironment.VirtualPathProvider.GetDirectory(rootFolder) as UnifiedDirectory; } catch (UnauthorizedAccessException) {} // If directory not found or access not granted for directory // return empty list if (directory == null) { ShowErrorMessage(Translate("/workroom/filelist/errormessage")); return(files); } UnifiedSearchQuery query = new UnifiedSearchQuery { ModifiedFrom = DateTime.MinValue, ModifiedTo = DateTime.Now, Path = rootFolder, FreeTextQuery = String.Empty }; UnifiedSearchHitCollection hits; try { hits = directory.Search(query); } catch (IOException) { ShowErrorMessage(Translate("/workroom/filelist/errormissingindex")); return(files); } for (int i = 0; i < hits.Count; i++) { try { files.Add((UnifiedFile)HostingEnvironment.VirtualPathProvider.GetFile(hits[i].Path)); } catch (UnauthorizedAccessException) { // If user has limited access right we still want to show files with access granted. } } files.Sort(new FileModifiedComparer()); if (files.Count > maxCount) { files.RemoveRange(maxCount - 1, files.Count - maxCount); } return(files); }
/// <summary> /// Moves a virtual file or folder to a new location, optionally renaming it at the same time. /// </summary> /// <param name="sourceItem">The source item to be moved.</param> /// <param name="targetDirectory">The destination directory.</param> /// <param name="newName">An optional new name. Supply <c>null</c> to use the original name of the source folder.</param> public static void MoveItem(VirtualFileBase sourceItem, UnifiedDirectory targetDirectory, string newName) { if (sourceItem.IsDirectory) { MoveDirectory((VirtualDirectory)sourceItem, targetDirectory, newName); } else { MoveFile((VirtualFile)sourceItem, targetDirectory, newName); } }
/// <summary> /// Copies a virtual file or folder to the target directory. /// </summary> /// <param name="sourceItem">The item to copy.</param> /// <param name="targetDirectory">The target directory.</param> public static void CopyItem(VirtualFileBase sourceItem, UnifiedDirectory targetDirectory) { if (sourceItem.IsDirectory) { CopyDirectory((VirtualDirectory)sourceItem, targetDirectory); } else { CopyFile((VirtualFile)sourceItem, targetDirectory); } }
public AmazonS3File(UnifiedDirectory directory, VirtualPathUnifiedProvider provider, string virtualPath, bool bypassAccessCheck, AmazonS3Object file) : base(directory, provider, virtualPath, bypassAccessCheck) { this._amazon = new AmazonS3Repository(((AmazonS3VirtualPathProvider)this.Provider).AwsAccessKey, ((AmazonS3VirtualPathProvider)this.Provider).AwsSecretKey, ((AmazonS3VirtualPathProvider)this.Provider).BucketName); this._summary = new AmazonS3Summary(); this._virtualPath = virtualPath; if (file != null) { this._file = file; } }
/// <summary> /// Creates the page folder of the workroom start page, if it isn't already created. /// </summary> /// <param name="workroomPage">The workroom start page.</param> private static void CreateFolder(PageData workroomPage) { //We want to allow folder to be created without access checks performed therefore this //construction. Otherwise it would be enough to call CurrentPage.GetPageDirectory(true) UnifiedDirectory pageDirectory = workroomPage.GetPageDirectory(false); if (pageDirectory == null) { UnifiedDirectory rootDirectory = VirtualPathHandler.Instance.GetDirectory(VirtualPathHandler.PageDirectoryRootVirtualPath, true) as UnifiedDirectory; rootDirectory.CreateSubdirectory(workroomPage.Property["PageFolderID"].ToString()); } }
/// <summary> /// Copies a directory to a new location, optionally with a new name. /// </summary> /// <param name="sourceDirectory">The source directory.</param> /// <param name="targetDirectory">The target directory.</param> /// <param name="newName">The optional new name of the directory. Supply <c>null</c> to use the name of the source directory.</param> public static void CopyDirectory(VirtualDirectory sourceDirectory, UnifiedDirectory targetDirectory, string newName) { if (String.IsNullOrEmpty(newName)) { newName = sourceDirectory.Name; } string targetVirtualDirectoryPath = VirtualPathUtility.Combine(targetDirectory.VirtualPath, newName); // TODO: Move this check into VirtualPath implementation. It may be OK depending on the implementation. // Copying a snapshot will work, but a recursive "live" copy operation will fail with infinite recursion. if (!ValidatePathStructureForMove(sourceDirectory.VirtualPath, targetVirtualDirectoryPath)) { throw new ArgumentException("Copying a folder below itself is not allowed"); } // Check if we are pasting a folder to same location it's copied from. if (VirtualPathUtility.GetDirectory(sourceDirectory.VirtualPath) == targetDirectory.VirtualPath) { throw new ArgumentException("Pasting a folder to the same location as the source folder is not allowed"); } if (!IsPathAvailable(targetVirtualDirectoryPath)) { throw new ArgumentException("Pasting a folder with the same name as a file or a folder in the target directory is not allowed"); } UnifiedDirectory unifiedSourceDirectory = sourceDirectory as UnifiedDirectory; if (unifiedSourceDirectory != null) { unifiedSourceDirectory.CopyTo(targetVirtualDirectoryPath); } else { UnifiedDirectory newDirectory = UnifiedDirectory.CreateDirectory(targetVirtualDirectoryPath); foreach (VirtualFileBase childItem in sourceDirectory.Children) { CopyItem(childItem, newDirectory); } } }
/// <summary> /// Appends the cookie crumb links to the control upplied as targetControl. /// </summary> /// <param name="currentDirectory">The current directory.</param> /// <param name="targetControl">The target control to append cookie crumb links to.</param> private void AppendBreadCrumbs(UnifiedDirectory currentDirectory, Control targetControl) { if (currentDirectory == null || FileManager.RootVirtualPath == currentDirectory.VirtualPath) { // Reset the link enumeration when we reach the topmost directory. _linkId = 0; } else { // Append cookie crumb for the parent directory before adding for the current directory. AppendBreadCrumbs(currentDirectory.Parent, targetControl); Literal slash = new Literal(); slash.Text = " / "; targetControl.Controls.Add(slash); } if (currentDirectory == null) { return; } string directoryName = currentDirectory.Name; if (currentDirectory.Parent != null && currentDirectory.Parent.IsFirstLevel) { PageData page = UnifiedDirectory.GetOwnerPageFromVirtualPath(currentDirectory.VirtualPath); if (page != null) { directoryName = page.PageName; } } LinkButton b = new LinkButton(); b.ID = "link" + _linkId++; b.Text = directoryName; b.CausesValidation = false; b.CommandArgument = currentDirectory.VirtualPath; b.CommandName = FileManagerCommandName.SelectFolder; b.Command += new CommandEventHandler(RaiseCommand); targetControl.Controls.Add(b); }
/// <summary> /// Changes the enabled state of buttons based on current file manager state. /// </summary> private void EnableButtons() { Details.Enabled = IsDetailsAllowed(); Version.Enabled = IsVersionAllowed(); UnifiedDirectory currentDirectory = FileManager.CurrentVirtualDirectory; if (currentDirectory != null && currentDirectory.QueryDistinctAccess(AccessLevel.Create | AccessLevel.Edit | AccessLevel.Delete)) { DisabledFunctionsMessage.Visible = false; AddFile.Enabled = IsDefaultView(); AddFolder.Enabled = IsDefaultView(); Rename.Enabled = IsRenameAllowed(); Delete.Enabled = IsChangeAllowed(); Cut.Enabled = IsChangeAllowed(); Copy.Enabled = IsDefaultView() && FileManager.SelectedItems.Count > 0; Paste.Enabled = IsDefaultView() && FileManager.PasteBuffer.Count > 0; Checkout.Enabled = IsCheckOutAllowed(); UndoCheckout.Enabled = IsUndoCheckOutAllowed(); CheckIn.Enabled = IsCheckInAllowed(); } else { DisabledFunctionsMessage.Visible = true; AddFile.Enabled = false; AddFolder.Enabled = false; Rename.Enabled = false; Delete.Enabled = false; Cut.Enabled = false; Copy.Enabled = false; Paste.Enabled = false; Checkout.Enabled = false; UndoCheckout.Enabled = false; CheckIn.Enabled = false; } ApplyDisabledCssClassToDisabledButtons(); }
// TODO: For nice flexibility this should be a wrapper with plug-in provider support. // Since the base provider API doesn't support any modifications at all, we need custom implementations for every modification. // The UnifiedFile and UnifiedDirectory provides abstract API for modifications, but it's likely that the individual inheritors // are incompatible with each other. Meaning that moving a file between different providers are likely to fail. /// <summary> /// Pastes the files in the source collection to the supplied target directory. /// </summary> /// <param name="sourceItems">A collection of source items to paste.</param> /// <param name="targetDirectory">The destination directory.</param> /// <param name="pasteMode"> /// Determines how the source files are treated when the paste operation is performed. /// <list type="table"> /// <listheader> /// <term>pasteMode</term><description>Result</description> /// </listheader> /// <item> /// <term><see cref="PasteMode.Copy"/></term> /// <description>The source items are copied to the new location.</description> /// </item> /// <item> /// <term><see cref="PasteMode.Cut"/></term> /// <description>The source items are moved from their original location to the new location.</description> /// </item> /// </list> /// </param> public static void PasteFiles(VirtualFileBaseCollection sourceItems, UnifiedDirectory targetDirectory, PasteMode pasteMode) { // Have to assume unified file // The Versioning VPP has copy/move support built in, but only for operations inside the Versioning file systems. foreach (VirtualFileBase sourceItem in sourceItems) { try { if (pasteMode == PasteMode.Copy) { CopyItem(sourceItem, targetDirectory); } else if (pasteMode == PasteMode.Cut) { MoveItem(sourceItem, targetDirectory); } } catch (FileIsCheckedOutException) { continue; } } }
private List <VppFiles> ParseFiles(UnifiedDirectory directory) { var vppFiles = new List <VppFiles>(); try { foreach (UnifiedFile file in directory.Files) { var vppFile = new VppFiles { VirtualPath = file.VirtualPath, VppPath = file.LocalPath }; vppFiles.Add(vppFile); } } catch (Exception ex) { Logger.Error(ex); } return(vppFiles); }
public virtual void OnUnifiedFileAdded(UnifiedDirectory sender, UnifiedVirtualPathEventArgs e) { if (UnifiedFileAdded != null) UnifiedFileAdded(sender, e); }
/// <summary> /// Creates a temporary file in the supplied directory. /// </summary> /// <param name="baseDirectory">The directory where the temporary file is created.</param> /// <returns>A Unified File</returns> public static UnifiedFile CreateTempFile(UnifiedDirectory baseDirectory) { string virtualPath = FindAvailableVirtualPath(VirtualPathUtility.Combine(baseDirectory.VirtualPath, TempFileName + TempFileExtension)); return(baseDirectory.CreateFile(VirtualPathUtility.GetFileName(virtualPath))); }
/// <summary> /// Moves a virtual directopry to a new location. /// </summary> /// <param name="sourceDirectory">The source directory to be moved.</param> /// <param name="targetDirectory">The destination directory.</param> public static void MoveDirectory(VirtualDirectory sourceDirectory, UnifiedDirectory targetDirectory) { MoveDirectory(sourceDirectory, targetDirectory, null); }
/// <summary> /// Moves a virtual file to another directory. /// </summary> /// <param name="sourceFile">The source file to be moved.</param> /// <param name="targetDirectory">The destination directory.</param> public static void MoveFile(VirtualFile sourceFile, UnifiedDirectory targetDirectory) { MoveFile(sourceFile, targetDirectory, null); }
private void GetImages(HashSet<string> images, UnifiedDirectory directory) { UnifiedFile[] files = directory.GetFiles(); foreach (UnifiedFile file in files) { if (IsImage(file.Extension)) { images.Add(file.VirtualPath); } } UnifiedDirectory[] subDirectories = directory.GetDirectories(); foreach (UnifiedDirectory subDirectory in subDirectories) { GetImages(images, subDirectory); } }
/// <summary> /// Moves a virtual file or folder to a new location. /// </summary> /// <param name="sourceItem">The source item to be moved.</param> /// <param name="targetDirectory">The destination directory.</param> public static void MoveItem(VirtualFileBase sourceItem, UnifiedDirectory targetDirectory) { MoveItem(sourceItem, targetDirectory, null); }
/// <summary> /// Creates a new directory in the supplied parent directory. /// </summary> /// <param name="parentDirectory">The parent directory in which to create a sub directory.</param> /// <param name="directoryName">Name of the new directory.</param> public static void CreateDirectory(UnifiedDirectory parentDirectory, string directoryName) { parentDirectory.CreateSubdirectory(directoryName); }
public virtual void OnUnifiedDirectoryAdding(UnifiedDirectory sender, UnifiedVirtualPathEventArgs e) { if (UnifiedDirectoryAdding != null) UnifiedDirectoryAdding(sender, e); }