Пример #1
0
        private string CopyTemporaryFiles(ISaveRequestHandler handler,
                                          UploadedFile[] oldFileList, UploadedFile[] newFileList, FilesToDelete filesToDelete)
        {
            foreach (var file in newFileList)
            {
                var filename = file.Filename.Trim();
                if (oldFileList.Any(x => String.Compare(x.Filename.Trim(), filename, StringComparison.OrdinalIgnoreCase) == 0))
                {
                    continue;
                }

                if (!filename.ToLowerInvariant().StartsWith("temporary/"))
                {
                    throw new InvalidOperationException("For security reasons, only temporary files can be used in uploads!");
                }

                ImageUploadBehavior.CheckUploadedImageAndCreateThumbs(attr, ref filename);

                var idField    = (Field)(((IIdRow)handler.Row).IdField);
                var copyResult = uploadHelper.CopyTemporaryFile(filename, idField.AsObject(handler.Row), filesToDelete,
                                                                s => ImageUploadBehavior.ProcessReplaceFields(s, this.replaceFields, handler));

                if (!attr.SubFolder.IsEmptyOrNull())
                {
                    copyResult.DbFileName = copyResult.DbFileName.Substring(attr.SubFolder.Length + 1);
                }

                file.Filename = copyResult.DbFileName;
            }

            return(JSON.Stringify(newFileList));
        }
Пример #2
0
        private string CopyTemporaryFiles(ISaveRequestHandler handler,
                                          UploadedFile[] oldFileList, UploadedFile[] newFileList, FilesToDelete filesToDelete)
        {
            foreach (var file in newFileList)
            {
                var filename = file.Filename.Trim();
                if (oldFileList.Any(x => String.Compare(x.Filename.Trim(), filename, StringComparison.OrdinalIgnoreCase) == 0))
                {
                    continue;
                }

                if (!filename.ToLowerInvariant().StartsWith("temporary/"))
                {
                    throw new InvalidOperationException("For security reasons, only temporary files can be used in uploads!");
                }

                var uploadHelper = new UploadHelper((subFolder.IsEmptyOrNull() ? "" : (subFolder + "/")) + fileNameFormat);
                var idField      = (Field)(((IIdRow)handler.Row).IdField);
                var copyResult   = uploadHelper.CopyTemporaryFile(filename, idField.AsObject(handler.Row), filesToDelete);
                if (subFolder != null && !this.storeSubFolderInDB)
                {
                    copyResult.DbFileName = copyResult.DbFileName.Substring(subFolder.Length + 1);
                }

                file.Filename = copyResult.DbFileName;
            }

            return(JSON.Stringify(newFileList));
        }
        private CopyTemporaryFileResult CopyTemporaryFile(ISaveRequestHandler handler)
        {
            var filename     = (StringField)(handler.Row.FindField(this.fileNameField) ?? handler.Row.FindFieldByPropertyName(fileNameField));
            var newFilename  = filename[handler.Row] = filename[handler.Row].TrimToNull();
            var uploadHelper = new UploadHelper((subFolder.IsEmptyOrNull() ? "" : (subFolder + "/")) + fileNameFormat);
            var copyResult   = uploadHelper.CopyTemporaryFile(newFilename, ((IIdRow)handler.Row).IdField[handler.Row].Value, filesToDelete);

            if (subFolder != null && !this.storeSubFolderInDB)
            {
                copyResult.DbFileName = copyResult.DbFileName.Substring(subFolder.Length + 1);
            }
            return(copyResult);
        }
Пример #4
0
        private CopyTemporaryFileResult CopyTemporaryFile(ISaveRequestHandler handler, FilesToDelete filesToDelete)
        {
            var fileName    = (StringField)Target;
            var newFilename = fileName[handler.Row] = fileName[handler.Row].TrimToNull();

            CheckUploadedImageAndCreateThumbs(attr, ref newFilename);

            var copyResult = uploadHelper.CopyTemporaryFile(newFilename, ((IIdRow)handler.Row).IdField[handler.Row].Value, filesToDelete);

            if (!attr.SubFolder.IsEmptyOrNull())
            {
                copyResult.DbFileName = copyResult.DbFileName.Substring(attr.SubFolder.Length + 1);
            }
            return(copyResult);
        }
Пример #5
0
        private CopyTemporaryFileResult CopyTemporaryFile(ISaveRequestHandler handler, FilesToDelete filesToDelete)
        {
            var fileName    = (StringField)Target;
            var newFilename = fileName[handler.Row] = fileName[handler.Row].TrimToNull();

            CheckUploadedImageAndCreateThumbs(attr, ref newFilename);

            var idField = (Field)(((IIdRow)handler.Row).IdField);

            var copyResult = uploadHelper.CopyTemporaryFile(newFilename, idField.AsObject(handler.Row), filesToDelete,
                                                            s => ImageUploadBehavior.ProcessReplaceFields(s, this.replaceFields, handler.Row));

            if (!attr.SubFolder.IsEmptyOrNull())
            {
                copyResult.DbFileName = copyResult.DbFileName.Substring(attr.SubFolder.Length + 1);
            }

            return(copyResult);
        }
Пример #6
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);
        }