Exemple #1
0
        public static void ChangeEmbeddedAttachmentLinks(this MailDraftData draft, ILog log = null)
        {
            if (log == null)
            {
                log = new NullLog();
            }

            var baseAttachmentFolder = MailStoragePathCombiner.GetMessageDirectory(draft.Mailbox.UserId, draft.StreamId);

            var doc = new HtmlDocument();

            doc.LoadHtml(draft.HtmlBody);
            var linkNodes = doc.DocumentNode.SelectNodes("//img[@src and (contains(@src,'" + baseAttachmentFolder + "'))]");

            if (linkNodes == null)
            {
                return;
            }

            foreach (var linkNode in linkNodes)
            {
                var link = linkNode.Attributes["src"].Value;
                log.InfoFormat("ChangeEmbededAttachmentLinks() Embeded attachment link for changing to cid: {0}", link);
                var fileLink = HttpUtility.UrlDecode(link.Substring(baseAttachmentFolder.Length));
                var fileName = Path.GetFileName(fileLink);

                var attach = CreateEmbbededAttachment(fileName, link, fileLink, draft.Mailbox.UserId, draft.Mailbox.TenantId, draft.Mailbox.MailBoxId, draft.StreamId);
                draft.AttachmentsEmbedded.Add(attach);
                linkNode.SetAttributeValue("src", "cid:" + attach.contentId);
                log.InfoFormat("ChangeEmbededAttachmentLinks() Attachment cid: {0}", attach.contentId);
            }
            draft.HtmlBody = doc.DocumentNode.OuterHtml;
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        public void ChangeEmbededAttachmentLinks(int tenant, string user)
        {
            var baseAttachmentFolder = MailStoragePathCombiner.GetMessageDirectory(user, StreamId);

            var doc = new HtmlDocument();

            doc.LoadHtml(HtmlBody);
            var linkNodes = doc.DocumentNode.SelectNodes("//img[@src and (contains(@src,'" + baseAttachmentFolder + "'))]");

            if (linkNodes == null)
            {
                return;
            }

            foreach (var linkNode in linkNodes)
            {
                var link = linkNode.Attributes["src"].Value;
                _logger.Info("ChangeEmbededAttachmentLinks() Embeded attachment link for changing to cid: {0}", link);
                var fileLink = HttpUtility.UrlDecode(link.Substring(baseAttachmentFolder.Length));
                var fileName = Path.GetFileName(fileLink);

                var attach = CreateEmbbededAttachment(fileName, link, fileLink, user, tenant, MailboxId);
                AttachmentsEmbedded.Add(attach);
                linkNode.SetAttributeValue("src", "cid:" + attach.contentId);
                _logger.Info("ChangeEmbededAttachmentLinks() Attachment cid: {0}", attach.contentId);
            }
            HtmlBody = doc.DocumentNode.OuterHtml;
        }
Exemple #4
0
        public static void ChangeEmbeddedAttachmentLinks(this MailDraftData draft, ILog log = null)
        {
            if (log == null)
            {
                log = new NullLog();
            }

            var baseAttachmentFolder = MailStoragePathCombiner.GetMessageDirectory(draft.Mailbox.UserId, draft.StreamId);

            var doc = new HtmlDocument();

            doc.LoadHtml(draft.HtmlBody);
            var linkNodes = doc.DocumentNode.SelectNodes("//img[@src and (contains(@src,'" + baseAttachmentFolder + "') or @x-mail-embedded)]");

            if (linkNodes == null)
            {
                return;
            }

            foreach (var linkNode in linkNodes)
            {
                var link = linkNode.Attributes["src"].Value;
                log.InfoFormat("ChangeEmbededAttachmentLinks() Embeded attachment link for changing to cid: {0}", link);

                var isExternal = link.IndexOf(baseAttachmentFolder) == -1;

                var fileLink = isExternal
                    ? link
                    : HttpUtility.UrlDecode(link.Substring(baseAttachmentFolder.Length));
                var fileName = Path.GetFileName(fileLink);

                var attach = CreateEmbbededAttachment(fileName, link, fileLink, draft.Mailbox.UserId, draft.Mailbox.TenantId, draft.Mailbox.MailBoxId, draft.StreamId);

                if (isExternal)
                {
                    using (var webClient = new WebClient())
                    {
                        //webClient.Headers.Add("Authorization", GetPartnerAuthHeader(actionUrl));
                        try
                        {
                            attach.data = webClient.DownloadData(fileLink);
                        }
                        catch (WebException we)
                        {
                            log.Error(we);
                            continue;
                        }
                    }
                }
                draft.AttachmentsEmbedded.Add(attach);
                linkNode.SetAttributeValue("src", "cid:" + attach.contentId);
                log.InfoFormat("ChangeEmbededAttachmentLinks() Attachment cid: {0}", attach.contentId);
            }
            draft.HtmlBody = doc.DocumentNode.OuterHtml;
        }
Exemple #5
0
        public bool RemoveGarbageMailboxData(MailBox mailbox)
        {
            if (!mailbox.IsRemoved)
            {
                return(false);
            }

            _log.Info(
                "Mailbox is removed. All mail's data for this mailboxId = {0} will be removed!",
                mailbox.MailBoxId);

            try
            {
                var dataStorage = MailDataStore.GetDataStore(mailbox.TenantId);

                var mailboxMessagesIterator = new MailboxMessagesIterator(mailbox, _mailBoxManager);

                var message = mailboxMessagesIterator.First();
                while (!mailboxMessagesIterator.IsDone)
                {
                    var path = MailStoragePathCombiner.GetMessageDirectory(mailbox.UserId, message.StreamId);

                    try
                    {
                        if (dataStorage.IsDirectory(string.Empty, path))
                        {
                            _log.Debug("Trying remove files on path '{0}'", path);
                            dataStorage.DeleteDirectory(string.Empty, path);
                        }
                    }
                    catch (Exception)
                    {
                        _log.Error("Can't remove stored mail data in path = '{0}' for mailboxId = {0} on tenant {1}",
                                   path, mailbox.MailBoxId, mailbox.TenantId);
                    }

                    _log.Info("Clear MessageId = {1}", mailbox.MailBoxId, message.Id);
                    message = mailboxMessagesIterator.Next();
                }

                _log.Debug("Trying remove garbage from db");
                _garbageManager.ClearMailboxData(mailbox);
            }
            catch (Exception)
            {
                _log.Error("Can't remove all stored mail data for mailboxId = {0} on tenant {1}", mailbox.MailBoxId, mailbox.TenantId);
                return(false);
            }

            return(true);
        }
        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);
        }