Example #1
0
        public void StoreAttachmentCopy(int tenant, string user, MailAttachmentData attachment, string streamId)
        {
            try
            {
                if (attachment.streamId.Equals(streamId) && !attachment.isTemp)
                {
                    return;
                }

                string s3Key;

                var dataClient = MailDataStore.GetDataStore(tenant);

                if (attachment.needSaveToTemp || attachment.isTemp)
                {
                    s3Key = MailStoragePathCombiner.GetTempStoredFilePath(attachment);
                }
                else
                {
                    s3Key = MailStoragePathCombiner.GerStoredFilePath(attachment);
                }

                if (!dataClient.IsFile(s3Key))
                {
                    return;
                }

                attachment.fileNumber =
                    !string.IsNullOrEmpty(attachment.contentId) //Upload hack: embedded attachment have to be saved in 0 folder
                        ? 0
                        : attachment.fileNumber;

                var newS3Key = MailStoragePathCombiner.GetFileKey(user, streamId, attachment.fileNumber,
                                                                  attachment.storedName);

                var copyS3Url = dataClient.Copy(s3Key, string.Empty, newS3Key);

                attachment.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(copyS3Url);

                attachment.streamId = streamId;

                attachment.tempStoredUrl = null;

                Log.DebugFormat("StoreAttachmentCopy() tenant='{0}', user_id='{1}', stream_id='{2}', new_s3_key='{3}', copy_s3_url='{4}', storedFileUrl='{5}',  filename='{6}'",
                                tenant, user, streamId, newS3Key, copyS3Url, attachment.storedFileUrl, attachment.fileName);
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("CopyAttachment(). filename='{0}', ctype='{1}' Exception:\r\n{2}\r\n",
                                attachment.fileName,
                                attachment.contentType,
                                ex.ToString());

                throw;
            }
        }
        public void StoreAttachmentWithoutQuota(int tenant, string user, MailAttachment attachment, IDataStore storage)
        {
            try
            {
                if (attachment.data == null || attachment.data.Length == 0)
                {
                    return;
                }

                if (string.IsNullOrEmpty(attachment.fileName))
                {
                    attachment.fileName = "attachment.ext";
                }

                var contentDisposition = MailStoragePathCombiner.PrepareAttachmentName(attachment.fileName);

                var ext = Path.GetExtension(attachment.fileName);

                attachment.storedName = CreateNewStreamId();

                if (!string.IsNullOrEmpty(ext))
                {
                    attachment.storedName = Path.ChangeExtension(attachment.storedName, ext);
                }

                attachment.fileNumber =
                    !string.IsNullOrEmpty(attachment.contentId) //Upload hack: embedded attachment have to be saved in 0 folder
                        ? 0
                        : attachment.fileNumber;

                var attachmentPath = MailStoragePathCombiner.GerStoredFilePath(attachment);

                using (var reader = new MemoryStream(attachment.data))
                {
                    var uploadUrl = storage.UploadWithoutQuota(string.Empty, attachmentPath, reader, attachment.contentType, contentDisposition);
                    attachment.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(uploadUrl);
                }
            }
            catch (Exception e)
            {
                _log.Error("StoreAttachmentWithoutQuota(). filename: {0}, ctype: {1} Exception:\r\n{2}\r\n",
                           attachment.fileName,
                           attachment.contentType,
                           e.ToString());

                throw;
            }
        }
        public void StoreAttachmentCopy(int id_tenant, string id_user, MailAttachment attachment, string stream_id)
        {
            try
            {
                if (attachment.streamId.Equals(stream_id))
                {
                    return;
                }

                var s3_key = MailStoragePathCombiner.GerStoredFilePath(attachment);

                var data_client = MailDataStore.GetDataStore(id_tenant);

                if (!data_client.IsFile(s3_key))
                {
                    return;
                }

                attachment.fileNumber =
                    !string.IsNullOrEmpty(attachment.contentId) //Upload hack: embedded attachment have to be saved in 0 folder
                        ? 0
                        : attachment.fileNumber;

                var new_s3_key = MailStoragePathCombiner.GetFileKey(id_user, stream_id, attachment.fileNumber,
                                                                    attachment.storedName);

                var copy_s3_url = data_client.Copy(s3_key, string.Empty, new_s3_key);

                attachment.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(copy_s3_url);

                attachment.streamId = stream_id;

                _log.Debug("StoreAttachmentCopy() tenant='{0}', user_id='{1}', stream_id='{2}', new_s3_key='{3}', copy_s3_url='{4}', storedFileUrl='{5}',  filename='{6}'",
                           id_tenant, id_user, stream_id, new_s3_key, copy_s3_url, attachment.storedFileUrl, attachment.fileName);
            }
            catch (Exception ex)
            {
                _log.Error("CopyAttachment(). filename='{0}', ctype='{1}' Exception:\r\n{2}\r\n",
                           attachment.fileName,
                           attachment.contentType,
                           ex.ToString());

                throw;
            }
        }
        public string GetMailEmlUrl(int tenant, string user, string streamId)
        {
            // Using id_user as domain in S3 Storage - allows not to add quota to tenant.
            var emlPath   = MailStoragePathCombiner.GetEmlKey(user, streamId);
            var dataStore = MailDataStore.GetDataStore(tenant);

            try
            {
                var emlUri = dataStore.GetUri(string.Empty, emlPath);
                var url    = MailStoragePathCombiner.GetStoredUrl(emlUri);

                return(url);
            }
            catch (Exception ex)
            {
                _log.Error("GetMailEmlUrl() tenant='{0}', user_id='{1}', save_eml_path='{2}' Exception: {3}",
                           tenant, user, emlPath, ex.ToString());
            }

            return("");
        }
        public string StoreCKeditorImageWithoutQuota(int tenant, string user, int mailboxId, string fileName, byte[] imageData, IDataStore storage)
        {
            try
            {
                if (imageData == null || imageData.Length == 0)
                {
                    throw new ArgumentNullException("imageData");
                }

                var ext = string.IsNullOrEmpty(fileName) ? ".jpg" : Path.GetExtension(fileName);

                if (string.IsNullOrEmpty(ext))
                {
                    ext = ".jpg";
                }

                var storeName = imageData.GetMd5();
                storeName = Path.ChangeExtension(storeName, ext);

                var contentDisposition = ContentDispositionUtil.GetHeaderValue(storeName);
                var contentType        = MimeTypesHelper.GetMimeqType(ext);

                var signatureImagePath = MailStoragePathCombiner.GerStoredSignatureImagePath(mailboxId, storeName);

                using (var reader = new MemoryStream(imageData))
                {
                    var uploadUrl = storage.UploadWithoutQuota(user, signatureImagePath, reader, contentType, contentDisposition);
                    return(MailStoragePathCombiner.GetStoredUrl(uploadUrl));
                }
            }
            catch (Exception e)
            {
                _log.Error("StoreCKeditorImageWithoutQuota(). filename: {0} Exception:\r\n{1}\r\n", fileName,
                           e.ToString());

                throw;
            }
        }
Example #6
0
        public void StoreAttachmentCopy(int id_tenant, string id_user, MailAttachment attachment, string stream_id)
        {
            try
            {
                if (!attachment.streamId.Equals(stream_id))
                {
                    var s3_key = attachment.GerStoredFilePath();

                    var data_client = GetDataStore(id_tenant);

                    if (data_client.IsFile(s3_key))
                    {
                        attachment.fileNumber =
                            !string.IsNullOrEmpty(attachment.contentId) //Upload hack: embedded attachment have to be saved in 0 folder
                                ? 0
                                : attachment.fileNumber;

                        var new_s3_key = MailStoragePathCombiner.GetFileKey(id_user, stream_id, attachment.fileNumber,
                                                                            attachment.storedName);

                        var copy_s3_url = data_client.Copy(s3_key, string.Empty, new_s3_key);

                        attachment.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(copy_s3_url);

                        attachment.streamId = stream_id;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Error("CopyAttachment(). filename: {0}, ctype: {1} Exception:\r\n{2}\r\n",
                           attachment.fileName,
                           attachment.contentType,
                           ex.ToString());

                throw;
            }
        }
        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);
        }