0 Comments By Damon on Jul 14 in Website and Releases.

I've written a fair share of stories myself, most of which include a variety of photos that I wanted to make available to anyone interested. Even with the most recent feature, a custom slideshow, I felt that I had to make the photos accessible in a more convienent way. If you consider for a moment the fact that some stories have over 22 images, you can imagine that it would take quite some time to click through each one in the slideshow and download them individually. Now that's no fun.

Since I'm a GMail user, although not heavily, I did happen to notice that they provide an option to download all attachments as a zip file. What a wonderful idea.

Here's the private method used by the action in my article controller to accomplish this small feat. 

def send_zip_file(files, article_title)
 filename = "#{Digest::SHA1.hexdigest(Time.now.to_s+rand.to_s)}.zip"
 path = RAILS_ROOT + "/public/images/zip_downloads/"
 begin
  `zip -j #{path}#{filename} #{files.join(" ")} 2>/dev/null`
 rescue
  logger.info("WARNING: Zipping of imges files failed.")
 end
 begin
  send_file "#{path}#{filename}", :type => "application/x-zip", :disposition => 'attachment', :filename => "#{filename}.zip", :stream => false
  File.unlink(path + filename)
 rescue
  logger.info("WARNING: Could not delete zip file generated for download_all action.")
 end
end

 
Basically what the above function does is:

  • Generate a random filename and append .zip to it
  • Takes all the articles images and converts them to an argument passed to the zip command line utility
  • Sends the file directly the the browser
  • Deletes the temporary file 

And voila, see that "22 images - Download" link in the top right corner. Now we have all the photos instantly available for download as one file. Rather than make this available to all users, including bots, and casual photo enthusiasts, I decided to make this feature only available to registered users. Therefore, if you are not logged in, this option will not be available. So when you get a chance please feel free to give it a whirl.

Current Rating: 4.0 rating from 1 vote

  • Current rating is 4
  •  
  •  
  •  
  •  
  •  

No Responses to "I'd like my photos to go please"

Comments are Closed

Name: (Required)
Website:
Comment:
Remember my info