Article 6BK5S CodeSOD: Switching File Types

CodeSOD: Switching File Types

by
Remy Porter
from The Daily WTF on (#6BK5S)

Jeremy found this VB.Net code. It has a simple job: return a file back to the client, but with a twist: they don't know what the correct file extension is. So this was their solution for that problem:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Application("PointsWhere") = "DEV" Then Dim strFile As String strFile = Application("UploadPODs").ToString.Trim & Session("PODClaim") & ".pdf" If System.IO.File.Exists(strFile) Then Response.ContentType = "Application/pdf" Else strFile = Application("UploadPODs").ToString.Trim & Session("PODClaim") & ".jpeg" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/jpeg" Else strFile = Application("UploadPODs").ToString.Trim & Session("PODClaim") & ".jpg" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/jpeg" Else strFile = Application("UploadPODs").ToString.Trim & Session("PODClaim") & ".gif" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/gif" Else strFile = Application("UploadPODs").ToString.Trim & Session("PODClaim") & ".bmp" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/bmp" Else strFile = Application("UploadPODs").ToString.Trim & Session("PODClaim") & ".png" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/png" Else strFile = Application("UploadPODs").ToString.Trim & Session("PODClaim") & ".tiff" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/tiff" Else strFile = Application("UploadPODs").ToString.Trim & Session("PODClaim") & ".tif" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/tiff" End If End If End If End If End If End If End If End If Response.WriteFile(strFile) Response.End() Else 'This is for Production Dim strFile As String strFile = Server.MapPath(Application("UploadPODs")) & Session("PODClaim") & ".pdf" If System.IO.File.Exists(strFile) Then Response.ContentType = "Application/pdf" Else strFile = Server.MapPath(Application("UploadPODs")) & Session("PODClaim") & ".jpeg" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/jpeg" Else strFile = Server.MapPath(Application("UploadPODs")) & Session("PODClaim") & ".jpg" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/jpeg" Else strFile = Server.MapPath(Application("UploadPODs")) & Session("PODClaim") & ".gif" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/gif" Else strFile = Server.MapPath(Application("UploadPODs")) & Session("PODClaim") & ".bmp" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/bmp" Else strFile = Server.MapPath(Application("UploadPODs")) & Session("PODClaim") & ".png" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/png" Else strFile = Server.MapPath(Application("UploadPODs")) & Session("PODClaim") & ".tiff" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/tiff" Else strFile = Server.MapPath(Application("UploadPODs")) & Session("PODClaim") & ".tif" If System.IO.File.Exists(strFile) Then Response.ContentType = "image/tiff" End If End If End If End If End If End If End If End If Response.WriteFile(strFile) Response.End() End If End Sub

I love the condition around whether they're in "DEV" or in "PROD", despite the code doing the exact same thing. The real magic, though, is the nested ifs for something that doesn't need to be nested. I can see the programmer's brain leaking smoke as they try and figure out the right way to check files and write a response, and the idea of iterating across an array of allowed extensions never occurred to them.

What I really wonder about: how many places do they branch on "DEV" and "PROD". It's like the world's worst implementation of feature flags, though marginally better than the solution an old workplace of mine had: store a text file on the server in a known location that contains the word "DEV", "TEST", or "PROD" so your code can check that file every time it needs to know what behavior it should use.

proget-icon.png [Advertisement] ProGet's got you covered with security and access controls on your NuGet feeds. Learn more.
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments