protected virtual ResourceBE BuildResourceRev(ResourceBE currentRevision) { currentRevision.AssertHeadRevision(); //Clone the resource revision ResourceBE newRev = new ResourceBE(currentRevision) { ChangeMask = ResourceBE.ChangeOperations.UNDEFINED, Timestamp = DateTime.UtcNow, UserId = DekiContext.Current.User.ID, ChangeSetId = 0, Deleted = false, ChangeDescription = null, IsHidden = false, Meta = currentRevision.Meta }; //Initialize for new revision (clear anything that shouldn't be carried over from current rev) return(newRev); }
/// <summary> /// Move this attachment to the target page. /// </summary> /// <remarks> /// This will fail if destination page has a file with the same name. /// </remarks> /// <param name="sourcePage">Current file location</param> /// <param name="targetPage">Target file location. May be same as sourcepage for rename</param> /// <param name="name">New filename or null for no change</param> /// <returns></returns> public ResourceBE MoveAttachment(ResourceBE attachment, PageBE sourcePage, PageBE targetPage, string name, bool loggingEnabled) { //TODO MaxM: Connect with a changeset uint changeSetId = 0; attachment.AssertHeadRevision(); bool move = targetPage != null && targetPage.ID != sourcePage.ID; bool rename = name != null && !name.EqualsInvariant(attachment.Name); //Just return the current revision if no change is being made if (!move && !rename) { return(attachment); } //validate filename if (rename) { name = ValidateFileName(name); } //Check the resource exists on the target (may be same as source page) with new name (if given) or current name ResourceBE existingAttachment = GetPageAttachment((targetPage ?? sourcePage).ID, name ?? attachment.Name); if (existingAttachment != null) { throw new AttachmentExistsOnPageConflictException(name ?? attachment.Name, (targetPage ?? sourcePage).Title.AsUserFriendlyName()); } //Performing a move? if (move) { _dekiContext.Instance.Storage.MoveFile(attachment, targetPage); //Perform the IStorage move (should be a no-op) } //Build the new revision ResourceBE newRevision = BuildRevForMoveAndRename(attachment, targetPage, name, changeSetId); //Insert new revision into DB try { newRevision = SaveResource(newRevision); } catch { //failed to save the revision, undo the file move with the IStorage. (Should be a no-op) if (move) { _dekiContext.Instance.Storage.MoveFile(attachment, sourcePage); } throw; //NOTE MaxM: file rename does not even touch IStorage. No need to undo it } //Notification for file move if (loggingEnabled) { if (move) { RecentChangeBL.AddFileRecentChange(_dekiContext.Now, sourcePage, _dekiContext.User, DekiResources.FILE_MOVED_TO(attachment.Name, targetPage.Title.AsPrefixedUserFriendlyPath()), changeSetId); RecentChangeBL.AddFileRecentChange(_dekiContext.Now, targetPage, _dekiContext.User, DekiResources.FILE_MOVED_FROM(attachment.Name, sourcePage.Title.AsPrefixedUserFriendlyPath()), changeSetId); } if (rename) { RecentChangeBL.AddFileRecentChange(_dekiContext.Now, sourcePage, _dekiContext.User, DekiResources.FILE_RENAMED_TO(attachment.Name, name), changeSetId); } } //Notification for file rename and move use same event _dekiContext.Instance.EventSink.AttachmentMove(_dekiContext.Now, attachment, sourcePage, _dekiContext.User); return(newRevision); }