My current projekt needs a feature where my client can upload some
pictures. For simplicity I just want to store them in the DB because
this is easier to maintain (backup, restore etc.) and it’s really only a
few dozens compressed files.
I looked into the Agile Rails Web Dev 2nd Ed book and figured out the
following action which sends the data of the picture (stored in the DB)
to the user as an image:
class PicturesController < ApplicationController
@pic = Picture.find(params[:id])
:filename => @pic.name,
:type => @pic.content_type,
:disposition => “inline”)
This works very well so far. To speed up the page, I want to cache this
action, so I use
in the PicturesController. And now I run in serious problems… Let me
- I open a page which has an HTML image tag to a picture, let’s say it
uses <img href="/public/pictures/receive/55" … />
- This loads the data slowly from the DB as expected and shows it as a
picture in my page.
- I reload the same page (no complete reload of all stuff, only by
placing the cursor in the address bar of Firefox and hitting Enter)
- The image is again very slow - it seems that it is again loaded from
the DB and not from the cache!
- I take a look at the cache: hrm, there definitely is a cached version
of my pic #55, but it’s called 55.png (although it was a JPG file), so
the complete path is /public/pictures/receive/55.png
- No reason the file is loaded from the cache again, because my HTML
img tag expects a file called only 55, not 55.png!
- I open the image directly in the browser by pointing it to
- This displays the image as expected, and again it is very slow
(because it’s loaded from the DB, nobody’s surprised here)
- I reload the image (again by placing the cursor in Firefox’
addressbar and hitting Enter)
- And now that seems very strange - no image is displayed, but a very
long text with very confusing characters and stuff! What happened here??
- I look into the cache - and what do I see? Now there’s another
cached file, named 55.html!
- I go back to the page I opened in the first step, and now the image
is displayed very fastly, so everything seems right from this
perspective: the cached image is loaded from the cache.
- But this all is far away from being bug-free code! The image now is
only shown as an image because (I guess) the browser expects an image
when using an img tag, so it doesn’t care about wrong headers or
something like that, and just interprets the incoming stuff from 55.html
as an image. (And 55.html is only successfully loaded by the img tag
because the webserver points to .html automatically when no extension is
given in this case).
So what’s the problem here? I have adapted the receive() method from the
Agile Rails Web Dev book, so it seems this code is not really functional
for real-world-applications? Anyone can fix it? I just want the action
pictures/receive/55 to cache the image as /public/pictures/55 with no
png or html extension…
Thanks a lot for your help!