<%@ WebHandler Language="VB" Class="Handler" %>

Imports System.IO
Imports AppCommon

Public Class Handler
    Implements IHttpHandler
 
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim strCommonDirectory As String = Com_Get_変数("PT_COMMON_DIRECTORY")
        Dim strSubDirectory As String = Com_Get_変数("PT_PICTURE_DIRECTORY")
        Dim strKey As String = CommonEncryption.DecryptString(context.Request.QueryString("KEY"))
        Dim strExtension As String = Com_Get_変数("PT_PICTURE_EXTENSION")
        Dim strPath As String = strCommonDirectory + strSubDirectory + strKey + strExtension
        Dim strPathDefault As String = strCommonDirectory + strSubDirectory + "default" + strExtension
        Dim strContextType As String = Com_Get_変数("PT_PICTURE_CONTENTTYPE")

        context.Response.Clear()
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
        context.Response.ContentType = strContextType
        If System.IO.File.Exists(strPath) Then
            Dim fs As FileStream
            fs = New FileStream(strPath, FileMode.Open, FileAccess.Read)
            
            Dim bs(fs.Length - 1) As Byte
            fs.Read(bs, 0, bs.Length)
            
            context.Response.OutputStream.Write(bs, 0, bs.Length)
        Else
            If System.IO.File.Exists(strPathDefault) Then
                Dim fs As FileStream
                fs = New FileStream(strPathDefault, FileMode.Open, FileAccess.Read)
            
                Dim bs(fs.Length - 1) As Byte
                fs.Read(bs, 0, bs.Length)
            
                context.Response.OutputStream.Write(bs, 0, bs.Length)
            End If
        End If
    End Sub

    Public ReadOnly Property IsReusable() As Boolean _
        Implements IHttpHandler.IsReusable

        Get
            Return True
        End Get
    End Property

End Class
