Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt at squashing the error 500 when the PDF download fails #590

Merged
merged 1 commit into from May 4, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions app/controllers/downloads_controller.rb
Expand Up @@ -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}"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down