Наследование: Serenity.Services.BaseSaveDeleteBehavior, IImplicitBehavior, IFieldBehavior
Пример #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
        public bool ActivateFor(Row row)
        {
            if (ReferenceEquals(null, Target))
            {
                return(false);
            }

            attr = Target.GetAttribute <ImageUploadEditorAttribute>();
            if (attr == null || attr.DisableDefaultBehavior || attr.EditorType != "MultipleImageUpload")
            {
                return(false);
            }

            if (!(Target is StringField))
            {
                throw new ArgumentException(String.Format(
                                                "Field '{0}' on row type '{1}' has a UploadEditor attribute but it is not a String field!",
                                                Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            if (!(row is IIdRow))
            {
                throw new ArgumentException(String.Format(
                                                "Field '{0}' on row type '{1}' has a UploadEditor attribute but Row type doesn't implement IIdRow!",
                                                Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            var format = attr.FilenameFormat;

            if (format == null)
            {
                format = row.GetType().Name;
                if (format.EndsWith("Row"))
                {
                    format = format.Substring(0, format.Length - 3);
                }
                format += "/~";
            }

            this.fileNameFormat = format.Replace("~", SplittedFormat);
            this.replaceFields  = ImageUploadBehavior.ParseReplaceFields(this.fileNameFormat, row, Target);

            this.uploadHelper = new UploadHelper((attr.SubFolder.IsEmptyOrNull() ? "" : (attr.SubFolder + "/")) + (this.fileNameFormat));

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