/// <summary> /// Action button handler. /// </summary> protected void btnOK_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(drpAction.SelectedValue)) { List <string> items = null; if (drpWhat.SelectedValue == "all") { // Get only the appropriate set of items string where = siteWhere; switch (drpAction.SelectedValue) { case "deleteindatabase": case "copytofilesystem": // Only process those where binary is available in DB where = SqlHelper.AddWhereCondition(where, "AttachmentBinary IS NOT NULL"); break; case "copytodatabase": // Only copy those where the binary is missing where = SqlHelper.AddWhereCondition(where, "AttachmentBinary IS NULL"); break; } // Get all, build the list of items DataSet ds = AttachmentInfoProvider.GetAttachments(where, gridFiles.SortDirect, false, 0, "AttachmentID"); if (!DataHelper.DataSourceIsEmpty(ds)) { items = new List <string>(); // Process all rows foreach (DataRow dr in ds.Tables[0].Rows) { int attachmentId = ValidationHelper.GetInteger(dr["AttachmentID"], 0); items.Add(attachmentId.ToString()); } } } else { // Take selected items items = gridFiles.SelectedItems; } if ((items != null) && (items.Count > 0)) { // Setup the async log pnlLog.Visible = true; pnlContent.Visible = false; ctlAsyncLog.TitleText = drpAction.SelectedItem.Text; CurrentError = string.Empty; // Process the file asynchronously var parameter = new object[] { items, drpAction.SelectedValue }; ctlAsyncLog.RunAsync(p => ProcessFiles(parameter), WindowsIdentity.GetCurrent()); } } }
/// <summary> /// Deletes the file binary from the file system. /// </summary> /// <param name="attachmentId">Attachment ID</param> /// <param name="name">Returning the attachment name</param> protected bool DeleteFromFileSystem(int attachmentId, ref string name) { // Delete the file in file system AttachmentInfo ai = AttachmentInfoProvider.GetAttachmentInfo(attachmentId, false); if (ai != null) { name = ai.AttachmentName; // Ensure the binary column first (check if exists) DataSet ds = AttachmentInfoProvider.GetAttachments("AttachmentID = " + attachmentId, null, true, 0, "CASE WHEN AttachmentBinary IS NULL THEN 0 ELSE 1 END AS HasBinary"); if (!DataHelper.DataSourceIsEmpty(ds)) { bool hasBinary = ValidationHelper.GetBoolean(ds.Tables[0].Rows[0][0], false); if (!hasBinary) { // Copy the binary data to database ai.AttachmentBinary = AttachmentInfoProvider.GetFile(ai, GetSiteName(ai.AttachmentSiteID)); ai.Generalized.UpdateData(); } // Delete the file from the disk AttachmentInfoProvider.DeleteFile(ai.AttachmentGUID, GetSiteName(ai.AttachmentSiteID), true, false); return(true); } } return(false); }
private void PageMoving(object sender, DocumentEventArgs e) { var node = e.Node; var target = e.TargetParentNode; bool isAtSynchronizedSite = _pageSync.IsAtSynchronizedSite(node); bool targetIsAtSynchronizedSite = _pageSync.IsAtSynchronizedSite(target); if (isAtSynchronizedSite != targetIsAtSynchronizedSite) { if (!targetIsAtSynchronizedSite) { // Delete subtree when moving away from synchronized site var subtree = _pageSync.GetSourceDocuments() .OnSite(node.NodeSiteName) .Path(node.NodeAliasPath, PathTypeEnum.Section) .AllCultures() .TypedResult .ToList(); var documentIds = subtree.Select(n => n.DocumentID).ToList(); var attachments = AttachmentInfoProvider.GetAttachments() .WhereIn("AttachmentDocumentID", documentIds) .BinaryData(false) .ExceptVariants() .TypedResult .ToList(); RunSynchronization(async() => { var info = node.NodeAliasPath + "%"; await _pageSync.DeletePages(null, subtree, info); await _assetSync.DeleteAttachments(null, attachments, info); }); } else { // Sync subtree when moving to synchronized site e.CallWhenFinished(() => { var subtree = _pageSync.GetSourceDocuments() .OnSite(node.NodeSiteName) .Path(node.NodeAliasPath, PathTypeEnum.Section) .AllCultures() .WithCoupledColumns() .TypedResult .ToList(); RunSynchronization(async() => { foreach (var page in subtree) { await _pageSync.SyncPageWithAllData(null, page); } }); }); } } }
protected void RunInternal(int daysBeforeLastModified, int maxAllowedVersions) { try { RunningInternal = true; ProgressMessageBuffer.Add("Starting attachment history removal process..."); ProgressMessageBuffer.Add(string.Format( "Limiting attachments older than {0} days to no more than {1} versions", daysBeforeLastModified, maxAllowedVersions )); var cutoffDate = DateTime.Today.AddDays(-daysBeforeLastModified); var cutoffDateString = string.Format("{0}-{1}-{2}", cutoffDate.Year, cutoffDate.Month + 1, cutoffDate.Day); var where = string.Format("AttachmentLastModified < '{0}'", cutoffDateString); ProgressMessageBuffer.Add(where); var attachments = AttachmentInfoProvider.GetAttachments(where, "AttachmentName", false, 0); ProgressMessageBuffer.Add(string.Format("Found {0} attachments last modified before {1}.", attachments.Count, cutoffDateString)); foreach (var att in attachments) { if (Cancelled) { ProgressMessageBuffer.Add("Attachment history removal process cancelled."); return; } TruncateAttachmentHistory(att, maxAllowedVersions); } ProgressMessageBuffer.Add("Attachment history removal process complete."); RunningInternal = false; } 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; } }
public async Task SyncAllAttachments(CancellationToken?cancellation, TreeNode node) { try { SyncLog.LogEvent(EventType.INFORMATION, "KenticoKontentPublishing", "SYNCALLATTACHMENTS", node.NodeAliasPath); var attachments = AttachmentInfoProvider.GetAttachments(node.DocumentID, false); await SyncAttachments(cancellation, attachments); } catch (Exception ex) { SyncLog.LogException("KenticoKontentPublishing", "SYNCALLATTACHMENTS", ex); throw; } }
private IList <Guid> GetAttachmentGuids(TreeNode node, FormFieldInfo attachmentsField) { var guidsQuery = AttachmentInfoProvider.GetAttachments(node.DocumentID, false) .ExceptVariants() .Columns("AttachmentGUID") .OrderBy("AttachmentOrder"); var narrowedQuery = (attachmentsField != null) ? guidsQuery.WhereEquals("AttachmentGroupGUID", attachmentsField.Guid) : guidsQuery.WhereTrue("AttachmentIsUnsorted"); var guids = narrowedQuery .GetListResult <Guid>(); return(guids); }
/// <summary> /// Gets search items filled with all necessary properties from SearchContext.CurrentSearchResults and mRawResults collection. /// </summary> /// <returns>Search items collection</returns> private IEnumerable <SearchResultItem> GetSearchItems() { var searchItems = new List <SearchResultItem>(); if (DataHelper.DataSourceIsEmpty(SearchContext.CurrentSearchResults) || (mRawResults == null) || (SearchContext.CurrentSearchResults == null)) { return(null); } var attachmentIdentifiers = new List <Guid>(); foreach (DataRow row in mRawResults.Tables[0].Rows) { string date, pageTypeDisplayName, pageTypeCodeName; int nodeId; var documentNodeId = GetDocumentNodeId(row["type"], row["id"]); var guid = ((row["image"] as string) == null) ? Guid.Empty : new Guid(row["image"].ToString()); attachmentIdentifiers.Add(guid); GetAdditionalData(row["type"], documentNodeId, out date, out nodeId, out pageTypeDisplayName, out pageTypeCodeName); var searchItem = new SearchResultItem { NodeId = nodeId, Title = row["title"].ToString(), Content = row["content"].ToString(), Date = date, PageTypeDispayName = pageTypeDisplayName, PageTypeCodeName = pageTypeCodeName }; searchItems.Add(searchItem); } var attachments = AttachmentInfoProvider.GetAttachments().OnSite(mSiteName).BinaryData(false).WhereIn("AttachmentGUID", attachmentIdentifiers).ToDictionary(x => x.AttachmentGUID); for (int i = 0; i < searchItems.Count; i++) { AttachmentInfo attachment = null; if (attachments.TryGetValue(attachmentIdentifiers[i], out attachment)) { searchItems[i].ImageAttachment = new Attachment(attachment); } } return(searchItems); }
private void PageDeleting(object sender, DocumentEventArgs e) { var node = e.Node; if (_pageSync.IsAtSynchronizedSite(node)) { var attachments = AttachmentInfoProvider.GetAttachments(node.DocumentID, false) .ExceptVariants() .TypedResult .ToList(); RunSynchronization(async() => { await _pageSync.DeletePage(node); await _assetSync.DeleteAttachments(null, attachments, node.NodeAliasPath); }); } }
public async Task SyncAllAttachments(CancellationToken?cancellation) { try { SyncLog.Log("Synchronizing page attachments"); SyncLog.LogEvent(EventType.INFORMATION, "KenticoKontentPublishing", "SYNCALLATTACHMENTS"); var attachments = AttachmentInfoProvider.GetAttachments() .OnSite(Settings.Sitename); await SyncAttachments(cancellation, attachments); } catch (Exception ex) { SyncLog.LogException("KenticoKontentPublishing", "SYNCALLATTACHMENTS", ex); throw; } }
protected void RunInternal() { try { RunningInternal = true; ProgressMessageBuffer.Add("Starting cleaning process..."); var attachmentIDs = AttachmentInfoProvider .GetAttachments(null, "AttachmentName", false) .Select(att => att.AttachmentID); if (attachmentIDs == null) { return; } var sites = SiteInfoProvider.GetSites(); // Configure attachment storage setting keys. Mover won't work without this: SettingsKeyInfoProvider.SetValue("CMSStoreFilesInFileSystem", "True", false); SettingsKeyInfoProvider.SetValue("CMSStoreFilesInDatabase", "False", false); foreach (var aID in attachmentIDs) { if (Cancelled) { RunningInternal = false; Cancelled = false; return; } var att = AttachmentInfoProvider.GetAttachmentInfo(aID, false); var attSite = sites.FirstOrDefault(s => s.SiteID == att.AttachmentSiteID); if (attSite == null) { continue; } AttachmentInfoProvider.EnsurePhysicalFile(att, attSite.SiteName); att.AttachmentBinary = null; att.Generalized.UpdateData(); ProgressMessageBuffer.Add(att.AttachmentName + " copied to file system."); } ProgressMessageBuffer.Add("Cleaning Process Complete"); RunningInternal = false; } catch (Exception e) { ProgressMessageBuffer.Add("ERROR --------------------------"); ProgressMessageBuffer.Add(e.Message); ProgressMessageBuffer.Add(e.StackTrace); RunningInternal = false; } }
/// <summary> /// Gets the page attachment info objects for the given attachment GUIDs. /// </summary> internal virtual Dictionary <Guid, AttachmentInfo> GetPageAttachments(ICollection <Guid> attachmentGuids) { return(AttachmentInfoProvider.GetAttachments().OnSite(mSiteName).BinaryData(false).WhereIn("AttachmentGUID", attachmentGuids).ToDictionary(x => x.AttachmentGUID)); }