Streaming Files to the Browser with Ruby on rails

Posted by Floyd Price Tue, 05 Feb 2008 13:01:00 GMT

Quite often when building a web application you need to store and send Documents, with ruby on rails, streaming files from your application to the browser is simple.


def download_file
    @document = Document.find(params[:id])
    # this is where the magic happens...
    send_file @document.file_location
end

This single line of code will stream a file from your servers filesystem directly to your browser.

The simple use described above is often enough but you have even further control over the file with the following options:

Options:

  • :filename – suggests a filename for the browser to use. Defaults to File.basename(path).
  • :type – specifies an HTTP content type. Defaults to ‘application/octet-stream’.
  • :disposition – specifies whether the file will be shown inline or downloaded. Valid values are ‘inline’ and ‘attachment’ (default).
  • :stream – whether to send the file to the user agent as it is read (true) or to read the entire file before sending (false). Defaults to true.
  • :buffer_size – specifies size (in bytes) of the buffer used to stream the file. Defaults to 4096.
  • :status – specifies the status code to send with the response. Defaults to ‘200 OK’.
  • :url_based_filename – set to true if you want the browser guess the filename from the URL, which is necessary for i18n filenames on certain browsers (setting :filename overrides this option).

Pay particular notice to the :disposition option, changing this from its default to ‘inline’ will enable you to display the contents of the file in the browser window rather than downloading it as an attachment.

def download_file
    @document = Document.find(params[:id])
    # this is where the magic happens (inline)...
    send_file @document.file_location, :disposition => "inline"
end

This is especially useful for things like word or excel documents when you know you target audience can read these in the browser.

EDGE RAILS

If you are brave enough to live on the Edge with Edge Rails you will get an extra treat for free, on Edge Rails the send_file method passes the correct information to your balancing server to allow it to stream the file rather than blocking your mongrel server.

Subversion Clients 2

Posted by Floyd Price Wed, 21 Nov 2007 09:47:00 GMT

Many Code Spaces users have asked my which Subversion Clinets are the best for a given platform so I a have compiled this list of Subversion Clients and grouped it by Operating System, I know this list is not complete so let me know if i have missed any really good ones ;-)

For Windows
  • TortioseSVN is the de-facto standard when it comes to Subversion clients, on windows that is, TortoiseSVN is OPEN SOURCE, and FREE and is a complete implementation, featuring visual Diff and Merge and really good integration with the windows shell.
  • Rapid SVN is a Cross Platform stand alone client. It sports a Simple interface that is familiar to most source control clients, and is OPEN SOURCE and FREE.
  • SmartSVN is a commercial Subversion Client, which includes all the standard SVN features, with some nice additions like a fancy Revision Graph. SmartSVN costs $79.99 per user but for those of you who don’t do Open Source, it’s a good alternative to TortioseSVN and RadipSVN.
  • SVN.exe is the command line Subversion client that comes with the standard Subversion install and for 90% of your subversion work is more than good enough to accomplish what you need, It isn’t a GUI and you have to learn all the commands, but once you have you will also have a much better understanding of the Subversion internals. SVN.exe is OPEN SOURCE and FREE.

Mac OS X

  • SCPlugin is an open source project that is building a Mac version of TortoiseSVN (see above), and although its far from being complete it does give you the shell integration and the basic SVN commands. it is OPEN SOURCE and FREE.
  • RapidSVN (See above) being cross platform is also a good client for Mac OS X, however the look and feel is not really what most Mac users expect. (Java tut).
  • SmartSVM (See above) being cross platform is also a good commercial Subversion client on Mac OS X.
  • The SVN command line client again is a great utility on the Mac.

Linux

  • RapidSVN and SmartSVN also run on Linux systems (java required) and are both great on this platform.
  • Svn command line is probably the tool of choice for most Linux developers. Linux users are generally more comfortable in the command line.

In my next post i will highlight some of the IDE’s and IDE Extensions that enable Subversion Integration.

Our Logo From Paper to Pixel in a few easy steps.

Posted by Floyd Price Tue, 20 Nov 2007 23:54:00 GMT

We recently redesigned the Code Spaces website, and a key part of this was the new Logo:

Any of you that know me will be both shocked and amazed to know that I did the Logo myself, but this is not me being cheap and trying to save a few hundred dollars, I actually couldn’t find somebody who:

a. Was able to run with my vague and often contradictory requirements 
b. Was able to give me a quote for less that $1000 dollars (I'm not cheep but come on its only a logo).
c. Was even remotely interested in giving me the time to explain my requirements.

Now I spent a good two weeks discussing and and giving feedback to a range of designers, some looking to charge $50 per logo to some looking to charge $1000.

I didn’t get any what i would call good icons during this stage but i found one interesting thing and that is, out of all the companies trying to charge more than $750 not one of them actually listened to my feedback. In fact the only good responses I got from my feedback was one of the guys looking to charge < $100 although ultimately it wasn’t to be.

As my dear old Granddad used to say “if you want a job doing properly, DO IT YOURSELF!”

Designing your own Icon or any other graphics is a daunting task for a software developer, things like creativity don’t come easy to us, but hey I’m sat in front of an iMAC “the graphics designers tool of choice” so I must be able to come up with something, shouldn’t I?

Well I decided that the best option would be to draw with a pencil on paper some prototype designs. I expected to draw hundreds, and that the creative juices from deep within my soul would rise to the surface to produce an icon of beauty that the whole world would marvel at however, I drew 3, yes three.

All three were basically a blob and the word Code Spaces, which is in fact the correct anatomy for a logo but at this point not too impressive.

Plan b, was to find an image to replace the blob and then draw some sketches based on that, so I browsed along to iStockPhoto and searched for vector illustrations with the key word cogs and identified 3 images that i could use to replace my “blobs” and redrew the designs.

This would be my short list of designs, and here it is for your viewing pleasure, (be gentle and click to enlarge)

You may have already noticed that I finally picked the middle logo to actually create.

I was able to purchase the image that inspired my design from iStockPhoto and using Illustrator was able to stick the work Code Spaces next to it, with a gradient and a drop shadow for good measure and what would your know? I have a logo. Not that great, but not to bad.

Best of all I saved hundreds of dollars and it only cost me 15 hours of my time, no only kidding the icon took only 30 mins after conception and the best thing really is that every time I visit the website I see it looking right back at me, and it gives me nice warm “I Did That” feeling.

So if your in this position, Do It Yourself!

Quick Access to your Code Spaces Subversion Repositories

Posted by Floyd Price Sat, 03 Nov 2007 22:15:00 GMT

If like us you have dozens of repositories in your Code Spaces account you can use this simple tip to get quick access to all your repositories.

Normally you would use a URL similar to this to access a specific repository:

http(s)://svn.codespaces.com/[my_account]/[my_repository]

However, if you simply go up one level to:

http(s)://svn.codespaces.com/[my_account]/

Paste this into your browsers address bar, you will see a list of all your repositories and even better, they are browsable (provided you have permission to them).