protected PageModel GetPageModel() { return(new PageModel { TotalAttachments = AttachmentInfoProvider.GetCount(), TotalAttachmentHistories = AttachmentHistoryInfoProvider.GetCount(), AttachmentHistoryRemoverIsRunning = AttachmentHistoryRemover.Running, AttachmentMoverIsRunning = AttachmentMover.Running, }); }
private void AttachmentOnBeforeSave(object sender, ObjectEventArgs e) { if (e.Object == null) { return; } // If workflow enabled if (e.Object is AttachmentHistoryInfo attachmentVersion) { var latestAttachmentVersion = AttachmentHistoryInfoProvider.GetAttachmentHistories() .WhereEquals("AttachmentGUID", attachmentVersion.AttachmentGUID) .OrderByDescending("AttachmentLastModified") .TopN(1) .FirstOrDefault(); if (latestAttachmentVersion == null || latestAttachmentVersion.AttachmentSize != attachmentVersion.AttachmentSize) { var optimizer = new TinyPngImageOptimizer(SiteContext.CurrentSiteName); optimizer.Optimize(attachmentVersion); } } // If workflow disabled if (e.Object is AttachmentInfo attachment) { var document = DocumentHelper.GetDocument(attachment.AttachmentDocumentID, new TreeProvider()); if (document.WorkflowStep == null) { var currentAttachment = AttachmentInfoProvider.GetAttachmentInfo(attachment.AttachmentID, true); if (currentAttachment == null || currentAttachment.AttachmentSize != attachment.AttachmentSize) { var optimizer = new TinyPngImageOptimizer(SiteContext.CurrentSiteName); optimizer.Optimize(attachment); } } } }
/// <summary> /// UniGrid external data bound. /// </summary> protected object gridAttachments_OnExternalDataBound(object sender, string sourceName, object parameter) { DataRowView rowView = null; string attName = null; string attachmentExt = null; switch (sourceName.ToLowerCSafe()) { case "clone": ImageButton imgClone = sender as ImageButton; if (imgClone != null) { if (this.IsLiveSite) { // Hide cloning on live site imgClone.Visible = false; return(imgClone); } Guid attGuid = ValidationHelper.GetGuid(((DataRowView)((GridViewRow)parameter).DataItem).Row["AttachmentGUID"], Guid.Empty); string objectType = null; int id = 0; if (VersionHistoryID > 0) { objectType = PredefinedObjectType.ATTACHMENTHISTORY; } else { objectType = PredefinedObjectType.ATTACHMENT; } BaseInfo att = AttachmentHistoryInfoProvider.GetInfoByGuid(objectType, attGuid); if (att != null) { id = att.Generalized.ObjectID; } imgClone.PreRender += new EventHandler(imgClone_PreRender); imgClone.OnClientClick = "modalDialog('" + URLHelper.ResolveUrl("~/CMSModules/Objects/Dialogs/CloneObjectDialog.aspx?objectType=" + objectType + "&objectId=" + id) + "', 'CloneObject', 750, 400); return false;"; } break; case "update": Panel pnlBlock = new Panel { ID = "pnlBlock" }; bool isWebDAVEnabled = CMSContext.IsWebDAVEnabled(SiteName); bool isWindowsAuthentication = RequestHelper.IsWindowsAuthentication(); pnlBlock.Style.Add("margin", "0 auto"); pnlBlock.Width = ((isWebDAVEnabled && isWindowsAuthentication) ? 32 : 16); // Add disabled image ImageButton imgUpdate = new ImageButton { ID = "imgUpdate" }; imgUpdate.PreRender += imgUpdate_PreRender; pnlBlock.Controls.Add(imgUpdate); // Add update control // Dynamically load uploader control DirectFileUploader dfuElem = Page.LoadUserControl("~/CMSModules/Content/Controls/Attachments/DirectFileUploader/DirectFileUploader.ascx") as DirectFileUploader; rowView = parameter as DataRowView; // Set uploader's properties if (dfuElem != null) { dfuElem.SourceType = MediaSourceEnum.DocumentAttachments; dfuElem.ID = "dfuElem" + DocumentID; dfuElem.IsLiveSite = IsLiveSite; dfuElem.EnableSilverlightUploader = false; dfuElem.ControlGroup = "update"; dfuElem.AttachmentGUID = GetAttachmentGuid(rowView); dfuElem.DisplayInline = true; dfuElem.UploadMode = MultifileUploaderModeEnum.DirectSingle; dfuElem.InnerLoadingDivHtml = " "; dfuElem.MaxNumberToUpload = 1; dfuElem.Height = 16; dfuElem.Width = 16; dfuElem.PreRender += dfuElem_PreRender; pnlBlock.Controls.Add(dfuElem); } attName = GetAttachmentName(rowView); attachmentExt = GetAttachmentExtension(rowView); int nodeGroupId = (Node != null) ? Node.GetIntegerValue("NodeGroupID") : 0; // Check if WebDAV allowed by the form bool allowWebDAV = (Form == null) || Form.AllowWebDAV; // Add WebDAV edit control if (allowWebDAV && isWebDAVEnabled && isWindowsAuthentication && (FormGUID == Guid.Empty) && WebDAVSettings.IsExtensionAllowedForEditMode(attachmentExt, SiteName)) { // Dynamically load control WebDAVEditControl webdavElem = Page.LoadUserControl("~/CMSModules/WebDAV/Controls/AttachmentWebDAVEditControl.ascx") as WebDAVEditControl; // Set editor's properties if (webdavElem != null) { webdavElem.Enabled = Enabled; webdavElem.ID = "webdavElem" + DocumentID; // Ensure form identification if ((Form != null) && (Form.Parent != null)) { webdavElem.FormID = Form.Parent.ClientID; } webdavElem.SiteName = SiteName; webdavElem.FileName = attName; webdavElem.NodeAliasPath = Node.NodeAliasPath; webdavElem.NodeCultureCode = Node.DocumentCulture; webdavElem.PreRender += webdavElem_PreRender; if (FieldInfo != null) { webdavElem.AttachmentFieldName = FieldInfo.Name; } // Set Group ID for live site webdavElem.GroupID = IsLiveSite ? nodeGroupId : 0; webdavElem.IsLiveSite = IsLiveSite; // Align left if WebDAV is enabled and windows authentication is enabled bool isRTL = (IsLiveSite && CultureHelper.IsPreferredCultureRTL()) || (!IsLiveSite && CultureHelper.IsUICultureRTL()); pnlBlock.Style.Add("text-align", isRTL ? "right" : "left"); pnlBlock.Controls.Add(webdavElem); } } return(pnlBlock); case "edit": // Get file extension string extension = ValidationHelper.GetString(((DataRowView)((GridViewRow)parameter).DataItem).Row["AttachmentExtension"], string.Empty).ToLowerCSafe(); Guid guid = ValidationHelper.GetGuid(((DataRowView)((GridViewRow)parameter).DataItem).Row["AttachmentGUID"], Guid.Empty); ImageButton img = sender as ImageButton; if (img != null) { img.AlternateText = String.Format("{0}|{1}", extension, guid); img.PreRender += img_PreRender; } break; case "delete": ImageButton imgDelete = sender as ImageButton; if (imgDelete != null) { // Turn off validation imgDelete.CausesValidation = false; imgDelete.PreRender += imgDelete_PreRender; // Explicitly initialize confirmation imgDelete.OnClientClick = "if(DeleteConfirmation() == false){return false;}"; } break; case "moveup": ImageButton imgUp = sender as ImageButton; if (imgUp != null) { // Turn off validation imgUp.CausesValidation = false; imgUp.PreRender += imgUp_PreRender; } break; case "movedown": ImageButton imgDown = sender as ImageButton; if (imgDown != null) { // Turn off validation imgDown.CausesValidation = false; imgDown.PreRender += imgDown_PreRender; } break; case "attachmentname": { rowView = parameter as DataRowView; // Get attachment GUID Guid attachmentGuid = GetAttachmentGuid(rowView); // Get attachment extension attachmentExt = GetAttachmentExtension(rowView); bool isImage = ImageHelper.IsImage(attachmentExt); string iconUrl = GetFileIconUrl(attachmentExt, "List"); // Get link for attachment string attachmentUrl = null; attName = ValidationHelper.GetString(rowView["AttachmentName"], string.Empty); int documentId = DocumentID; if (Node != null) { if (IsLiveSite && (documentId > 0)) { attachmentUrl = CMSContext.ResolveUIUrl(TreePathUtils.GetAttachmentUrl(attachmentGuid, URLHelper.GetSafeFileName(attName, CMSContext.CurrentSiteName), 0, null)); } else { attachmentUrl = CMSContext.ResolveUIUrl(TreePathUtils.GetAttachmentUrl(attachmentGuid, URLHelper.GetSafeFileName(attName, CMSContext.CurrentSiteName), VersionHistoryID, null)); } } else { attachmentUrl = ResolveUrl(DocumentHelper.GetAttachmentUrl(attachmentGuid, VersionHistoryID)); } attachmentUrl = URLHelper.UpdateParameterInUrl(attachmentUrl, "chset", Guid.NewGuid().ToString()); // Ensure correct URL if (OriginalNodeSiteName != CMSContext.CurrentSiteName) { attachmentUrl = URLHelper.AddParameterToUrl(attachmentUrl, "sitename", OriginalNodeSiteName); } // Add latest version requirement for live site if (IsLiveSite && (documentId > 0)) { // Add requirement for latest version of files for current document string newparams = "latestfordocid=" + documentId; newparams += "&hash=" + ValidationHelper.GetHashString("d" + documentId); attachmentUrl += "&" + newparams; } // Optionally trim attachment name string attachmentName = TextHelper.LimitLength(attName, ATTACHMENT_NAME_LIMIT, "..."); // Tooltip string tooltip = null; if (ShowTooltip) { string title = ValidationHelper.GetString(DataHelper.GetDataRowViewValue(rowView, "AttachmentTitle"), string.Empty); string description = ValidationHelper.GetString(DataHelper.GetDataRowViewValue(rowView, "AttachmentDescription"), string.Empty); int imageWidth = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(rowView, "AttachmentImageWidth"), 0); int imageHeight = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(rowView, "AttachmentImageHeight"), 0); tooltip = UIHelper.GetTooltipAttributes(attachmentUrl, imageWidth, imageHeight, title, attachmentName, attachmentExt, description, null, 300); } // Icon string imageTag = String.Format("<img class=\"Icon\" src=\"{0}\" alt=\"{1}\" />", iconUrl, attachmentName); if (isImage) { return(String.Format("<a href=\"#\" onclick=\"javascript: window.open('{0}'); return false;\"><span id=\"{1}\" {2}>{3}{4}</span></a>", attachmentUrl, attachmentGuid, tooltip, imageTag, attachmentName)); } else { return(String.Format("<a href=\"{0}\"><span id=\"{1}\" {2}>{3}{4}</span></a>", attachmentUrl, attachmentGuid, tooltip, imageTag, attachmentName)); } } case "attachmentsize": long size = ValidationHelper.GetLong(parameter, 0); return(DataHelper.GetSizeString(size)); } return(parameter); }
protected void TruncateAttachmentHistory(AttachmentInfo att, int maxAllowedVersions = 1, bool clearSingleHistoryBinaries = false) { try { // If we delete ALL attachment history, Kentico can't find ANY attacments: if (maxAllowedVersions < 1) { return; } var where = string.Format("AttachmentGuid = '{0}'", att.AttachmentGUID); var attachmentHistories = AttachmentHistoryInfoProvider .GetAttachmentHistories(where, "AttachmentLastModified", 0, "AttachmentHistoryID") .BinaryData(false); var attachmentHistoriesCount = attachmentHistories.Count(); ProgressMessageBuffer.Add(string.Format("Found {0} histories for {1}", attachmentHistoriesCount, att.AttachmentName)); // DON'T EVER DELETE ALL histories for an attachment. It will become unfindable to Kentico. if (attachmentHistoriesCount <= maxAllowedVersions || attachmentHistoriesCount == 1) { return; } var maxAllowedRange = attachmentHistoriesCount - 1; var range = (maxAllowedVersions > maxAllowedRange) ? maxAllowedRange : attachmentHistoriesCount - maxAllowedVersions; var historiesToDelete = attachmentHistories.Take(range); var historiesToDeleteCount = historiesToDelete.Count(); if (historiesToDeleteCount == 0) { return; } if (attachmentHistoriesCount - historiesToDeleteCount < 1) { return; } foreach (var h in historiesToDelete) { h.Generalized.DeleteData(); h.Generalized.UpdateData(); } var finalHistoryCount = attachmentHistoriesCount - historiesToDeleteCount; ProgressMessageBuffer.Add(string.Format( "Truncated history for {0} from {1} versions to {2}", att.AttachmentName, attachmentHistoriesCount, finalHistoryCount )); } catch (Exception e) { ProgressMessageBuffer.Add("Removal stopped after encountering error."); ProgressMessageBuffer.Add(e.StackTrace); ProgressMessageBuffer.Add(e.Message); ProgressMessageBuffer.Add("ERROR --------------------------"); EventLogProvider.LogEvent(new EventLogInfo("SPRING CLEANING", e.StackTrace, "REMOVE ATTACHMENT HISTORY")); RunningInternal = false; } }