// NOTE: EVERYTHING should be done here. We shouldn't have LoadData. We should audit everyone using this control and see if we can improve things.
        // NOTE: This should also be full of delegates that run when events (such as deleting a file) are occurring.
        // NOTE: There should be a way to tell if a file was uploaded.
        void ControlTreeDataLoader.LoadData()
        {
            if (fileCollectionId != null)
            {
                file = BlobFileOps.GetFirstFileFromCollection(fileCollectionId.Value);
            }

            var controlStack = ControlStack.Create(true);

            if (file != null)
            {
                var download = new PostBackButton(
                    PostBack.CreateFull(
                        id: PostBack.GetCompositeId("ewfFile", file.FileId.ToString()),
                        actionGetter: () => {
                    // Refresh the file here in case a new one was uploaded on the same post-back.
                    return
                    (new PostBackAction(
                         new SecondaryResponse(new BlobFileResponse(BlobFileOps.GetFirstFileFromCollection(fileCollectionId.Value).FileId, () => true), false)));
                }),
                    new TextActionControlStyle(Translation.DownloadExisting + " (" + file.FileName + ")"),
                    false);
                controlStack.AddControls(download);
            }
            else if (!HideNoExistingFileMessage)
            {
                controlStack.AddControls(new Label {
                    Text = Translation.NoExistingFile
                });
            }

            uploadedFile = new EwfFileUpload();
            if (file != null)
            {
                uploadedFile.SetInitialDisplay(false);
                var replaceExistingFileLink = new ToggleButton(
                    uploadedFile.ToSingleElementArray(),
                    new TextActionControlStyle(Translation.ClickHereToReplaceExistingFile))
                {
                    AlternateText = ""
                };
                controlStack.AddControls(replaceExistingFileLink);
            }

            controlStack.AddControls(uploadedFile);

            var thumbnailControl = BlobFileOps.GetThumbnailControl(file, ThumbnailResourceInfoCreator);

            if (thumbnailControl != null)
            {
                Controls.Add(thumbnailControl);
            }
            Controls.Add(controlStack);
        }
예제 #2
0
        public BlobFileManager(int?fileCollectionId)
        {
            this.fileCollectionId = fileCollectionId;

            if (fileCollectionId != null)
            {
                file = BlobFileOps.GetFirstFileFromCollection(fileCollectionId.Value);
            }

            var controlStack = ControlStack.Create(true);

            if (file != null)
            {
                var download = new PostBackButton(
                    new TextActionControlStyle(Translation.DownloadExisting + " (" + file.FileName + ")"),
                    usesSubmitBehavior: false,
                    postBack: PostBack.CreateFull(
                        id: PostBack.GetCompositeId("ewfFile", file.FileId.ToString()),
                        actionGetter: () => {
                    // Refresh the file here in case a new one was uploaded on the same post-back.
                    return
                    (new PostBackAction(
                         new SecondaryResponse(new BlobFileResponse(BlobFileOps.GetFirstFileFromCollection(fileCollectionId.Value).FileId, () => true), false)));
                }));
                controlStack.AddControls(download);
            }
            else if (!HideNoExistingFileMessage)
            {
                controlStack.AddControls(new Label {
                    Text = Translation.NoExistingFile
                });
            }

            uploadedFile = new EwfFileUpload();
            if (file != null)
            {
                uploadedFile.SetInitialDisplay(false);
                var replaceExistingFileLink = new ToggleButton(
                    uploadedFile.ToCollection(),
                    new TextActionControlStyle(Translation.ClickHereToReplaceExistingFile),
                    false,
                    (postBackValue, validator) => { })
                {
                    AlternateText = ""
                };
                controlStack.AddControls(replaceExistingFileLink);
            }

            controlStack.AddControls(uploadedFile);

            this.AddControlsReturnThis(BlobFileOps.GetThumbnailControl(file, ThumbnailResourceInfoCreator));
            Controls.Add(controlStack);
        }
