private void EditDocument(int attachment_id, HttpContext context)
 {
     var mail_box_manager = new MailBoxManager(0);
     var file = mail_box_manager.GetMessageAttachment(attachment_id, TenantId, Username);
     var temp_file_url = MailStoragePathCombiner.GetPreSignedUrl(file);
     var editor_url = FilesLinkUtility.GetFileWebEditorExternalUrl(temp_file_url, file.fileName);
     editor_url = new UriBuilder(CommonLinkUtility.GetFullAbsolutePath(editor_url)).ToString();
     context.Response.Redirect(editor_url, false);
 }
 private void ViewDocument(int attachment_id, HttpContext context)
 {
     var mail_box_manager = new MailBoxManager(ConfigurationManager.ConnectionStrings["mail"], 0);
     var file = mail_box_manager.GetMessageAttachment(attachment_id, TenantId, Username);
     var temp_file_url = file.GerPreSignedUrl();
     var viewer_url = CommonLinkUtility.GetFileWebViewerExternalUrl(temp_file_url, file.fileName);
     viewer_url = new UriBuilder(CommonLinkUtility.GetFullAbsolutePath(viewer_url)).ToString();
     context.Response.Redirect(viewer_url, false);
 }
 private void ViewDocument(int attachmentId, HttpContext context)
 {
     var mailBoxManager = new MailBoxManager();
     var file = mailBoxManager.GetMessageAttachment(attachmentId, TenantId, Username);
     var tempFileUrl = MailStoragePathCombiner.GetPreSignedUrl(file);
     // ToDo GetFileWebViewerExternalUrl last argument need to be reviewed
     var viewerUrl = FilesLinkUtility.GetFileWebViewerExternalUrl(tempFileUrl, file.fileName, "");
     viewerUrl = new UriBuilder(CommonLinkUtility.GetFullAbsolutePath(viewerUrl)).ToString();
     context.Response.Redirect(viewerUrl, false);
 }
        public List<MailAttachment> ChangeEmbededAttachmentLinksForStoring(int tenant, string username, int mail_id, MailBoxManager manager)
        {
            //Todo: This method can be separated in two
            var fck_storage = StorageManager.GetDataStoreForCkImages(tenant);
            var attachment_storage = StorageManager.GetDataStoreForAttachments(tenant);

            var current_mail_fckeditor_url = fck_storage.GetUri(StorageManager.CkeditorImagesDomain, "").ToString();
            var current_mail_attachment_folder_url = GetThisMailFolder(username);
            var current_user_storage_url = GetUserFolder(username);
            _logger.Info("ChangeEmbededAttachmentLinksForStoring() Fckeditor storage base url: {0}", current_mail_fckeditor_url);
            _logger.Info("ChangeEmbededAttachmentLinksForStoring() Current mail attachment folder storage base url: {0}", current_mail_attachment_folder_url);
            _logger.Info("ChangeEmbededAttachmentLinksForStoring() Current user folder storage base url: {0}", current_user_storage_url);

            var xpath_query = StorageManager.GetXpathQueryForAttachmentsToResaving(current_mail_fckeditor_url, current_mail_attachment_folder_url, current_user_storage_url);
            _logger.Info("ChangeEmbededAttachmentLinksForStoring() Xpath Query selector for embedded attachment links: {0}", xpath_query);
            var attachments_for_saving = new List<MailAttachment>();

            var doc = new HtmlDocument();
            doc.LoadHtml(HtmlBody);
            
            var link_nodes = doc.DocumentNode.SelectNodes(xpath_query);

            if (link_nodes != null)
            {
                foreach (var link_node in link_nodes)
                {
                    try
                    {
                        var link = link_node.Attributes["src"].Value;

                        _logger.Info("ChangeEmbededAttachmentLinksForStoring() Original selected file_link: {0}", link);

                        var is_fck_image = link.StartsWith(current_mail_fckeditor_url);
                        var prefix_length = is_fck_image
                                                ? current_mail_fckeditor_url.Length
                                                : link.IndexOf(current_user_storage_url, StringComparison.Ordinal) +
                                                  current_user_storage_url.Length + 1;
                        var file_link = HttpUtility.UrlDecode(link.Substring(prefix_length));

                        var file_name = Path.GetFileName(file_link);
                        var attach = CreateEmbbededAttachment(file_name, link, file_link, username, tenant);

                        var saved_attachment_id = manager.GetAttachmentId(mail_id, attach.contentId);
                        var attachment_was_saved = saved_attachment_id != 0;
                        var current_img_storage = is_fck_image ? fck_storage : attachment_storage;
                        var domain = is_fck_image ? StorageManager.CkeditorImagesDomain : username;

                        if (mail_id == 0 || !attachment_was_saved)
                        {
                            attach.data = StorageManager.LoadDataStoreItemData(domain, file_link, current_img_storage);
                            //TODO: Add quota if needed
                            manager.StoreAttachmentWithoutQuota(tenant, username, attach);
                            attachments_for_saving.Add(attach);
                        }

                        if (attachment_was_saved)
                        {
                            attach = manager.GetMessageAttachment(saved_attachment_id, tenant, username);
                            var path = MailStoragePathCombiner.GerStoredFilePath(attach);
                            current_img_storage = attachment_storage;
                            attach.storedFileUrl = current_img_storage.GetUri("", path).ToString();
                        }

                        _logger.Info("ChangeEmbededAttachmentLinksForStoring() Restored new file_link: {0}",
                                     attach.storedFileUrl);
                        link_node.SetAttributeValue("src", attach.storedFileUrl);

                    }
                    catch (Exception ex)
                    {
                        _logger.Error("ChangeEmbededAttachmentLinksForStoring() failed with exception: {0}", ex.ToString());
                    }
                }

                HtmlBody = doc.DocumentNode.OuterHtml;
            }

            return attachments_for_saving;
        }
