private void validateFormValues(
     Validator validator, string subject, bool requireUploadIfNoFile, Action <Validator, System.Drawing.Image> validateImage, bool mustBeImage)
 {
     BlobFileOps.ValidateUploadedFile(validator, uploadedFile, acceptableFileExtensions, validateImage, mustBeImage);
     if (requireUploadIfNoFile && file == null && !uploadedFile.ValueChangedOnPostBack(AppRequestState.Instance.EwfPageRequestState.PostBackValues))
     {
         validator.NoteErrorAndAddMessage(Translation.PleaseUploadAFile + " '" + subject + "'.");
     }
 }
        // 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);
        }
        private void addFileRow(DynamicTable table, BlobFile file, ActionPostBack deletePb, List <Func <bool> > deleteModMethods)
        {
            var cells = new List <EwfTableCell>();

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

            if (thumbnailControl != null)
            {
                cells.Add(thumbnailControl);
            }

            var fileIsUnread = fileIdsMarkedAsRead != null && !fileIdsMarkedAsRead.Contains(file.FileId);

            cells.Add(
                new PostBackButton(
                    PostBack.CreateFull(
                        id: PostBack.GetCompositeId(postBackIdBase, file.FileId.ToString()),
                        firstModificationMethod: () => {
                if (fileIsUnread && markFileAsReadMethod != null)
                {
                    markFileAsReadMethod(file.FileId);
                }
            },
                        actionGetter: () => new PostBackAction(new SecondaryResponse(new BlobFileResponse(file.FileId, () => true), false))),
                    new TextActionControlStyle(file.FileName),
                    false)
            {
                ToolTip = file.FileName
            });

            cells.Add(file.UploadedDate.ToDayMonthYearString(false));
            cells.Add((fileIsUnread ? "New!" : "").ToCell(new TableCellSetup(classes: "ewfNewness".ToSingleElementArray())));

            var delete         = false;
            var deleteCheckBox =
                FormItem.Create(
                    "",
                    new EwfCheckBox(false, postBack: deletePb),
                    validationGetter: control => new EwfValidation((pbv, v) => { delete = control.IsCheckedInPostBack(pbv); }, deletePb)).ToControl();

            cells.Add(ReadOnly ? null : deleteCheckBox);
            deleteModMethods.Add(
                () => {
                if (!delete)
                {
                    return(false);
                }
                BlobFileOps.SystemProvider.DeleteFile(file.FileId);
                return(true);
            });

            table.AddRow(cells.ToArray());
        }
        private ControlList getUploadControlList()
        {
            var dm = PostBack.CreateFull(id: PostBack.GetCompositeId(postBackIdBase, "add"));

            RsFile file = null;
            var    fi   = FormItem.Create(
                "",
                new EwfFileUpload(),
                validationGetter: control => new EwfValidation(
                    (pbv, validator) => {
                BlobFileOps.ValidateUploadedFile(validator, control, acceptableFileExtensions, ValidateImage, AcceptOnlyImages);
                file = control.GetPostBackValue(pbv);
            },
                    dm));

            dm.AddModificationMethod(
                () => {
                if (file == null)
                {
                    return;
                }

                var existingFile = files.SingleOrDefault(i => i.FileName == file.FileName);
                int newFileId;
                if (existingFile != null)
                {
                    BlobFileOps.SystemProvider.UpdateFile(existingFile.FileId, file.FileName, file.Contents, BlobFileOps.GetContentTypeForPostedFile(file));
                    newFileId = existingFile.FileId;
                }
                else
                {
                    newFileId = BlobFileOps.SystemProvider.InsertFile(fileCollectionId, file.FileName, file.Contents, BlobFileOps.GetContentTypeForPostedFile(file));
                }

                if (NewFileNotificationMethod != null)
                {
                    NewFileNotificationMethod(newFileId);
                }
                EwfPage.AddStatusMessage(StatusMessageType.Info, "File uploaded successfully.");
            });

            return(ControlList.CreateWithControls(
                       true,
                       "Select and upload a new file:",
                       fi.ToControl(),
                       new PostBackButton(dm, new ButtonActionControlStyle("Upload new file"), false)));
        }
        /// <summary>
        /// Call this during ModifyData.  This returns the file collection ID of the existing or just-inserted file collection.
        /// This will return an ID even if there are 0 files in the collection (in other words, no file). No file should always be
        /// represented by a non-null file collection ID which happens to have 0 files in it.  Null file collection IDs are not supported.
        /// </summary>
        public int ModifyData()
        {
            if (fileCollectionId == null)
            {
                fileCollectionId = BlobFileOps.SystemProvider.InsertFileCollection();
            }

            var rsFile = uploadedFile.GetPostBackValue(AppRequestState.Instance.EwfPageRequestState.PostBackValues);

            if (rsFile != null)
            {
                BlobFileOps.SystemProvider.DeleteFilesLinkedToFileCollection(fileCollectionId.Value);
                BlobFileOps.SystemProvider.InsertFile(fileCollectionId.Value, rsFile.FileName, rsFile.Contents, BlobFileOps.GetContentTypeForPostedFile(rsFile));
            }
            return(fileCollectionId.Value);
        }
 /// <summary>
 /// Performs validate form values, but also forces the file to be an image of the specified dimensions.
 /// </summary>
 public void ValidateAsImage(Validator validator, string subject, bool requireUploadIfNoFile, int width, int height)
 {
     ValidateAsImage(validator, subject, requireUploadIfNoFile, BlobFileOps.GetWidthAndHeightImageValidationMethod(subject, width, height));
 }