示例#1
0
        // TODO: (MH) (core) This is only used in one ocasion. Use code there (QueuedEmailController > DownloadAttachment) directly.
        public virtual byte[] LoadQueuedMailAttachmentBinary(QueuedEmailAttachment attachment)
        {
            Guard.NotNull(attachment, nameof(attachment));

            if (attachment.StorageLocation == EmailAttachmentStorageLocation.Blob)
            {
                return(attachment.MediaStorage?.Data ?? Array.Empty <byte>());
            }

            return(null);
        }
示例#2
0
        private async Task <QueuedEmailAttachment> CreatePdfInvoiceAttachmentAsync(int orderId)
        {
            QueuedEmailAttachment attachment = null;
            var request = _urlHelper.ActionContext.HttpContext.Request;
            var path    = _urlHelper.Action("Print", "Order", new { id = orderId, pdf = true, area = string.Empty });
            var url     = WebHelper.GetAbsoluteUrl(path, request);

            var downloadRequest = WebRequest.CreateHttp(url);

            downloadRequest.UserAgent = "Smartstore";
            downloadRequest.Timeout   = 5000;
            downloadRequest.SetAuthenticationCookie(request);
            downloadRequest.SetVisitorCookie(request);

            using (var response = (HttpWebResponse)await downloadRequest.GetResponseAsync())
                using (var stream = response.GetResponseStream())
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        attachment = new QueuedEmailAttachment
                        {
                            StorageLocation = EmailAttachmentStorageLocation.Blob,
                            MimeType        = response.ContentType,
                            MediaStorage    = new MediaStorage
                            {
                                // INFO: stream.Length not supported here.
                                Data = await stream.ToByteArrayAsync()
                            }
                        };

                        var contentDisposition = response.Headers["Content-Disposition"];
                        if (contentDisposition.HasValue())
                        {
                            attachment.Name = new ContentDisposition(contentDisposition).FileName;
                        }

                        if (attachment.Name.IsEmpty())
                        {
                            attachment.Name = WebHelper.GetFileNameFromUrl(url);
                        }
                    }
                }

            if (attachment == null)
            {
                throw new InvalidOperationException(T("Admin.System.QueuedEmails.ErrorEmptyAttachmentResult", path));
            }
            else if (!attachment.MimeType.EqualsNoCase("application/pdf"))
            {
                throw new InvalidOperationException(T("Admin.System.QueuedEmails.ErrorNoPdfAttachment"));
            }

            return(attachment);
        }