<%@ WebHandler language="VB" class="DnnForge.SimpleGallery.ImageHandler" %>

Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2d
Imports System.Drawing.Imaging
Imports System.Web

Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Portals

Imports Microsoft.VisualBasic

Imports DnnForge.SimpleGallery.Entities

Namespace DnnForge.SimpleGallery

    Public Class ImageHandler : Implements IHttpHandler

#Region " Private Members "

    Private _width As Integer = 100
    Private _height As Integer = 100
    Private _homeDirectory As String = Null.NullString
    Private _fileName As String = Null.NullString
    Private _quality As Boolean = False

#End Region

#Region " Private Methods "

    Private Sub ReadQueryString(context As HttpContext)

        If Not (context.Request("Width") Is Nothing) Then
            If (IsNumeric(context.Request("Width"))) Then
                _width = Convert.ToInt32(context.Request("Width"))
            End If
        End If

        If Not (context.Request("Height") Is Nothing) Then
            If (IsNumeric(context.Request("Height"))) Then
                _height = Convert.ToInt32(context.Request("Height"))
            End If
        End If

        If Not (context.Request("HomeDirectory") Is Nothing) Then
			_homeDirectory = context.Server.UrlDecode(context.Request("HomeDirectory"))
        End If        

		If Not (context.Request("FileName") Is Nothing) Then
			_fileName = context.Server.UrlDecode(context.Request("FileName"))
        End If
        
        If Not (context.Request("Q") Is Nothing) Then
			If( context.Request("Q") = "1" ) Then
				_quality = True
			End If
        End If

    End Sub

#End Region

#Region " Properties "

    Public ReadOnly Property IsReusable As Boolean Implements IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

#End Region

#Region " Event Handlers "

	Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
		
		' Set up the response settings
		context.Response.ContentType = "image/jpeg"
	    
		' Caching 
		context.Response.Cache.SetCacheability(HttpCacheability.Public)
		context.Response.Cache.SetExpires(DateTime.Now.AddDays(1))
		context.Response.Cache.VaryByParams("FileName") = True
		context.Response.Cache.VaryByParams("HomeDirectory") = True
		context.Response.Cache.VaryByParams("Width") = True
		context.Response.Cache.VaryByParams("Height") = True
	    
		context.Items.Add("httpcompress.attemptedinstall", "true")

		ReadQueryString(context)

		If (_fileName <> "") Then
		
			Try
				Dim path As String = ""
				If _fileName = "placeholder-600.jpg" Then
					path = "Images/placeholder-600.jpg"
				Else
					path = _homeDirectory & "/" & _fileName
				End If

				Dim photo As Image = Image.FromFile(context.Server.MapPath(path))

				Dim thumb As Image = photo.GetThumbnailImage(_width, _height, Nothing, New IntPtr)

				If (_quality) Then
					Dim info As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
					Dim params As New EncoderParameters
					params.Param(0) = New EncoderParameter(Encoder.Quality, 100L)
					thumb.Save(context.Response.OutputStream, info(1), params)
				Else
					thumb.Save(context.Response.OutputStream, Imaging.ImageFormat.Jpeg)
				End If

				photo.Dispose()
				thumb.Dispose()
			Catch
			End Try
			
		End If

    End Sub

#End Region

    End Class

End Namespace
