/// <summary>
    /// Deletes the file binary from the file system.
    /// </summary>
    /// <param name="attachmentId">Attachment ID</param>
    /// <param name="name">Returning the attachment name</param>
    protected bool DeleteFromFileSystem(int attachmentId, ref string name)
    {
        // Delete the file in file system
        AttachmentInfo ai = AttachmentInfoProvider.GetAttachmentInfo(attachmentId, false);

        if (ai != null)
        {
            name = ai.AttachmentName;

            // Ensure the binary column first (check if exists)
            DataSet ds = AttachmentInfoProvider.GetAttachments("AttachmentID = " + attachmentId, null, true, 0, "CASE WHEN AttachmentBinary IS NULL THEN 0 ELSE 1 END AS HasBinary");
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                bool hasBinary = ValidationHelper.GetBoolean(ds.Tables[0].Rows[0][0], false);
                if (!hasBinary)
                {
                    // Copy the binary data to database
                    ai.AttachmentBinary = AttachmentInfoProvider.GetFile(ai, GetSiteName(ai.AttachmentSiteID));
                    ai.Generalized.UpdateData();
                }

                // Delete the file from the disk
                AttachmentInfoProvider.DeleteFile(ai.AttachmentGUID, GetSiteName(ai.AttachmentSiteID), true, false);

                return(true);
            }
        }

        return(false);
    }
    /// <summary>
    /// Copies the file binary to the database.
    /// </summary>
    /// <param name="attachmentId">Attachment ID</param>
    /// <param name="name">Returning the attachment name</param>
    protected bool CopyToDatabase(int attachmentId, ref string name)
    {
        // Copy the file from file system to the database
        AttachmentInfo ai = AttachmentInfoProvider.GetAttachmentInfo(attachmentId, true);
        if (ai != null)
        {
            name = ai.AttachmentName;

            if (ai.AttachmentBinary == null)
            {
                // Ensure the binary data
                ai.AttachmentBinary = AttachmentInfoProvider.GetFile(ai, GetSiteName(ai.AttachmentSiteID));
                ai.Generalized.UpdateData();

                return true;
            }
        }

        return false;
    }
    /// <summary>
    /// Initializes common properties used for processing image.
    /// </summary>
    private void baseImageEditor_InitializeProperties()
    {
        var currentUser = MembershipContext.AuthenticatedUser;

        // Process attachment
        switch (baseImageEditor.ImageType)
        {
        // Process physical file
        case ImageHelper.ImageTypeEnum.PhysicalFile:
        {
            if (!String.IsNullOrEmpty(filePath))
            {
                if ((currentUser != null) && currentUser.IsGlobalAdministrator)
                {
                    try
                    {
                        // Load the file from disk
                        string physicalPath = Server.MapPath(filePath);
                        byte[] data         = File.ReadAllBytes(physicalPath);
                        baseImageEditor.ImgHelper = new ImageHelper(data);
                    }
                    catch
                    {
                        baseImageEditor.LoadingFailed = true;
                        baseImageEditor.ShowError(GetString("img.errors.loading"));
                    }
                }
                else
                {
                    baseImageEditor.LoadingFailed = true;
                    baseImageEditor.ShowError(GetString("img.errors.rights"));
                }
            }
            else
            {
                baseImageEditor.LoadingFailed = true;
                baseImageEditor.ShowError(GetString("img.errors.loading"));
            }
        }
        break;

        // Process metafile
        case ImageHelper.ImageTypeEnum.Metafile:
        {
            // Get metafile
            mf = MetaFileInfoProvider.GetMetaFileInfoWithoutBinary(metafileGuid, CurrentSiteName, true);

            // If file is not null and current user is global administrator then set image
            if (mf != null)
            {
                if (UserInfoProvider.IsAuthorizedPerObject(mf.MetaFileObjectType, mf.MetaFileObjectID, PermissionsEnum.Modify, CurrentSiteName, MembershipContext.AuthenticatedUser))
                {
                    // Ensure metafile binary data
                    mf.MetaFileBinary = MetaFileInfoProvider.GetFile(mf, CurrentSiteName);
                    if (mf.MetaFileBinary != null)
                    {
                        baseImageEditor.ImgHelper = new ImageHelper(mf.MetaFileBinary);
                    }
                    else
                    {
                        baseImageEditor.LoadingFailed = true;
                        baseImageEditor.ShowError(GetString("img.errors.loading"));
                    }
                }
                else
                {
                    baseImageEditor.LoadingFailed = true;
                    baseImageEditor.ShowError(GetString("img.errors.rights"));
                }
            }
            else
            {
                baseImageEditor.LoadingFailed = true;
                baseImageEditor.ShowError(GetString("img.errors.loading"));
            }
        }
        break;

        default:
        {
            baseImageEditor.Tree = new TreeProvider(currentUser);

            // If using workflow then get versioned attachment
            if (VersionHistoryID != 0)
            {
                // Get the versioned attachment
                AttachmentHistoryInfo attachmentVersion = VersionManager.GetAttachmentVersion(VersionHistoryID, attachmentGuid);
                if (attachmentVersion != null)
                {
                    // Create new attachment object
                    ai = new AttachmentInfo(attachmentVersion.Generalized.DataClass);
                    if (ai != null)
                    {
                        AttachmentHistoryID           = attachmentVersion.AttachmentHistoryID;
                        ai.AttachmentVersionHistoryID = VersionHistoryID;
                    }
                }
            }
            // Else get file without binary data
            else
            {
                ai = AttachmentInfoProvider.GetAttachmentInfoWithoutBinary(attachmentGuid, CurrentSiteName);
            }

            // If file is not null and current user is set
            if (ai != null)
            {
                TreeNode node;
                if (ai.AttachmentDocumentID > 0)
                {
                    node = baseImageEditor.Tree.SelectSingleDocument(ai.AttachmentDocumentID);
                }
                else
                {
                    // Get parent node ID in case attachment is edited for document not created yet
                    int parentNodeId = QueryHelper.GetInteger("parentId", 0);

                    node = baseImageEditor.Tree.SelectSingleNode(parentNodeId);
                }

                // If current user has appropriate permissions then set image - check hash fro live site otherwise check node permissions
                if ((currentUser != null) && (node != null) && ((IsLiveSite && QueryHelper.ValidateHash("hash")) || (!IsLiveSite && (currentUser.IsAuthorizedPerDocument(node, NodePermissionsEnum.Modify) == AuthorizationResultEnum.Allowed))))
                {
                    // Ensure attachment binary data
                    if (VersionHistoryID == 0)
                    {
                        ai.AttachmentBinary = AttachmentInfoProvider.GetFile(ai, CurrentSiteName);
                    }

                    if (ai.AttachmentBinary != null)
                    {
                        baseImageEditor.ImgHelper = new ImageHelper(ai.AttachmentBinary);
                    }
                    else
                    {
                        baseImageEditor.LoadingFailed = true;
                        baseImageEditor.ShowError(GetString("img.errors.loading"));
                    }
                }
                else
                {
                    baseImageEditor.LoadingFailed = true;
                    baseImageEditor.ShowError(GetString("img.errors.filemodify"));
                }
            }
            else
            {
                baseImageEditor.LoadingFailed = true;
                baseImageEditor.ShowError(GetString("img.errors.loading"));
            }
        }
        break;
        }

        // Check that image is in supported formats
        if ((!baseImageEditor.LoadingFailed) && (baseImageEditor.ImgHelper.ImageFormatToString() == null))
        {
            baseImageEditor.LoadingFailed = true;
            baseImageEditor.ShowError(GetString("img.errors.format"));
        }

        // Disable editor if loading failed
        if (baseImageEditor.LoadingFailed)
        {
            Enabled = false;
        }
    }