Example #5
0
        public List<MailAttachment> ChangeEmbededAttachmentLinksForStoring(int tenant, string user, int mailId, MailBoxManager manager)
        {
            //Todo: This method can be separated in two
            var fckStorage = StorageManager.GetDataStoreForCkImages(tenant);
            var attachmentStorage = StorageManager.GetDataStoreForAttachments(tenant);

            var currentMailFckeditorUrl = fckStorage.GetUri(StorageManager.CKEDITOR_IMAGES_DOMAIN, "").ToString();
            var currentMailAttachmentFolderUrl = MailStoragePathCombiner.GetMessageDirectory(user, StreamId);
            var currentUserStorageUrl = MailStoragePathCombiner.GetUserMailsDirectory(user);
            _logger.Info("ChangeEmbededAttachmentLinksForStoring() Fckeditor storage base url: {0}", currentMailFckeditorUrl);
            _logger.Info("ChangeEmbededAttachmentLinksForStoring() Current mail attachment folder storage base url: {0}", currentMailAttachmentFolderUrl);
            _logger.Info("ChangeEmbededAttachmentLinksForStoring() Current user folder storage base url: {0}", currentUserStorageUrl);

            var xpathQuery = StorageManager.GetXpathQueryForAttachmentsToResaving(currentMailFckeditorUrl, currentMailAttachmentFolderUrl, currentUserStorageUrl);
            _logger.Info("ChangeEmbededAttachmentLinksForStoring() Xpath Query selector for embedded attachment links: {0}", xpathQuery);
            var attachmentsForSaving = new List<MailAttachment>();

            var doc = new HtmlDocument();
            doc.LoadHtml(HtmlBody);

            var linkNodes = doc.DocumentNode.SelectNodes(xpathQuery);

            if (linkNodes != null)
            {
                foreach (var linkNode in linkNodes)
                {
                    try
                    {
                        var link = linkNode.Attributes["src"].Value;

                        _logger.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, user, tenant, MailboxId);

                        var savedAttachmentId = manager.GetAttachmentId(mailId, attach.contentId);
                        var attachmentWasSaved = savedAttachmentId != 0;
                        var currentImgStorage = isFckImage ? fckStorage : attachmentStorage;
                        var domain = isFckImage ? StorageManager.CKEDITOR_IMAGES_DOMAIN : user;

                        if (mailId == 0 || !attachmentWasSaved)
                        {
                            attach.data = StorageManager.LoadDataStoreItemData(domain, fileLink, currentImgStorage);
                            manager.StoreAttachmentWithoutQuota(tenant, user, attach);
                            attachmentsForSaving.Add(attach);
                        }

                        if (attachmentWasSaved)
                        {
                            attach = manager.GetMessageAttachment(savedAttachmentId, tenant, user);
                            var path = MailStoragePathCombiner.GerStoredFilePath(attach);
                            currentImgStorage = attachmentStorage;
                            attach.storedFileUrl =
                                MailStoragePathCombiner.GetStoredUrl(currentImgStorage.GetUri(path));
                        }

                        _logger.Info("ChangeEmbededAttachmentLinksForStoring() Restored new file_link: {0}",
                                     attach.storedFileUrl);
                        linkNode.SetAttributeValue("src", attach.storedFileUrl);
                    }
                    catch(Exception ex)
                    {
                        _logger.Error("ChangeEmbededAttachmentLinksForStoring() failed with exception: {0}", ex.ToString());
                    }
                }

                HtmlBody = doc.DocumentNode.OuterHtml;
            }

            return attachmentsForSaving;
        }
