Skip to content

Commit

Permalink
Ensure correct extension when downloading documents
Browse files Browse the repository at this point in the history
  • Loading branch information
paroga committed Oct 10, 2017
1 parent 9a3ab14 commit c4582d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 1 addition & 5 deletions plugins/documents/app/controllers/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ def destroy

def show
@document = Document.find(params[:id])
filename = @document.name
unless filename.include? '.'
filename += '.' + MIME::Types[@document.mime].first.preferred_extension
end
send_data(@document.data, :filename => filename, :type => @document.mime)
send_data(@document.data, filename: @document.filename, type: @document.mime)
end
end
15 changes: 15 additions & 0 deletions plugins/documents/app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@ class Document < ActiveRecord::Base
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'

validates_presence_of :data

def filename
types = MIME::Types[mime]

if name.include? '.'
types.each do |type|
type.extensions.each do |extension|
return name if name.end_with? ".#{extension}"
end
end
end

"#{name}.#{types.first.preferred_extension}"
end

end

0 comments on commit c4582d5

Please sign in to comment.