/// <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> /// Resets the upload form to the initial state. /// </summary> private void UpdateForm() { labelExceptionMessage.Visible = false; UploadButton.Visible = UploadButton.Enabled = true; FileUploadPanel.Visible = true; // Show comments panel only for versioning files CommentsPanel.Visible = FileManager.CurrentVirtualDirectory.IsVersioningSupported; CheckInComment.Text = String.Empty; UnifiedFile[] selectedFiles = this.SelectedFiles; if (selectedFiles.Length == 0) { this.UploadButton.Enabled = false; } else { UnifiedFile file = this.SelectedFiles[this.CurrentFileIndex]; IVersioningFile versioningFile = file as IVersioningFile; this.fileCount.Visible = selectedFiles.Length > 1; this.fileCount.Text = "(" + string.Format(Translate("/filemanager/checkinselection/filexofy"), this.CurrentFileIndex + 1, selectedFiles.Length) + ")"; if (versioningFile != null && !FileSystemUtility.IsCheckedOutByCurrentUser(versioningFile)) { this.fileCount.Visible = true; this.fileCount.ForeColor = Color.Red; this.fileCount.Text = Translate("/filemanager/checkinselection/filenotcheckedout"); this.CommentsPanel.Enabled = false; this.CheckInComment.Enabled = false; this.UploadButton.Visible = false; } this.currentFile.Text = file.Name; } }
public int Compare(VirtualFileBase x, VirtualFileBase y) { IVersioningFile xAsFile = x as IVersioningFile; //Check if we are comparing a folder and a file if (x.GetType() != y.GetType()) { _compareValue = (x is VirtualDirectory) ? -1 : 1; } else if (xAsFile != null) { _compareValue = String.Compare(xAsFile.CheckedOutBy, ((IVersioningFile)y).CheckedOutBy, StringComparison.OrdinalIgnoreCase); } else { _compareValue = 0; } if (_direction == SortDirection.Ascending) { _compareValue *= -1; } return(_compareValue); }
/// <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> /// Saves the source stream to a unified file and adds a comment if the file implrments IVersioningFile. /// </summary> /// <param name="sourceStream">The source stream.</param> /// <param name="targetFile">The target file.</param> /// <param name="comment">A comment to add when checking in a versioning file.</param> public static void WriteToFile(Stream sourceStream, UnifiedFile targetFile, string comment) { // It may be a versioning file IVersioningFile versioningFile = targetFile as IVersioningFile; if (versioningFile != null) { ThrowIfCheckedOut(versioningFile); if (!versioningFile.IsCheckedOut) { versioningFile.CheckOut(); } } // Copy the source stream to the target file stream. using (Stream writeStream = targetFile.Open(FileMode.Create, FileAccess.Write)) { StreamConsumer.CopyToEnd(sourceStream, writeStream); } // If versioning, then check in with the supplied comment. if (versioningFile != null) { versioningFile.CheckIn(comment ?? String.Empty); } }
/// <summary> /// Restore a specific file version as the current version. /// </summary> /// <param name="versioningFileItem">A versioning file instance containing the version to restore.</param> /// <param name="versionId">Version id of the version to restore.</param> public static void RestoreVersion(IVersioningFile versioningFileItem, string versionId) { Validate.RequiredParameter("versioningFileItem", versioningFileItem); if (String.IsNullOrEmpty(versionId)) { throw new ArgumentException("versionId can not be null or empty", "versionId"); } ThrowIfCheckedOut(versioningFileItem); IList <UnifiedVersion> versions = versioningFileItem.GetVersions(); if (versions.Count == 1) { throw new InvalidVersioningOperationException("Can not restore version when only one version exists."); } UnifiedVersion targetVersion = versions.SingleOrDefault <UnifiedVersion>(v => v.Id.ToString() == versionId); if (targetVersion == null) { throw new VersionNotFoundException(versionId); } var query = from version in versions orderby version.Changed descending select version; UnifiedVersion lastVersion = query.First <UnifiedVersion>(); if (lastVersion.Id.ToString() == targetVersion.Id.ToString()) { throw new InvalidVersioningOperationException("The version to be restored is already the currently active version."); } targetVersion.Restore(); }
/// <summary> /// Ensures that versioning file can be updated. /// </summary> /// <param name="file">The file.</param> private static void ThrowIfCheckedOut(IVersioningFile file) { Validate.RequiredParameter("file", file); if (IsCheckedOutBySomeoneElse(file)) { throw new FileIsCheckedOutException(file.CheckedOutBy); } }
/// <summary> /// Gets the username for the user the file is checked out to. /// </summary> /// <param name="fileItem">The file item.</param> /// <returns>The user name, or an epmty string if the file is not checked out.</returns> protected static string GetCheckedOutString(VirtualFileBase fileItem) { IVersioningFile versioningFile = fileItem as IVersioningFile; if (versioningFile != null && versioningFile.IsCheckedOut) { return(versioningFile.CheckedOutBy); } return(String.Empty); }
/// <summary> /// Binds the file list to the file repeater control. /// </summary> protected void DataBindVersionList() { IVersioningFile selectedFile = FileManager.SingleSelectedFile as IVersioningFile; if (selectedFile != null) { VersionRepeater.DataSource = GetVersions(selectedFile.GetVersions()); VersionRepeater.DataBind(); _isDataBound = true; } }
/// <summary> /// Determines whether the currently logged on user can undo a check out of the specified file. /// </summary> /// <param name="file">The file.</param> /// <returns> /// <c>true</c> if the user is allowed to undo check out of the specified file; otherwise, <c>false</c>. /// </returns> /// <remarks>Administrators are always allowed to undo a check out files</remarks> public static bool CanUndoCheckOut(VirtualFileBase file) { Validate.RequiredParameter("file", file); IVersioningFile versioningFile = file as IVersioningFile; if (versioningFile != null) { // Admins are allowed to undo other users check outs return((versioningFile.IsCheckedOut && PrincipalInfo.HasAdminAccess) || IsCheckedOutByCurrentUser(versioningFile)); } return(false); }
/// <summary> /// Determines whether the currently logged on user can check out the specified file. /// </summary> /// <param name="file">The file.</param> /// <returns> /// <c>true</c> if the currently logged on user is permitted to check out the specified file; otherwise, <c>false</c>. /// </returns> public static bool CanCheckOut(VirtualFileBase file) { Validate.RequiredParameter("virtualFileBase", file); if (!CanEdit(file)) { return(false); } IVersioningFile versioningFile = file as IVersioningFile; return(versioningFile == null ? false : !versioningFile.IsCheckedOut); }
/// <summary> /// Updates the file summary. /// </summary> /// <param name="file">The file.</param> /// <param name="updatedData">The updated data.</param> public static void UpdateFileSummary(UnifiedFile file, NameValueCollection updatedData) { IVersioningFile versioningFile = file as IVersioningFile; if (versioningFile != null) { ThrowIfCheckedOut(versioningFile); } if (updatedData != null) { IDictionary dict = file.Summary.Dictionary; foreach (string key in updatedData) { dict[key] = updatedData[key]; } } file.Summary.SaveChanges(); }
private bool IsCheckInAllowed() { if (!IsDefaultView() || FileManager.SelectedItems.Count == 0) { return(false); } foreach (var file in FileManager.SelectedItems) { IVersioningFile versioningFile = file as IVersioningFile; // If any selected file isn't a checked out versioned file with edit permissions check in is disabled if (versioningFile == null || !versioningFile.IsCheckedOut || !FileSystemUtility.CanEdit(file)) { return(false); } } return(true); }
/// <summary> /// Raises a command on the current <see cref="FileManager"/>. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param> protected void SelectVersion(object sender, CommandEventArgs e) { string versionId = (string)e.CommandArgument; ButtonDelete.Visible = false; ButtonRestore.Visible = false; IVersioningFile selectedFile = FileManager.SingleSelectedFile as IVersioningFile; if (selectedFile != null) { FileManager.SelectedFileVersionId = versionId; if (FileManager.CurrentVirtualDirectory.QueryDistinctAccess(AccessLevel.Create | AccessLevel.Edit | AccessLevel.Delete)) { ButtonRestore.Visible = IsRestoreVersionEnabled(selectedFile); ButtonDelete.Visible = IsDeleteVersionEnabled(selectedFile); } } DataBindVersionList(); }
/// <summary> /// Determines if a file is checked out by the user currently logged on. /// </summary> /// <param name="file">The file.</param> /// <returns> /// <c>true</c> if file is checked out by the active user; otherwise, <c>false</c>. /// </returns> public static bool IsCheckedOutByCurrentUser(IVersioningFile file) { if (!file.IsCheckedOut || String.IsNullOrEmpty(file.CheckedOutBy)) { return(false); } string checkedOutByName = file.CheckedOutBy; int slashCheckedOutByName = checkedOutByName.IndexOf("\\", StringComparison.Ordinal); string currentUserName = PrincipalInfo.Current.Name; int slashCurrentUserName = currentUserName.IndexOf("\\", StringComparison.Ordinal); if (slashCheckedOutByName > 0 && slashCurrentUserName == -1) { checkedOutByName = checkedOutByName.Substring(slashCheckedOutByName + 1); } if (slashCheckedOutByName == -1 && slashCurrentUserName > 0) { currentUserName = currentUserName.Substring(slashCurrentUserName + 1); } return(String.Equals(checkedOutByName, currentUserName, StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// Deletes a specific file version. /// </summary> /// <param name="versioningFileItem">File version.</param> /// <param name="versionId">File version Id.</param> public static void DeleteVersion(IVersioningFile versioningFileItem, string versionId) { Validate.RequiredParameter("versioningFileItem", versioningFileItem); if (String.IsNullOrEmpty(versionId)) { throw new ArgumentException("versionId can not be null or empty", "versionId"); } ThrowIfCheckedOut(versioningFileItem); IList <UnifiedVersion> versions = versioningFileItem.GetVersions(); if (versions.Count == 1) { throw new InvalidVersioningOperationException(); } UnifiedVersion targetVersion = versions.SingleOrDefault <UnifiedVersion>(v => v.Id.ToString() == versionId); if (targetVersion == null) { throw new VersionNotFoundException(versionId); } targetVersion.Delete(); }
/// <summary> /// Determines whether can perform operations with versions of the specified file. /// </summary> /// <param name="file">The file.</param> /// <returns> /// <c>true</c> if can perform operations with versions of the specified file; otherwise, <c>false</c>. /// </returns> public static bool CanChangeVersions(IVersioningFile file) { Validate.RequiredParameter("file", file); return(file.GetVersions().Count > 1 && CanEdit(file as VirtualFileBase)); }
private bool IsDeleteVersionEnabled(IVersioningFile file) { return(!String.IsNullOrEmpty(FileManager.SelectedFileVersionId) && FileSystemUtility.CanChangeVersions(file)); }
/// <summary> /// Determines whether the file is checked out to another user than the one currently logged on. /// </summary> /// <param name="file">The file.</param> /// <returns> /// <c>true</c> if the versioing file is checked out to another user; otherwise, <c>false</c>. /// </returns> public static bool IsCheckedOutBySomeoneElse(IVersioningFile file) { return(file.IsCheckedOut && !IsCheckedOutByCurrentUser(file)); }
private bool IsRestoreVersionEnabled(IVersioningFile file) { return(!FileManager.SelectedFileVersionId.Equals(CurrentVersion) && FileSystemUtility.CanChangeVersions(file)); }
/// <summary> /// Handles file upload when the UploadFile button is clicked. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> protected void UploadFile_Click(object sender, EventArgs e) { this.labelExceptionMessage.Visible = false; if (string.IsNullOrEmpty(FileUpload.PostedFile.FileName)) { Page.Validators.Add(new StaticValidator(Translate("/filemanager/validation/requiredfile"))); return; } // Abort if there is a folder with the same name as the file if (GenericHostingEnvironment.VirtualPathProvider.DirectoryExists(VirtualPathUtility.Combine(FileManager.CurrentVirtualDirectory.VirtualPath, FileUpload.FileName))) { Page.Validators.Add(new StaticValidator(String.Format(CultureInfo.CurrentCulture, Translate("/filemanager/validation/folderwithsamename"), FileUpload.FileName))); return; } // Check if there already exists a file with the same name VirtualFile existingFile = FileSystemUtility.GetFile(FileManager.CurrentVirtualDirectory, FileUpload.FileName); if (existingFile == null) { // No file found. Let's create a new file. try { FileSystemUtility.CreateFile(FileManager.CurrentVirtualDirectory, FileUpload.FileName, CheckInComment.Text.ToSafeString(), FileUpload.PostedFile.InputStream); ResetUploadForm(); FileManager.LoadDefaultView(this); } catch (InvalidOperationException ex) { this.labelExceptionMessage.Text = ex.Message; this.labelExceptionMessage.Visible = true; } } else { // A file with the same name already exists. IVersioningFile versioningFile = existingFile as IVersioningFile; if (versioningFile != null && FileSystemUtility.IsCheckedOutBySomeoneElse(versioningFile)) { Page.Validators.Add(new StaticValidator(Translate("/filemanager/errormessage/checkedout"))); return; } try { // File already exists; create a temp file for upload and alert the user. UnifiedFile targetFile = FileSystemUtility.CreateTempFile(FileManager.CurrentVirtualDirectory, FileUpload.PostedFile.InputStream); TemporaryFileName = targetFile.Name; DestinationFileName = FileUpload.FileName; } catch (Exception ex) { Page.Validators.Add(new StaticValidator(ex.Message)); return; } ReplaceFileButton.Visible = true; Page.Validators.Add(new StaticValidator(String.Format(Translate("/filemanager/errormessage/replacefile"), DestinationFileName))); FileUploadPanel.Visible = false; UploadButton.Visible = false; CommentsPanel.Visible = false; } }