Moving my image gallery from one domain to another posed a few problems. How would I avoid leaving a trail of red X's behind? What would happen when people follow links to images? Well, I came up with a solution that might be useful to some of you guys...
Code:
<%@ Page Language="VB" %>
<%@Import Namespace="System.Net" %>
<%@Import Namespace="System.Drawing" %>
<script language="vb" runat="server">
Function Debug(byval thestring as string)
Response.Write("<!-- " & thestring & " -->" & vbcrlf)
End function
Function GetFileContents(FileName As String)
Dim objFile As System.IO.File
Dim objStreamReader As System.IO.StreamReader
objStreamReader = objFile.OpenText(Server.MapPath(FileName))
GetFileContents = objStreamReader.ReadToEnd()
objStreamReader.Close()
End Function
</script>
<%
Dim qs, extns, olddomain, newdomain, galleryroot as string
qs = Request.ServerVariables("QUERY_STRING")
extns = "jpg jpeg gif png bmp tiff tga"
olddomain = "http://images.shad.tv"
newdomain = "http://alfa147.net"
galleryroot = "/g/"
If instr(lcase(qs),"/stvg/?p=")>0 then
'this is an stvg 404, redirect to new path
Response.Redirect(newdomain & galleryroot & "?p=" & right(qs,len(qs)-instrrev(qs,"?p=")-2))
ElseIf right(qs,6) = "/stvg/" or right(qs,5) = "/stvg" then
Response.Redirect(newdomain & galleryroot)
Else
dim extn as string = right(qs,len(qs)-instrrev(qs,"."))
If instr(extns,extn)>0 then
'try to load image from new server, if not found display 404 image
Dim webC As New WebClient()
Dim strFilename as string
strFilename = newdomain & Replace(right(qs,len(qs)-instrrev(qs,olddomain & "/")-(len(olddomain)-1)),"/stvg/",galleryroot)
Try
' create a bitmap based on the image from the URL
Dim g = New Bitmap(webC.OpenRead(strFilename))
If g.RawFormat.Equals(Imaging.Imageformat.Gif) then
Response.ContentType = "image/gif"
Else
Response.ContentType = "image/jpeg"
End if
' send the image to the viewer
g.Save(Response.OutputStream, g.RawFormat)
g.Dispose()
g = Nothing
Catch x as Exception
Dim image404 as System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(".") & "\image404.gif")
Dim imgFormat = image404.RawFormat
Response.ContentType = "image/gif"
image404.Save(Response.OutputStream,imgFormat)
image404.Dispose()
image404 = Nothing
imgFormat = Nothing
End try
webC = Nothing
Else
'display 404 page
Response.Write(GetFileContents("404b.htm"))
End if
End if
%>
Basically it will attempt to forward any gallery page requests to the new server. That was a simple thing to do. However if the request is directly for an image file, then it trys to load the image file over the web from the new server and spit it back to the browser. For example, my sig image no longer exists on the domain images.shad.tv, but the script is grabbing it from the new server. If it can't find the image on the new server then it just displays a '404 image' instead. Lastly it none of the above works, it just displays a stock IIS6 404 page.
Anyway, quite a handy thing for me, hopefully some of it will be of use to you guys