예제 #3
0
        /// <summary>
        /// Uploaded file cannot be null. But if uploadedFile.HasFile is false, this will be a no-op.
        /// Pass null for acceptableFileExtensions if there is no restriction on file extension.
        /// PerformAdditionalImageValidation cannot be null but may be an empty delegate.
        /// </summary>
        public static void ValidateUploadedFile(
            Validator validator, EwfFileUpload uploadedFile, string[] acceptableFileExtensions, Action <Validator, System.Drawing.Image> performAdditionalImageValidation,
            bool mustBeRenderableImage)
        {
            var file = uploadedFile.GetPostBackValue(AppRequestState.Instance.EwfPageRequestState.PostBackValues);

            if (file == null)
            {
                return;
            }

            // Perform generic file validation.
            if (acceptableFileExtensions != null && !FileExtensions.MatchesAGivenExtension(file.FileName, acceptableFileExtensions))
            {
                validator.NoteErrorAndAddMessage(Translation.UnacceptableFileExtension + " " + acceptableFileExtensions.GetCommaDelimitedStringFromCollection());
                // Don't bother trying to see if it's an image and parse the image. The file extension message be more detailed than the messages those errors produce.
                return;
            }

            // Perform image-specific validation if necessary.
            if (mustBeRenderableImage)
            {
                // Make sure it is an image according to its content type.
                if (!ContentTypes.IsImageType(GetContentTypeForPostedFile(file)))
                {
                    validator.NoteErrorAndAddMessage("Please upload a valid image file.");
                }
                else
                {
                    // Make sure it is an image type that we understand. Also perform optional custom validation.
                    try {
                        using (var stream = new MemoryStream(file.Contents)) {
                            var image = System.Drawing.Image.FromStream(stream);
                            performAdditionalImageValidation(validator, image);
                        }
                    }
                    catch (ArgumentException) {
                        // If we end up in this catch block, it means that System.Drawing.Image does not understand our image. Since we already know that our content type
                        // is image at this point, this usually means that the file is some sort of unsupported image format, like NEF.
                        validator.NoteErrorAndAddMessage("The uploaded image file is in an unsupported format.");
                    }
                }
            }
        }
        // NOTE: EVERYTHING should be done here. We shouldn't have LoadData. We should audit everyone using this control and see if we can improve things.
        // NOTE: This should also be full of delegates that run when events (such as deleting a file) are occurring.
        // NOTE: There should be a way to tell if a file was uploaded.
        void ControlTreeDataLoader.LoadData()
        {
            if( fileCollectionId != null )
                file = BlobFileOps.GetFirstFileFromCollection( fileCollectionId.Value );

            var controlStack = ControlStack.Create( true );
            if( file != null ) {
                var download = new PostBackButton(
                    PostBack.CreateFull(
                        id: PostBack.GetCompositeId( "ewfFile", file.FileId.ToString() ),
                        actionGetter: () => {
                            // Refresh the file here in case a new one was uploaded on the same post-back.
                            return
                                new PostBackAction(
                                    new SecondaryResponse( new BlobFileResponse( BlobFileOps.GetFirstFileFromCollection( fileCollectionId.Value ).FileId, () => true ), false ) );
                        } ),
                    new TextActionControlStyle( Translation.DownloadExisting + " (" + file.FileName + ")" ),
                    false );
                controlStack.AddControls( download );
            }
            else if( !HideNoExistingFileMessage )
                controlStack.AddControls( new Label { Text = Translation.NoExistingFile } );

            uploadedFile = new EwfFileUpload();
            if( file != null ) {
                uploadedFile.SetInitialDisplay( false );
                var replaceExistingFileLink = new ToggleButton(
                    uploadedFile.ToSingleElementArray(),
                    new TextActionControlStyle( Translation.ClickHereToReplaceExistingFile ) ) { AlternateText = "" };
                controlStack.AddControls( replaceExistingFileLink );
            }

            controlStack.AddControls( uploadedFile );

            var thumbnailControl = BlobFileOps.GetThumbnailControl( file, ThumbnailResourceInfoCreator );
            if( thumbnailControl != null )
                Controls.Add( thumbnailControl );
            Controls.Add( controlStack );
        }
        /// <summary>
        /// Uploaded file cannot be null. But if uploadedFile.HasFile is false, this will be a no-op.
        /// Pass null for acceptableFileExtensions if there is no restriction on file extension.
        /// PerformAdditionalImageValidation cannot be null but may be an empty delegate.
        /// </summary>
        public static void ValidateUploadedFile(
            Validator validator, EwfFileUpload uploadedFile, string[] acceptableFileExtensions, Action<Validator, System.Drawing.Image> performAdditionalImageValidation,
            bool mustBeRenderableImage)
        {
            var file = uploadedFile.GetPostBackValue( AppRequestState.Instance.EwfPageRequestState.PostBackValues );
            if( file == null )
                return;

            // Perform generic file validation.
            if( acceptableFileExtensions != null && !FileExtensions.MatchesAGivenExtension( file.FileName, acceptableFileExtensions ) ) {
                validator.NoteErrorAndAddMessage( Translation.UnacceptableFileExtension + " " + acceptableFileExtensions.GetCommaDelimitedStringFromCollection() );
                // Don't bother trying to see if it's an image and parse the image. The file extension message be more detailed than the messages those errors produce.
                return;
            }

            // Perform image-specific validation if necessary.
            if( mustBeRenderableImage ) {
                // Make sure it is an image according to its content type.
                if( !ContentTypes.IsImageType( GetContentTypeForPostedFile( file ) ) )
                    validator.NoteErrorAndAddMessage( "Please upload a valid image file." );
                else {
                    // Make sure it is an image type that we understand. Also perform optional custom validation.
                    try {
                        using( var stream = new MemoryStream( file.Contents ) ) {
                            var image = System.Drawing.Image.FromStream( stream );
                            performAdditionalImageValidation( validator, image );
                        }
                    }
                    catch( ArgumentException ) {
                        // If we end up in this catch block, it means that System.Drawing.Image does not understand our image. Since we already know that our content type
                        // is image at this point, this usually means that the file is some sort of unsupported image format, like NEF.
                        validator.NoteErrorAndAddMessage( "The uploaded image file is in an unsupported format." );
                    }
                }
            }
        }