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 def show
@document = Document.find(params[:id]) @document = Document.find(params[:id])
filename = @document.name send_data(@document.data, filename: @document.filename, type: @document.mime)
unless filename.include? '.'
filename += '.' + MIME::Types[@document.mime].first.preferred_extension
end
send_data(@document.data, :filename => filename, :type => @document.mime)
end end
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' belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'
validates_presence_of :data 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 end