0

I'm using a custom written auto uploader to import images from users to Amazon S3. I'm building up a parallel image library in my database, so I know what images I can access on S3 to not waste any http-requests.

However, my uploader sometimes throws errors (e.g. source image missing) and although I'm validating, I'm sometimes ending up with entries in my media table and no matching image on S3.

To correct these, I'm thinking of creating a cfthread/cfschedule which clears my image database from faulty entries. What I'm not sure is how to capture 404 responses. Right now I'm having this on a page:

<img src="#variables.imageSrc#" alt="#some alt#" title="#some title#" class="ui-li-thumb ui-corner-all" />

which tries to load the image and returns a 404 if not successful.

Question
How would I capture this 404? I don't want to put anything in the markup, so I assume this should go to onrequestend or another Coldfusion event being monitored in my application.cfc. If so, is there an easy way to identify image request, because I would not want to run a big routine on every applicationr request.

Thanks for insights!

EDIT:
I don't think running isDefined on every image before displaying it is feasable, because it will be a double request to S3 and there is a lot of images. I want to take the 404 and then clean up my database, so next time the image will not be accessed anymore.

frequent
  • 27,643
  • 59
  • 181
  • 333

2 Answers2

1

If you don't want to use cfhttp and test each image like Matt suggested, why not trigger an ajax call from the browser using the onError handler of the img tag. Something like this but instead of showing a custom graphic, trigger your ajax to set your flag, or maybe even delete the image since it's happening invisible to the user. jQuery/JavaScript to replace broken images

More info on Image onerror Event

I'm curious to see how you would use isdefined on an image. From your code posted isdefined("variables.imageSrc") wouldn't help you much.

Community
  • 1
  • 1
genericHCU
  • 4,394
  • 2
  • 22
  • 34
  • hm. But is there no way to capture an http-request response (and other parameters) on `onRequestEnd`? – frequent Nov 14 '12 at 12:23
  • Not that I know of. CF just generates text to send to the browser. It's up to the browser to handle any missing assets. – genericHCU Nov 14 '12 at 12:24
  • 2
    In other words, `` doesn't cause CF to make a request to that resource, it just passes it on to the browser so CF has no way of knowing if it is valid or not. To make CF check the status code of a request the browser would make, use cfhttp. If you're worried about bandwidth use, you could try using `method="head"` that *should* prevent amazon from sending the whole image and just give you header information. – genericHCU Nov 14 '12 at 12:31
0

If you're running a scheduled task to do this, why not use cfhttp to perform a GET request on the image asset? You can then check the status code in the response to validate the file existence on the server, and then update the database accordingly.

Matt Gifford
  • 1,268
  • 9
  • 13
  • Not quite. I'm getting occasional errors (missing images) on my client page. My idea was to set a flag whenever an image is missing an then run a scheduled task to remove these faulty media lib entries from my database, so I'm not bothering the user having to wait for cleanups. My problem is, a) `where` to capture the http-return (`onRequestEnd`?) and `how to capture it` (something like `GetPageContext().GetRequest()`?). – frequent Nov 14 '12 at 10:51