Imports System
Imports System.IO
Imports System.Web
Public Class FileStreamer
Implements System.Web.Routing.IRouteHandler
Function GetHttpHandler(requestContext As Routing.RequestContext) As IHttpHandler Implements System.Web.Routing.IRouteHandler.GetHttpHandler
Dim MyhttpHandler As HttpHandler = New HttpHandler()
Return MyhttpHandler
End Function
Public Class HttpHandler
Implements IHttpHandler
Public ReadOnly Property IsReusable As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return True
End Get
End Property
Public Sub ProcessRequest(context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
System.Net.ServicePointManager.DefaultConnectionLimit = 65535
Dim ImageFileRef As String = ""
Dim Size As Integer = 500
Dim Transparency As Integer = 0
If Not IsNothing(context.Request.QueryString("img")) Then
ImageFileRef = context.Request.QueryString("img")
End If
If Not IsNothing(context.Request.QueryString("s")) Then
If IsNumeric(context.Request.QueryString("s")) Then
If context.Request.QueryString("s") < 2001 And context.Request.QueryString("s") > 10 Then
Size = context.Request.QueryString("s")
End If
End If
End If
If Not IsNothing(context.Request.QueryString("t")) Then
If IsNumeric(context.Request.QueryString("t")) Then
If context.Request.QueryString("t") = 1 Then
Transparency = 1
End If
End If
End If
'// Image Locator and streamer
Try
If ImageFileRef.Length > 0 Then
ImageFileRef = ImageFileRef.Replace(".pdf", ".png")
If File.Exists(HttpContext.Current.Server.MapPath("\Uploads\" & ImageFileRef)) Then
Dim Myinput As System.IO.FileStream = New System.IO.FileStream(HttpContext.Current.Server.MapPath("\Uploads\" & ImageFileRef), FileMode.Open, FileAccess.Read)
Dim Buffer() As Byte
ReDim Buffer(Myinput.Length) ' - 1)
Myinput.Read(Buffer, 0, Buffer.Length)
Myinput.Close()
Myinput.Dispose()
context.Response.Clear()
context.Response.ContentType = "image/png"
context.Response.AddHeader("Content-Type", "image/png")
context.Response.AddHeader("Content-Length", Buffer.Length.ToString)
context.Response.AddHeader("Content-Transfer-Encoding", "binary")
context.Response.BinaryWrite(Buffer)
context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
context.Response.Cache.SetAllowResponseInBrowserHistory(False)
context.Response.Flush()
context.Response.Close()
GC.Collect()
Else
'// See if we can generate the preview
Dim Sourcefile As String = ImageFileRef
'// transform to PDF & page
Dim NewSource As String = Sourcefile.Replace(".png", ".pdf")
Dim Path As String = HttpContext.Current.Server.MapPath("\PDFUploads")
If ImageCoder.GeneratePDFThumbnail(Path, NewSource, Size, Transparency, 0) Then
Dim Checkfile As String = ImageFileRef
Checkfile = Checkfile.Replace(".pdf", ".png")
If File.Exists(HttpContext.Current.Server.MapPath("\PDFUploads\" & ImageFileRef)) Then
Dim Myinput As System.IO.FileStream = New System.IO.FileStream(HttpContext.Current.Server.MapPath("\PDFUploads\" & ImageFileRef), FileMode.Open, FileAccess.Read)
Dim Buffer() As Byte
ReDim Buffer(Myinput.Length) ' - 1)
Myinput.Read(Buffer, 0, Buffer.Length)
Myinput.Close()
Myinput.Dispose()
context.Response.Clear()
context.Response.ContentType = "image/png"
context.Response.AddHeader("Content-Type", "image/png")
context.Response.AddHeader("Content-Length", Buffer.Length.ToString)
context.Response.AddHeader("Content-Transfer-Encoding", "binary")
context.Response.BinaryWrite(Buffer)
context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
context.Response.Cache.SetAllowResponseInBrowserHistory(False)
context.Response.Flush()
context.Response.Close()
GC.Collect()
Else
NoImage(context)
End If
Else
NoImage(context)
End If
End If
Else
context.Response.Flush()
context.Response.Close()
GC.Collect()
End If
Catch ex As Exception
Errorhandler.ErrorHandler(0, ex.ToString)
context.Response.Flush()
context.Response.Close()
GC.Collect()
End Try
End Sub
Sub NoImage(context As System.Web.HttpContext)
'// Stream no image
Dim Myinput As System.IO.FileStream = New System.IO.FileStream(ConfigurationManager.AppSettings("NoImage"), FileMode.Open, FileAccess.Read)
Dim Buffer() As Byte
ReDim Buffer(Myinput.Length) ' - 1)
Myinput.Read(Buffer, 0, Buffer.Length)
Myinput.Close()
Myinput.Dispose()
context.Response.Clear()
context.Response.ContentType = "image/png"
context.Response.AddHeader("Content-Type", "image/png")
context.Response.AddHeader("Content-Length", Buffer.Length.ToString)
context.Response.AddHeader("Content-Transfer-Encoding", "binary")
context.Response.BinaryWrite(Buffer)
context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
context.Response.Cache.SetAllowResponseInBrowserHistory(False)
context.Response.Flush()
context.Response.Close()
GC.Collect()
End Sub
End Class
End Class