Skip to content

Commit

Permalink
handle attahcment with send_file
Browse files Browse the repository at this point in the history
  • Loading branch information
dzaporozhets committed Feb 11, 2013
1 parent ab0cfc0 commit a699ebd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/controllers/files_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class FilesController < ApplicationController
def download
uploader = Note.find(params[:id]).attachment
uploader.retrieve_from_store!(params[:filename])
send_file uploader.file.path, disposition: 'attachment'
end
end

4 changes: 4 additions & 0 deletions app/uploaders/attachment_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ def image?
rescue
false
end

def secure_url
"/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}"
end
end
2 changes: 1 addition & 1 deletion app/views/events/event/_note.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
= markdown truncate(event.target.note, length: 70)
- note = event.target
- if note.attachment.url
= link_to note.attachment.url, target: "_blank", class: 'note-file-attach' do
= link_to note.attachment.secure_url, target: "_blank", class: 'note-file-attach' do
- if note.attachment.image?
= image_tag note.attachment.url, class: 'note-image-attach'
- else
Expand Down
2 changes: 1 addition & 1 deletion app/views/notes/_note.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
- if note.attachment.image?
= image_tag note.attachment.url, class: 'note-image-attach'

This comment has been minimized.

Copy link
@andyjeffries

andyjeffries Jun 18, 2013

Why do you use .url here rather than .secure_url? On my server inline images are broken, but the download links work correctly. I can't see how this should be different - if you use secure_url to download the attachment and the attachment is an image, why not use secure_url as the source?

.attachment.pull-right
= link_to note.attachment.url, target: "_blank" do
= link_to note.attachment.secure_url, target: "_blank" do
%i.icon-paper-clip
= note.attachment_identifier
.clear
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
root to: "projects#index"
end

#
# Attachments serving
#
get 'files/:type/:id/:filename' => 'files#download', constraints: { id: /\d+/, type: /[a-z]+/, filename: /[a-zA-Z.0-9_\-\+]+/ }

#
# Admin Area
#
Expand Down

0 comments on commit a699ebd

Please sign in to comment.