Example #6
0
        public List<MailAttachment> ChangeEmbededAttachmentLinksForStoring(int tenant, string username, int mail_id, MailBoxManager manager)
        {
            //Todo: This method can be separated in two
            var fck_storage = GetDataStoreForFckImages(tenant);
            var attachment_storage = GetDataStoreForAttachments(tenant);
            
            var current_mail_fckeditor_url = fck_storage.GetUri(EMBEDDED_DOMAIN, "").ToString();
            var current_mail_attachment_folder_url = GetThisMailFolder(username);
            var current_user_storage_url = GetUserFolder(username);

            var xpath_query = GetXpathQueryForAttachmentsToResaving(current_mail_fckeditor_url, current_mail_attachment_folder_url, current_user_storage_url);
            var attachments_for_saving = new List<MailAttachment>();

            var doc = new HtmlDocument();
            doc.LoadHtml(HtmlBody);
            
            var link_nodes = doc.DocumentNode.SelectNodes(xpath_query);

            if (link_nodes != null)
            {
                foreach (var link_node in link_nodes)
                {
                    var link = link_node.Attributes["src"].Value;
                    var is_fck_image = link.StartsWith(current_mail_fckeditor_url);
                    var prefix_length = is_fck_image ? current_mail_fckeditor_url.Length 
                                                     : link.IndexOf(current_user_storage_url, StringComparison.Ordinal) + current_user_storage_url.Length + 1;
                    var file_link = HttpUtility.UrlDecode(link.Substring(prefix_length));

                    var file_name = Path.GetFileName(file_link);
                    var attach = CreateEmbbededAttachment(file_name, link, file_link, username, tenant);

                    var saved_attachment_id = manager.GetAttachmentId(mail_id, attach.contentId);
                    var attachment_was_saved = saved_attachment_id != 0;
                    var current_img_storage = is_fck_image ? fck_storage : attachment_storage;
                    var domain = is_fck_image ? EMBEDDED_DOMAIN : username;

                    if (mail_id == 0 || !attachment_was_saved)
                    {
                        LoadAttachmentData(attach, domain, file_link, current_img_storage);
                        //TODO: Add quota if needed
                        manager.StoreAttachmentWithoutQuota(tenant, username, attach);
                        attachments_for_saving.Add(attach);
                    }

                    if (attachment_was_saved)
                    {
                        attach = manager.GetMessageAttachment(saved_attachment_id, tenant, username);
                        var path = attach.GerStoredFilePath();
                        current_img_storage = attachment_storage;
                        attach.storedFileUrl = current_img_storage.GetUri("", path).ToString();
                    }

                    link_node.SetAttributeValue("src", attach.storedFileUrl);
                }

                HtmlBody = doc.DocumentNode.OuterHtml;
            }

            return attachments_for_saving;
        }