Beispiel #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);
        }
        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;
            }
        }