Imports BVSoftware.BVC.Core Imports System.IO Imports System.Drawing Imports System.Text Public Class ImageBrowser Inherits BaseAdminPage #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Protected WithEvents MetaDescriptionControl As BVSoftware.WebControls.MetaTag Protected WithEvents MetaKeywordsControl As BVSoftware.WebControls.MetaTag Protected WithEvents imgPreview As System.Web.UI.WebControls.Image Protected WithEvents lblImageInfo As System.Web.UI.WebControls.Label Protected WithEvents btnSelect As System.Web.UI.WebControls.ImageButton Protected WithEvents btnCancel As System.Web.UI.WebControls.ImageButton Protected WithEvents chkOptimize As System.Web.UI.WebControls.CheckBox Protected WithEvents lstOptimize As System.Web.UI.WebControls.DropDownList Protected WithEvents UploadFileField As System.Web.UI.HtmlControls.HtmlInputFile Protected WithEvents ResizeWidthField As System.Web.UI.WebControls.TextBox Protected WithEvents ResizeHeightField As System.Web.UI.WebControls.TextBox Protected WithEvents btnUpload As System.Web.UI.WebControls.ImageButton Protected WithEvents btnDelete As MetaBuilders.WebControls.ConfirmedImageButton Protected WithEvents rbResizeCustom As System.Web.UI.WebControls.RadioButton Protected WithEvents rbResizeMedium As System.Web.UI.WebControls.RadioButton Protected WithEvents rbResizeSmall As System.Web.UI.WebControls.RadioButton Protected WithEvents rbResizeNone As System.Web.UI.WebControls.RadioButton Protected WithEvents msg As BVSoftware.WebControls.WebPageMessage Protected WithEvents chkRename As System.Web.UI.WebControls.CheckBox Protected WithEvents RenameField As System.Web.UI.WebControls.TextBox Protected WithEvents FilesListRepeater As System.Web.UI.WebControls.Repeater Protected WithEvents NewFolderField As System.Web.UI.WebControls.TextBox Protected WithEvents btnNewFolder As System.Web.UI.WebControls.ImageButton Protected WithEvents FileCopyField As System.Web.UI.WebControls.TextBox Protected WithEvents btnFileCopy As System.Web.UI.WebControls.ImageButton Protected WithEvents FileRenameField As System.Web.UI.WebControls.TextBox Protected WithEvents btnFileRename As System.Web.UI.WebControls.ImageButton 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load BVC2004Store.CheckThisPage(Security.RolePermission.LoginToAdmin) If Not Page.IsPostBack Then MetaKeywordsControl.Content = WebAppSettings.MetaKeywords MetaDescriptionControl.Content = WebAppSettings.MetaDescription Me.rbResizeSmall.Text = "Resize to Small (" & WebAppSettings.ImagesSmallWidth & " x " & WebAppSettings.ImagesSmallHeight & ")" Me.rbResizeMedium.Text = "Resize to Medium (" & WebAppSettings.ImagesMediumWidth & " x " & WebAppSettings.ImagesMediumHeight & ")" Me.ResizeWidthField.Text = WebAppSettings.ImagesSmallWidth Me.ResizeHeightField.Text = WebAppSettings.ImagesSmallHeight If Not Request.Params("ReturnURL") Is Nothing Then ViewState("ReturnURL") = Request.Params("ReturnURL") End If If Not Request.Params("ReturnName") Is Nothing Then ViewState("ReturnName") = Request.Params("ReturnName") End If ' Look for start DIR request If Not Request.Params("StartDir") Is Nothing Then SetStartDir(Request.Params("StartDir")) Else SetStartDir("") End If ' Look for selected file requests If Not Request.Params("SelectedFile") Is Nothing Then ViewState("StartDir") = Request.Params("SelectedFile") End If If ViewState("SelectedFile") Is Nothing Then ViewState("SelectedFile") = "" End If End If Trace.Write("Populating List in Page Load") Trace.Write("View State Dir = " & ViewState("StartDir")) PopulateFileList(ViewState("StartDir")) End Sub Private Sub PopulateFileList(ByVal startDir As String) startDir = startDir.TrimStart("/") startDir = startDir.TrimStart("\") Dim physicalStart As String = Request.PhysicalApplicationPath physicalStart = Path.Combine(physicalStart, startDir) Dim parentDir As DirectoryInfo Dim childDirs As DirectoryInfo() Dim childFiles As FileInfo() parentDir = New DirectoryInfo(physicalStart) childDirs = parentDir.GetDirectories childFiles = parentDir.GetFiles If Directory.Exists(physicalStart) = True Then ' Up Folder Link Dim lastSlash As Integer = startDir.LastIndexOf("\") Dim location As String = "" If lastSlash > 0 Then location = startDir.Substring(0, lastSlash) Else location = "" End If Dim dt As New DataTable dt.Columns.Add("Link", System.Type.GetType("System.String")) dt.Columns.Add("CommandArgument", System.Type.GetType("System.String")) dt.Columns.Add("CommandName", System.Type.GetType("System.String")) dt.Columns.Add("LinkType", System.Type.GetType("System.String")) If startDir.Length > 0 Then Dim linkText As String = " .." Dim cmdArg As String = location Dim dr As DataRow = dt.NewRow() dr.Item("Link") = linkText dr.Item("CommandArgument") = cmdArg dr.Item("CommandName") = "" dr.Item("LinkType") = "DIR" dt.Rows.Add(dr) 'Dim d1 As New LinkButton 'd1.Text = " .." 'd1.CommandArgument = location 'AddHandler d1.Click, AddressOf Me.SelectDirectory_Click 'Me.FilesListField.Controls.Add(d1) 'Me.FilesListField.Controls.Add(New LiteralControl("
")) End If For i As Integer = 0 To childDirs.Length - 1 Dim linkText As String = " " & childDirs(i).Name Dim cmdArg As String = Path.Combine(startDir, childDirs(i).Name) Dim dr As DataRow = dt.NewRow() dr.Item("Link") = linkText dr.Item("CommandArgument") = cmdArg dr.Item("CommandName") = "" dr.Item("LinkType") = "DIR" dt.Rows.Add(dr) 'Dim d2 As New LinkButton 'd2.Text = " " & childDirs(i).Name 'd2.CommandArgument = Path.Combine(startDir, childDirs(i).Name) 'AddHandler d2.Click, AddressOf SelectDirectory_Click 'Me.FilesListField.Controls.Add(d2) 'Me.FilesListField.Controls.Add(New LiteralControl("
")) Next For i As Integer = 0 To childFiles.Length - 1 If Me.CheckExtension(childFiles(i).Extension) = True Then Dim linkText As String = AddIconToFileName(childFiles(i).Name) Dim cmdArg As String = childFiles(i).Name Dim cmdName As String = childFiles(i).FullName Dim dr As DataRow = dt.NewRow() dr.Item("Link") = linkText dr.Item("CommandArgument") = cmdArg dr.Item("CommandName") = cmdName dr.Item("LinkType") = "FILE" dt.Rows.Add(dr) 'Dim l As New LinkButton 'l.Text = AddIconToFileName(childFiles(i).Name) 'l.CommandArgument = childFiles(i).Name 'l.CommandName = childFiles(i).FullName 'AddHandler l.Click, AddressOf SelectImage_Click 'Me.FilesListField.Controls.Add(l) 'Me.FilesListField.Controls.Add(New LiteralControl("
")) End If Next Me.FilesListRepeater.DataSource = dt Me.FilesListRepeater.DataBind() Else msg.ShowWarning("Folder Not Found") End If End Sub Private Function AddIconToFileName(ByVal fileName As String) As String Dim result As String = fileName Dim extension As String = Path.GetExtension(fileName) extension = extension.Trim(".") If File.Exists(Path.Combine(Request.PhysicalApplicationPath, "images\Resources\FileIcons\" & extension & ".gif")) = True Then Dim sb As New StringBuilder sb.Append(" ") sb.Append(fileName) result = sb.ToString End If Return result End Function Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCancel.Click msg.Clear() If Not ViewState("ReturnURL") Is Nothing Then Response.Redirect(ViewState("ReturnURL")) Else Response.Redirect("admindefault.aspx") End If End Sub Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSelect.Click msg.Clear() Dim go As String = ViewState("ReturnURL") If go Is Nothing Then Response.Redirect("admindefault.aspx") Else If go.IndexOfAny("?") > 0 Then go += "&" Else go += "?" End If go += ViewState("ReturnName") go += "=" go += Server.UrlEncode(Path.Combine(ViewState("StartDir"), ViewState("SelectedFile"))) Response.Redirect(go) End If End Sub Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnUpload.Click msg.Clear() Try ' Check File Extension Dim sFile As String sFile = Me.UploadFileField.PostedFile.FileName If sFile.Length < 1 Then msg.ShowWarning("Please enter a file name first to upload a new image.") Else Dim sExtension As String = Path.GetExtension(sFile) Dim startDir As String = ViewState("StartDir") startDir.TrimStart("/") startDir.TrimStart("\") Dim SavePath As String = Path.Combine(Request.PhysicalApplicationPath, startDir) Dim SaveName As String = Path.Combine(SavePath, Path.GetFileName(sFile)) If Me.chkRename.Checked = True Then SaveName = Path.Combine(SavePath, RenameField.Text.Trim) End If If CheckExtension(sExtension) = True Then If File.Exists(SaveName) = True Then msg.ShowWarning("" & Path.GetFileName(sFile) & " already exists. Please delete it first.") Else UploadFileField.PostedFile.SaveAs(SaveName) If Me.rbResizeNone.Checked = False Then Dim rwidth As Integer = 0 Dim rheight As Integer = 0 If rbResizeSmall.Checked = True Then rwidth = WebAppSettings.ImagesSmallWidth rheight = WebAppSettings.ImagesSmallHeight ElseIf rbResizeMedium.Checked = True Then rwidth = WebAppSettings.ImagesMediumWidth rheight = WebAppSettings.ImagesMediumHeight Else rwidth = Me.ResizeWidthField.Text rheight = Me.ResizeHeightField.Text End If ImageHelper.ResizeImage(SaveName, SaveName, rheight, rwidth) End If If (Me.chkOptimize.Checked = True) And ((Path.GetExtension(SaveName).ToLower = ".jpg") Or (Path.GetExtension(SaveName).ToLower = ".jpeg")) Then Dim quality As Integer = 100 While (ImageHelper.GetImageInformation(SaveName).SizeInBytes > (lstOptimize.SelectedValue * 1024)) And quality > 0 ImageHelper.CompressJPEG(SaveName, quality) quality -= 10 End While End If 'PopulateFileList(ViewState("StartDir")) End If Else msg.ShowWarning(BVSoftware.BVC.Core.Content.SiteTerms.GetTerm("ImageExtensionError")) End If End If Catch Ex As Exception msg.ShowException(Ex) End Try PopulateFileList(ViewState("StartDir")) End Sub Public Function CheckExtension(ByVal sExt As String) As Boolean Dim result As Boolean = False If sExt.ToLower = ".jpg" Then result = True End If If sExt.ToLower = ".gif" Then result = True End If If sExt.ToLower = ".png" Then result = True End If Return result End Function Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnDelete.Click msg.Clear() DeleteCurrentFile() Me.PopulateFileList(ViewState("StartDir")) End Sub Private Sub DeleteCurrentFile() msg.Clear() Dim fileToDelete As String = Path.Combine(Request.PhysicalApplicationPath, ViewState("StartDir")) fileToDelete = Path.Combine(fileToDelete, ViewState("SelectedFile")) If File.Exists(fileToDelete) = True Then File.Delete(fileToDelete) End If ClearCurrentImage() End Sub Private Sub SelectImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) SetCurrentImage(CType(sender, LinkButton).CommandArgument, CType(sender, LinkButton).CommandName) PopulateFileList(ViewState("StartDir")) End Sub Private Sub SelectDirectory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Trace.Write("Selecting Directory") Trace.Write("View State Dir Before = " & ViewState("StartDir")) SetStartDir(CType(sender, LinkButton).CommandArgument) Trace.Write("View State Dir After = " & ViewState("StartDir")) ClearCurrentImage() PopulateFileList(CType(sender, LinkButton).CommandArgument) End Sub Private Sub SetCurrentImage(ByVal fileName As String, ByVal longName As String) ViewState("SelectedFile") = fileName Try Dim shortName As String = Path.Combine(ViewState("StartDir"), fileName) Me.imgPreview.ImageUrl = Path.Combine("../", shortName) Dim info As ImageHelper.ImageInfo = ImageHelper.GetImageInformation(longName) If info.Width > 300 Then Me.imgPreview.Width = Unit.Pixel(300) Else Me.imgPreview.Width = Unit.Pixel(info.Width) End If Me.lblImageInfo.Text = fileName Me.lblImageInfo.Text += "
" & info.FormattedDimensions Me.lblImageInfo.Text += "
" & info.FormattedSize Catch ex As Exception End Try End Sub Private Sub ClearCurrentImage() ViewState("SelectedFile") = "" Me.imgPreview.ImageUrl = "../images/NoImageAvailable.gif" Me.lblImageInfo.Text = "No File Selected" End Sub Private Sub SetStartDir(ByVal startDir As String) ViewState("StartDir") = startDir If ViewState("StartDir") Is Nothing Then ViewState("StartDir") = "" End If End Sub Private Sub FilesListRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles FilesListRepeater.ItemDataBound If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then Dim btnFile As LinkButton btnFile = e.Item.FindControl("btnFile") If Not btnFile Is Nothing Then btnFile.Text = DataBinder.Eval(e.Item.DataItem, "Link") btnFile.CommandArgument = DataBinder.Eval(e.Item.DataItem, "CommandArgument") btnFile.CommandName = DataBinder.Eval(e.Item.DataItem, "CommandName") Dim fileType As String = DataBinder.Eval(e.Item.DataItem, "LinkType") If fileType = "DIR" Then AddHandler btnFile.Click, AddressOf SelectDirectory_Click Else AddHandler btnFile.Click, AddressOf SelectImage_Click End If End If End If End Sub Private Sub btnNewFolder_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnNewFolder.Click Me.msg.Clear() Try Dim startDir As String = ViewState("StartDir") startDir.TrimStart("/") startDir.TrimStart("\") Dim SavePath As String = Path.Combine(Request.PhysicalApplicationPath, startDir) SavePath = Path.Combine(SavePath, Me.NewFolderField.Text.Trim) Directory.CreateDirectory(SavePath) PopulateFileList(ViewState("StartDir")) Catch ioex As IOException Me.msg.ShowError("File IO exception: " & ioex.Message & " - Check ASPNET user account permissions.") Catch ex As Exception Me.msg.ShowError(ex.Message) End Try End Sub Private Sub btnFileCopy_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnFileCopy.Click Me.msg.Clear() If CopyFileTo(Me.FileCopyField.Text.Trim) = True Then Me.msg.ShowOK("File Copied") End If PopulateFileList(ViewState("StartDir")) End Sub Private Function CopyFileTo(ByVal newFileName As String) As Boolean Dim result As Boolean = False Try Dim oldPath As String = Path.Combine(Request.PhysicalApplicationPath, ViewState("StartDir")) Dim oldFile As String = Path.Combine(oldPath, ViewState("SelectedFile")) Dim newFile As String = Path.Combine(oldPath, newFileName) File.Copy(oldFile, newFile) If File.Exists(newFile) = True Then result = True End If Catch ioex As IOException Me.msg.ShowError("File IO exception: " & ioex.Message & " - Check ASPNET user account permissions.") Catch ex As Exception msg.ShowError(ex.Message) End Try Return result End Function Private Sub btnFileRename_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnFileRename.Click If CopyFileTo(Me.FileRenameField.Text.Trim) = True Then DeleteCurrentFile() Dim longName As String = Path.Combine(Request.PhysicalApplicationPath, ViewState("StartDir")) longName = Path.Combine(longName, Me.FileRenameField.Text.Trim) SetCurrentImage(Me.FileRenameField.Text.Trim, longName) End If Me.PopulateFileList(ViewState("StartDir")) End Sub End Class