Revision 642
Added by Jean-Philippe Lang over 18 years ago
| trunk/app/controllers/attachments_controller.rb | ||
|---|---|---|
| 19 | 19 |
layout 'base' |
| 20 | 20 |
before_filter :find_project, :check_project_privacy |
| 21 | 21 |
|
| 22 |
# sends an attachment |
|
| 23 | 22 |
def download |
| 24 |
send_file @attachment.diskfile, :filename => @attachment.filename |
|
| 23 |
# images are sent inline |
|
| 24 |
send_file @attachment.diskfile, :filename => @attachment.filename, |
|
| 25 |
:type => @attachment.content_type, |
|
| 26 |
:disposition => (@attachment.image? ? 'inline' : 'attachment') |
|
| 25 | 27 |
rescue |
| 28 |
# in case the disk file was deleted |
|
| 26 | 29 |
render_404 |
| 27 | 30 |
end |
| 28 |
|
|
| 29 |
# sends an image to be displayed inline |
|
| 30 |
def show |
|
| 31 |
render(:nothing => true, :status => 404) and return unless @attachment.diskfile =~ /\.(jpeg|jpg|gif|png)$/i |
|
| 32 |
send_file @attachment.diskfile, :filename => @attachment.filename, :type => "image/#{$1}", :disposition => 'inline'
|
|
| 33 |
rescue |
|
| 34 |
render_404 |
|
| 35 |
end |
|
| 36 | 31 |
|
| 37 | 32 |
private |
| 38 | 33 |
def find_project |
| trunk/app/helpers/application_helper.rb | ||
|---|---|---|
| 163 | 163 |
rf = Regexp.new(filename, Regexp::IGNORECASE) |
| 164 | 164 |
# search for the picture in attachments |
| 165 | 165 |
if found = attachments.detect { |att| att.filename =~ rf }
|
| 166 |
image_url = url_for :controller => 'attachments', :action => 'show', :id => found.id
|
|
| 166 |
image_url = url_for :controller => 'attachments', :action => 'download', :id => found.id
|
|
| 167 | 167 |
"!#{align}#{image_url}!"
|
| 168 | 168 |
else |
| 169 | 169 |
"!#{align}#{filename}!"
|
| trunk/app/models/attachment.rb | ||
|---|---|---|
| 84 | 84 |
container.is_a?(Project) ? container : container.project |
| 85 | 85 |
end |
| 86 | 86 |
|
| 87 |
def image? |
|
| 88 |
self.filename =~ /\.(jpeg|jpg|gif|png)$/i |
|
| 89 |
end |
|
| 90 |
|
|
| 87 | 91 |
private |
| 88 | 92 |
def sanitize_filename(value) |
| 89 | 93 |
# get only the filename, not the whole path |
Also available in: Unified diff
Image attachments are now sent inline to be viewed directly in the browser.