public static List <string> GetEmbeddedAttachmentLinks(this MailComposeBase draft) { var links = new List <string>(); var fckStorage = StorageManager.GetDataStoreForCkImages(draft.Mailbox.TenantId); //todo: replace selector var currentMailFckeditorUrl = fckStorage.GetUri(StorageManager.CKEDITOR_IMAGES_DOMAIN, "").ToString(); var currentMailAttachmentFolderUrl = MailStoragePathCombiner.GetMessageDirectory(draft.Mailbox.UserId, draft.StreamId); var currentUserStorageUrl = MailStoragePathCombiner.GetUserMailsDirectory(draft.Mailbox.UserId); var xpathQuery = StorageManager.GetXpathQueryForAttachmentsToResaving(currentMailFckeditorUrl, currentMailAttachmentFolderUrl, currentUserStorageUrl); var doc = new HtmlDocument(); doc.LoadHtml(draft.HtmlBody); var linkNodes = doc.DocumentNode.SelectNodes(xpathQuery); if (linkNodes == null) { return(links); } links.AddRange(linkNodes.Select(linkNode => linkNode.Attributes["src"].Value)); return(links); }
private static void RemoveUserMailDirectory(int tenant, string userId, ILogger log) { log.Debug("MailDataStore.GetDataStore(Tenant = {0})", tenant); var dataStorage = MailDataStore.GetDataStore(tenant); var userMailDir = MailStoragePathCombiner.GetUserMailsDirectory(userId); try { log.Info("RemoveUserMailDirectory(Path: {0}, Tenant = {1} User = '******')", userMailDir, tenant, userId); dataStorage.DeleteDirectory(userMailDir); } catch (Exception ex) { log.Error("MailDataStore.DeleteDirectory(path: {0}) failed. Error: {1}", userMailDir, ex.ToString()); } }
public bool RemoveTerminatedUserGarbageMailData(int tenant, string user) { var userInfo = GetUserInfo(tenant, user); if (userInfo == null || userInfo.Status != EmployeeStatus.Terminated) { return(false); } _log.Info( "User is terminated. All mail's data for this user = '******' (Tenant={1}) will be removed!", user, tenant); try { var dataStorage = MailDataStore.GetDataStore(tenant); var path = MailStoragePathCombiner.GetUserMailsDirectory(userInfo.ID.ToString()); if (dataStorage.IsDirectory(string.Empty, path)) { _log.Debug("Trying remove all user mail's files from storage '{0}' on path '{1}'", Defines.MODULE_NAME, path); dataStorage.DeleteDirectory(string.Empty, path); } } catch (Exception ex) { _log.Error("Can't remove all stored mail data for user '{0}' on tenant {1}\r\nException: {2}", user, tenant, ex.ToString()); return(false); } _log.Debug("Trying remove garbage from db"); _garbageManager.ClearUserMailData(tenant, userInfo); return(true); }
private List <MailAttachmentData> FixHtmlBodyWithEmbeddedAttachments(MailComposeBase compose) { var embededAttachmentsForSaving = new List <MailAttachmentData>(); var embeddedLinks = compose.GetEmbeddedAttachmentLinks(); if (!embeddedLinks.Any()) { return(embededAttachmentsForSaving); } var fckStorage = StorageManager.GetDataStoreForCkImages(compose.Mailbox.TenantId); var attachmentStorage = StorageManager.GetDataStoreForAttachments(compose.Mailbox.TenantId); var currentMailFckeditorUrl = fckStorage.GetUri(StorageManager.CKEDITOR_IMAGES_DOMAIN, "").ToString(); var currentUserStorageUrl = MailStoragePathCombiner.GetUserMailsDirectory(compose.Mailbox.UserId); var engine = new EngineFactory(compose.Mailbox.TenantId, compose.Mailbox.UserId); StorageManager storage = null; foreach (var embeddedLink in embeddedLinks) { try { var isFckImage = embeddedLink.StartsWith(currentMailFckeditorUrl); var prefixLength = isFckImage ? currentMailFckeditorUrl.Length : embeddedLink.IndexOf(currentUserStorageUrl, StringComparison.Ordinal) + currentUserStorageUrl.Length + 1; var fileLink = HttpUtility.UrlDecode(embeddedLink.Substring(prefixLength)); var fileName = Path.GetFileName(fileLink); var attach = new MailAttachmentData { fileName = fileName, storedName = fileName, contentId = embeddedLink.GetMd5(), storedFileUrl = fileLink, streamId = compose.StreamId, user = compose.Mailbox.UserId, tenant = compose.Mailbox.TenantId, mailboxId = compose.Mailbox.MailBoxId }; var savedAttachment = engine.AttachmentEngine.GetAttachment( new ConcreteContentAttachmentExp(compose.Id, attach.contentId)); var savedAttachmentId = savedAttachment == null ? 0 : savedAttachment.fileId; var attachmentWasSaved = savedAttachmentId != 0; var currentImgStorage = isFckImage ? fckStorage : attachmentStorage; var domain = isFckImage ? StorageManager.CKEDITOR_IMAGES_DOMAIN : compose.Mailbox.UserId; if (compose.Id == 0 || !attachmentWasSaved) { attach.data = StorageManager.LoadDataStoreItemData(domain, fileLink, currentImgStorage); if (storage == null) { storage = new StorageManager(compose.Mailbox.TenantId, compose.Mailbox.UserId); } storage.StoreAttachmentWithoutQuota(attach); embededAttachmentsForSaving.Add(attach); } if (attachmentWasSaved) { attach = engine.AttachmentEngine.GetAttachment( new ConcreteUserAttachmentExp(savedAttachmentId, compose.Mailbox.TenantId, compose.Mailbox.UserId)); var path = MailStoragePathCombiner.GerStoredFilePath(attach); currentImgStorage = attachmentStorage; attach.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(currentImgStorage.GetUri(path)); } compose.HtmlBody = compose.HtmlBody.Replace(embeddedLink, attach.storedFileUrl); } catch (Exception ex) { Log.ErrorFormat("ChangeEmbededAttachmentLinksForStoring() failed with exception: {0}", ex.ToString()); } } return(embededAttachmentsForSaving); }
public static List <MailAttachment> ChangeEmbededAttachmentLinksForStoring(this MailDraft draft, MailBoxManager manager, ILogger log = null) { if (log == null) { log = new NullLogger(); } //Todo: This method can be separated in two var fckStorage = StorageManager.GetDataStoreForCkImages(draft.Mailbox.TenantId); var attachmentStorage = StorageManager.GetDataStoreForAttachments(draft.Mailbox.TenantId); var currentMailFckeditorUrl = fckStorage.GetUri(StorageManager.CKEDITOR_IMAGES_DOMAIN, "").ToString(); var currentMailAttachmentFolderUrl = MailStoragePathCombiner.GetMessageDirectory(draft.Mailbox.UserId, draft.StreamId); var currentUserStorageUrl = MailStoragePathCombiner.GetUserMailsDirectory(draft.Mailbox.UserId); log.Info("ChangeEmbededAttachmentLinksForStoring() Fckeditor storage base url: {0}", currentMailFckeditorUrl); log.Info("ChangeEmbededAttachmentLinksForStoring() Current mail attachment folder storage base url: {0}", currentMailAttachmentFolderUrl); log.Info("ChangeEmbededAttachmentLinksForStoring() Current user folder storage base url: {0}", currentUserStorageUrl); var xpathQuery = StorageManager.GetXpathQueryForAttachmentsToResaving(currentMailFckeditorUrl, currentMailAttachmentFolderUrl, currentUserStorageUrl); log.Info( "ChangeEmbededAttachmentLinksForStoring() Xpath Query selector for embedded attachment links: {0}", xpathQuery); var attachmentsForSaving = new List <MailAttachment>(); var doc = new HtmlDocument(); doc.LoadHtml(draft.HtmlBody); var linkNodes = doc.DocumentNode.SelectNodes(xpathQuery); if (linkNodes != null) { foreach (var linkNode in linkNodes) { try { var link = linkNode.Attributes["src"].Value; log.Info("ChangeEmbededAttachmentLinksForStoring() Original selected file_link: {0}", link); var isFckImage = link.StartsWith(currentMailFckeditorUrl); var prefixLength = isFckImage ? currentMailFckeditorUrl.Length : link.IndexOf(currentUserStorageUrl, StringComparison.Ordinal) + currentUserStorageUrl.Length + 1; var fileLink = HttpUtility.UrlDecode(link.Substring(prefixLength)); var fileName = Path.GetFileName(fileLink); var attach = CreateEmbbededAttachment(fileName, link, fileLink, draft.Mailbox.UserId, draft.Mailbox.TenantId, draft.Mailbox.MailBoxId, draft.StreamId); var savedAttachmentId = manager.GetAttachmentId(draft.Id, attach.contentId); var attachmentWasSaved = savedAttachmentId != 0; var currentImgStorage = isFckImage ? fckStorage : attachmentStorage; var domain = isFckImage ? StorageManager.CKEDITOR_IMAGES_DOMAIN : draft.Mailbox.UserId; if (draft.Id == 0 || !attachmentWasSaved) { attach.data = StorageManager.LoadDataStoreItemData(domain, fileLink, currentImgStorage); manager.StoreAttachmentWithoutQuota(draft.Mailbox.TenantId, draft.Mailbox.UserId, attach); attachmentsForSaving.Add(attach); } if (attachmentWasSaved) { attach = manager.GetMessageAttachment(savedAttachmentId, draft.Mailbox.TenantId, draft.Mailbox.UserId); var path = MailStoragePathCombiner.GerStoredFilePath(attach); currentImgStorage = attachmentStorage; attach.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(currentImgStorage.GetUri(path)); } log.Info("ChangeEmbededAttachmentLinksForStoring() Restored new file_link: {0}", attach.storedFileUrl); linkNode.SetAttributeValue("src", attach.storedFileUrl); } catch (Exception ex) { log.Error("ChangeEmbededAttachmentLinksForStoring() failed with exception: {0}", ex.ToString()); } } draft.HtmlBody = doc.DocumentNode.OuterHtml; } return(attachmentsForSaving); }