RSS
 

Posts Tagged ‘image leaching’

Prevent Image “Leaching”

10 Dec

I came across a situation where people where “leaching” images off my family photo site. “File Leaching” means that instead of the person downloading the file, and simply serving it up from their server, they were simply linking to my version of it.

While this may seem trivial, it happened on a site that has 10,000 family pictures, and was causing serious bandwidth issues for me.

So… being the good developer I think I am, I came up with a way to prevent that.

The basics of it are, simply store your files above the websites root directory, and stream them to the user upon request.

I found using ADO.Stream this is possible, and very efficient.

Hope you find it as useful as I do… Enjoy!

Usage:

  1. <%  
  2. ‘*****************************************************************************  
  3. ‘*****************************************************************************  
  4. ‘  
  5. ‘ This code can be used anywhere you like, all I ask is that you keep this  
  6. ‘ notice here, so people know who actually made it! =D  Thanks!  
  7. ‘  
  8. ‘ This code was developed by Kevin Pirnie, c/o o7th Web Design  
  9. ‘ support@07th.com :: http://www.07th.com  
  10. ‘  
  11. ‘*****************************************************************************  
  12. ‘*****************************************************************************
  13. Dim strPath : strPath = "C:\WebStorage\thefile.zip" ‘physical path to the file
  14. Dim strFileName : strFileName = "WhatIWantToCallIt.zip" ‘whatever I feel like naming the file
  15. Call downloadFile(strPath, strFileName)
  16. %>

Code:

  1. <%
  2. ‘Force File Download
  3. Sub DownloadFile(ByVal strFileName, ByVal strFileNamePass, ByVal intFileType)
  4.      Response.Buffer = True
  5.      Response.Clear
  6.      Set objStream = CreateObject("ADODB.Stream")
  7.      objStream.Open
  8.      objStream.Type = 1
  9.      Set objFilesystem = CreateObject("Scripting.FileSystemObject")
  10.                  If Not objFilesystem.FileExists(strFilename) Then
  11.                           Write("<h1>Error</h1>: " & strFilename & " does not exist<p>")
  12.                           Response.End
  13.                  End If
  14.                  Set objFilestream = objFilesystem.GetFile( strFilename )
  15.                          intFilelength = objFilestream.size
  16.                          objStream.LoadFromFile( strFilename )
  17.                          If Err Then
  18.                                   Write("<h1>Error: </h1>" & Err.Description & "<p>")
  19.                                   Response.End
  20.                          End If
  21.                          If Len( Trim(strDownloadFilename) ) > 0 Then
  22.                                   strFileNamePass = Trim( strFileNamePass )
  23.                          Else
  24.                                   strFileNamePass = objFilestream.Name
  25.                          End If
  26.                          Select Case intFileType
  27.                                 Case 1 ‘CSV
  28.                                         Response.ContentType = "text/csv"
  29.                                 Case 2 ‘XML
  30.                                         Response.ContentType = "text/xml"
  31.                          End Select
  32.                          Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileNamePass
  33.                          Response.AddHeader "Content-Length", intFilelength
  34.                          Response.Charset = "UTF-8"
  35.                          For i = 0 To objStream.Size
  36.                                   i = i + 128000
  37.                                   Response.BinaryWrite(objStream.Read(128000))
  38.                                   Response.Flush
  39.                          Next : i = Null
  40.                  Set objFilestream = Nothing
  41.         Set objFilesystem = Nothing
  42. End Sub
  43. %>
 
 
 

Optimized by SEO Ultimate