Ensure correct extension when downloading documents

This commit is contained in:
Patrick Gansterer 2017-10-11 01:30:29 +02:00
parent 7d112516a1
commit 957b347b04
2 changed files with 16 additions and 5 deletions

View File

@ -54,10 +54,6 @@ class DocumentsController < ApplicationController
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

View File

@ -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