Imports System.Xml
Imports System.Xml.Serialization
Imports CharterDotCom.CommonEntities.Library
Imports System.Configuration
Imports System.Runtime.Remoting

Public Class ContentBinaryFileHandler
    Implements IHttpHandler

    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest

        Dim request As HttpRequest = context.Request
        Dim response As HttpResponse = context.Response

        Dim BinaryFileName As String
        Dim BinaryFileExtension As String
        Dim objContent As ContentSchema.BINARYFILE
        Try
            Dim strTemp As String
            Dim strChar(1) As Char
            Dim strSplit() As String
            strTemp = request.RawUrl
            strChar(0) = CChar("/")
            Select Case True
                Case (InStr(strTemp, ".ccswf") > 0)
                    strSplit = strTemp.Split(strChar, StringSplitOptions.RemoveEmptyEntries)
                    BinaryFileName = Replace(strSplit(UBound(strSplit)), ".ccswf", "")
                    BinaryFileExtension = ".swf"
                Case (InStr(strTemp, ".ccxml") > 0)
                    strSplit = strTemp.Split(strChar, StringSplitOptions.RemoveEmptyEntries)
                    BinaryFileName = Replace(strSplit(UBound(strSplit)), ".ccxml", "")
                    BinaryFileExtension = ".xml"
                Case (InStr(strTemp, ".ccflv") > 0)
                    strSplit = strTemp.Split(strChar, StringSplitOptions.RemoveEmptyEntries)
                    BinaryFileName = Replace(strSplit(UBound(strSplit)), ".ccflv", "")
                    BinaryFileExtension = ".flv"
                Case Else
                    Exit Sub
            End Select

            If context.Cache("*CMSBinaryFiles*" & BinaryFileName & BinaryFileExtension) Is Nothing Then
                objContent = GetBinaryFile(BinaryFileName, BinaryFileExtension)
                Dim expiredate As DateTime
                If Now() > Date.Today.AddHours(3) Then
                    expiredate = Date.Today.AddDays(1).AddHours(3)
                Else
                    expiredate = Date.Today.AddHours(3)
                End If
                context.Cache.Insert("*CMSBinaryFiles*" & BinaryFileName & BinaryFileExtension, objContent.m_BinaryFileData, Nothing, expiredate, Caching.Cache.NoSlidingExpiration, Caching.CacheItemPriority.Default, Nothing)
            Else
                objContent.m_BinaryFileData = CType(context.Cache("*CMSBinaryFiles*" & BinaryFileName & BinaryFileExtension), Byte())
            End If

            If Not objContent.m_BinaryFileData Is Nothing Then
                response.BinaryWrite(objContent.m_BinaryFileData)
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Function GetBinaryFile(ByVal BinaryFileName As String, ByVal BinaryFileExtension As String) As ContentSchema.BINARYFILE
        Try
            Dim cmsBinaryFile As ContentSchema.BINARYFILE = Nothing

            Dim obj As CMS.BINARYFILE = CMSContentManager.Instance.GetBinaryFile(BinaryFileName, BinaryFileExtension)
            If obj IsNot Nothing Then
                cmsBinaryFile.m_BinaryFileData = obj.BINARYFILEDATA
            End If
            Return cmsBinaryFile

        Catch ex As Exception
            Throw ex
        End Try
    End Function

End Class