Skip to content

Commit

Permalink
Viewable PDFs in browser
Browse files Browse the repository at this point in the history
If a user tries to view a PDF, the file will be sent to the browser, and
the browser should display it (tested in FF and Chrome).
  • Loading branch information
dlbucci committed Mar 8, 2015
1 parent 43de694 commit f0823d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'archive.rb'
require 'pdf.rb'

class SubmissionsController < ApplicationController

Expand Down Expand Up @@ -240,6 +241,11 @@ def view
end
return unless file

filename = @submission.handin_file_path
if PDF.is_pdf?(file) then
send_data(file, type: "application/pdf", disposition: "inline", filename: File.basename(filename)) and return
end

begin
@data = @submission.annotated_file(file, @filename, params[:header_position])
rescue
Expand Down
10 changes: 10 additions & 0 deletions lib/pdf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module PDF
def self.is_pdf?(file)
return false unless file
# all PDFs have '%PDF-<major-version>.<minor-version>' in them somewhere.
# this is used instead of MIME type detection because it should work
# for PDFs inside of Archives as well.
# the scrub call prevents invalid byte sequence errors.
return (file.scrub("*") =~ /\%PDF-\d+\.?\d+/)
end
end

0 comments on commit f0823d3

Please sign in to comment.