From 08984d0bb6b51f7b32792a8ab1f26b313a3a9f3d Mon Sep 17 00:00:00 2001 From: Cristina Date: Sun, 22 Apr 2012 21:42:19 +0100 Subject: [PATCH] Attempt at squashing the error 500 when the PDF download fails (Not a fix for *why* it doesn't get created, that one is way above my head) This is just http://code.google.com/p/otwarchive/issues/detail?id=3024 --- app/controllers/downloads_controller.rb | 29 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/app/controllers/downloads_controller.rb b/app/controllers/downloads_controller.rb index df303ae09ec..00fedb730e7 100644 --- a/app/controllers/downloads_controller.rb +++ b/app/controllers/downloads_controller.rb @@ -22,7 +22,7 @@ def show if @work.unrevealed? flash[:error] = ts("Sorry, you can't download an unrevealed work") - redirect_back_or_default works_path + redirect_back_or_default works_path and return end Rails.logger.debug "Work basename: #{@work.download_basename}" @@ -58,8 +58,11 @@ def download_pdf Rails.logger.debug cmd `#{cmd} 2> /dev/null` - # send as PDF - check_for_file("pdf") + # send as PDF, if file exists, or flash error and redirect + unless check_for_file("pdf") + flash[:error] = ts('We were not able to render this work. Please try another format') + redirect_back_or_default work_path(@work) and return + end send_file("#{@work.download_basename}.pdf", :type => "application/pdf") end @@ -90,7 +93,12 @@ def download_mobi end Rails.logger.debug cmd `#{cmd} 2> /dev/null` - check_for_file("mobi") + + # send as mobi, if file exists, or flash error and redirect + unless check_for_file("mobi") + flash[:error] = ts('We were not able to render this work. Please try another format') + redirect_back_or_default work_path(@work) and return + end send_file("#{@work.download_basename}.mobi", :type => "application/mobi") end @@ -107,16 +115,17 @@ def download_epub Rails.logger.debug cmd `#{cmd} 2> /dev/null` - # send the file - check_for_file("epub") + # send as epub, if file exists, or flash error and redirect + unless check_for_file("epub") + flash[:error] = ts('We were not able to render this work. Please try another format') + redirect_back_or_default work_path(@work) and return + end send_file("#{@work.download_basename}.epub", :type => "application/epub") end + # redirect and return inside this method would only exit *this* method, not the controller action it was called from def check_for_file(format) - unless File.exists?("#{@work.download_basename}.#{format}") - flash[:error] = ts('We were not able to render this work. Please try another format') - redirect_back_or_default work_path(@work) and return - end + File.exists?("#{@work.download_basename}.#{format}") end def create_work_html