Example #1
0
        private ServiceResponse HandleUploadRequest(HttpContext context)
        {
            if (context.Request.Form.Files.Count != 1)
            {
                throw new ArgumentOutOfRangeException("files");
            }

            var file = context.Request.Form.Files[0];

            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (file.FileName.IsEmptyOrNull())
            {
                throw new ArgumentNullException("filename");
            }

            var processor = new UploadProcessor(UploadStorage, Logger)
            {
                ThumbWidth  = 128,
                ThumbHeight = 96
            };

            if (processor.ProcessStream(file.OpenReadStream(),
                                        Path.GetExtension(file.FileName), Localizer))
            {
                var temporaryFile = processor.TemporaryFile;
                UploadStorage.SetOriginalName(temporaryFile, file.FileName);

                return(new UploadResponse()
                {
                    TemporaryFile = temporaryFile,
                    Size = processor.FileSize,
                    IsImage = processor.IsImage,
                    Width = processor.ImageWidth,
                    Height = processor.ImageHeight
                });
            }
            else
            {
                return(new UploadResponse()
                {
                    Error = new ServiceError()
                    {
                        Code = "Exception",
                        Message = processor.ErrorMessage
                    }
                });
            }
        }
Example #2
0
        private ServiceResponse HandleUploadRequest(HttpContextBase context)
        {
            if (context.Request.Files.Count != 1)
            {
                throw new ArgumentOutOfRangeException("files");
            }

            HttpPostedFileBase file = context.Request.Files[0];

            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (file.FileName.IsEmptyOrNull())
            {
                throw new ArgumentNullException("filename");
            }

            var processor = new UploadProcessor
            {
                ThumbWidth  = 128,
                ThumbHeight = 96
            };

            if (processor.ProcessStream(file.InputStream, Path.GetExtension(file.FileName)))
            {
                var temporaryFile = "temporary/" + Path.GetFileName(processor.FilePath);
                using (var sw = new StreamWriter(Path.ChangeExtension(UploadHelper.DbFilePath(temporaryFile), ".orig")))
                    sw.WriteLine(file.FileName);

                return(new UploadResponse()
                {
                    TemporaryFile = temporaryFile,
                    Size = processor.FileSize,
                    IsImage = processor.IsImage,
                    Width = processor.ImageWidth,
                    Height = processor.ImageHeight
                });
            }
            else
            {
                return(new UploadResponse()
                {
                    Error = new ServiceError()
                    {
                        Code = "Exception",
                        Message = processor.ErrorMessage
                    }
                });
            }
        }
Example #3
0
        public static AttachmentList GetAttachmentList(MimeMessage message,
                                                       bool forReply, FilesToDelete filesToDelete)
        {
            var uploadHelper = new UploadHelper("Nortwind/Mail/{3:yyyyMM}/{4}");

            var result = new AttachmentList();

            result.ByIndex = new Dictionary <int, AttachmentInfo>();

            result.InOrder = new List <AttachmentInfo>();
            var attachmentNumber = 0;
            var visitor          = new EmailPreviewVisitor(message, (mime, embedUrl) =>
            {
                var sameAttachment = result.InOrder.FirstOrDefault(x => x.Mime == mime);
                if (sameAttachment != null)
                {
                    sameAttachment.IsEmbedded = sameAttachment.IsEmbedded || embedUrl != null;
                    return(sameAttachment.Url);
                }

                ++attachmentNumber;
                string fileName;
                string downUrl = null;

                byte[] buffer;
                using (var output = new MemoryStream())
                {
                    (mime as MimePart).Content.DecodeTo(output);
                    buffer = output.GetBuffer();
                }

                if (mime is MessagePart)
                {
                    fileName = mime.ContentDisposition?.FileName ?? (mime.ContentType.Name ?? "attached" + attachmentNumber + ".eml");
                }
                else
                {
                    fileName = ((MimePart)mime).FileName ?? "file" + attachmentNumber + ".dat";
                }

                if (embedUrl != null)
                {
                    downUrl = string.Format("data:{0};base64,{1}",
                                            mime.ContentType.MimeType, Convert.ToBase64String(buffer, 0, buffer.Length));
                }
                else
                {
                    var processor = new UploadProcessor
                    {
                        ThumbWidth  = 128,
                        ThumbHeight = 96
                    };

                    if (processor.ProcessStream(new MemoryStream(buffer), Path.GetExtension(fileName)))
                    {
                        var temporaryFile = "temporary/" + Path.GetFileName(processor.FilePath);
                        using (var sw = new StreamWriter(Path.ChangeExtension(UploadHelper.DbFilePath(temporaryFile), ".orig")))
                            sw.WriteLine(fileName);

                        var copyResult = uploadHelper.CopyTemporaryFile(temporaryFile, -1, filesToDelete);
                        downUrl        = "/upload/" + copyResult.DbFileName;
                    }
                    else
                    {
                        downUrl = "?";
                    }
                }

                var info = new AttachmentInfo
                {
                    Key        = attachmentNumber,
                    Filename   = fileName,
                    Mime       = mime,
                    Url        = downUrl,
                    IsEmbedded = embedUrl != null,
                    Size       = buffer.Length
                };

                result.ByIndex[attachmentNumber] = info;
                result.InOrder.Add(info);
                return(downUrl);
            }, forReply);

            message.Accept(visitor);
            result.HtmlBody = visitor.HtmlBody;

            return(result);
        }