Ejemplo n.º 1
0
        public static AttachmentStream ToAttachmentStream(this MailAttachmentData mailAttachmentData, int offset = 0)
        {
            if (mailAttachmentData == null)
            {
                throw new InvalidOperationException("Attachment not found");
            }

            var storage        = MailDataStore.GetDataStore(mailAttachmentData.tenant);
            var attachmentPath = MailStoragePathCombiner.GerStoredFilePath(mailAttachmentData);
            var result         = new AttachmentStream
            {
                FileStream = storage.GetReadStream("", attachmentPath, offset),
                FileName   = mailAttachmentData.fileName,
                FileSize   = mailAttachmentData.size
            };

            return(result);
        }
Ejemplo n.º 2
0
        public string StoreCKeditorImageWithoutQuota(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        = MimeMapping.GetMimeMapping(ext);

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

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

                throw;
            }
        }
Ejemplo n.º 3
0
        public void StoreAttachmentWithoutQuota(MailAttachmentData mailAttachmentData)
        {
            try
            {
                if ((mailAttachmentData.dataStream == null || mailAttachmentData.dataStream.Length == 0) && (mailAttachmentData.data == null || mailAttachmentData.data.Length == 0))
                {
                    return;
                }

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

                var storage = MailDataStore.GetDataStore(Tenant);

                storage.QuotaController = null;

                if (string.IsNullOrEmpty(mailAttachmentData.storedName))
                {
                    mailAttachmentData.storedName = MailUtil.CreateStreamId();

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

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

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

                var attachmentPath = MailStoragePathCombiner.GerStoredFilePath(mailAttachmentData);

                if (mailAttachmentData.data != null)
                {
                    using (var reader = new MemoryStream(mailAttachmentData.data))
                    {
                        var uploadUrl = (mailAttachmentData.needSaveToTemp)
                            ? storage.Save("attachments_temp", attachmentPath, reader, mailAttachmentData.fileName)
                            : storage.Save(attachmentPath, reader, mailAttachmentData.fileName);

                        mailAttachmentData.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(uploadUrl);
                    }
                }
                else
                {
                    var uploadUrl = (mailAttachmentData.needSaveToTemp)
                        ? storage.Save("attachments_temp", attachmentPath, mailAttachmentData.dataStream, mailAttachmentData.fileName)
                        : storage.Save(attachmentPath, mailAttachmentData.dataStream, mailAttachmentData.fileName);

                    mailAttachmentData.storedFileUrl = MailStoragePathCombiner.GetStoredUrl(uploadUrl);
                }

                if (mailAttachmentData.needSaveToTemp)
                {
                    mailAttachmentData.tempStoredUrl = mailAttachmentData.storedFileUrl;
                }
            }
            catch (Exception e)
            {
                Log.ErrorFormat("StoreAttachmentWithoutQuota(). filename: {0}, ctype: {1} Exception:\r\n{2}\r\n",
                                mailAttachmentData.fileName,
                                mailAttachmentData.contentType,
                                e.ToString());

                throw;
            }
        }