Commit a699ebdbcc11051b9473a88788cf8efdde659975

Authored by Dmitriy Zaporozhets
1 parent ab0cfc00

handle attahcment with send_file

app/controllers/files_controller.rb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +class FilesController < ApplicationController
  2 + def download
  3 + uploader = Note.find(params[:id]).attachment
  4 + uploader.retrieve_from_store!(params[:filename])
  5 + send_file uploader.file.path, disposition: 'attachment'
  6 + end
  7 +end
  8 +
... ...
app/uploaders/attachment_uploader.rb
... ... @@ -19,4 +19,8 @@ class AttachmentUploader &lt; CarrierWave::Uploader::Base
19 19 rescue
20 20 false
21 21 end
  22 +
  23 + def secure_url
  24 + "/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}"
  25 + end
22 26 end
... ...
app/views/events/event/_note.html.haml
... ... @@ -26,7 +26,7 @@
26 26 = markdown truncate(event.target.note, length: 70)
27 27 - note = event.target
28 28 - if note.attachment.url
29   - = link_to note.attachment.url, target: "_blank", class: 'note-file-attach' do
  29 + = link_to note.attachment.secure_url, target: "_blank", class: 'note-file-attach' do
30 30 - if note.attachment.image?
31 31 = image_tag note.attachment.url, class: 'note-image-attach'
32 32 - else
... ...
app/views/notes/_note.html.haml
... ... @@ -31,7 +31,7 @@
31 31 - if note.attachment.image?
32 32 = image_tag note.attachment.url, class: 'note-image-attach'
33 33 .attachment.pull-right
34   - = link_to note.attachment.url, target: "_blank" do
  34 + = link_to note.attachment.secure_url, target: "_blank" do
35 35 %i.icon-paper-clip
36 36 = note.attachment_identifier
37 37 .clear
... ...
config/routes.rb
... ... @@ -46,6 +46,11 @@ Gitlab::Application.routes.draw do
46 46 end
47 47  
48 48 #
  49 + # Attachments serving
  50 + #
  51 + get 'files/:type/:id/:filename' => 'files#download', constraints: { id: /\d+/, type: /[a-z]+/, filename: /[a-zA-Z.0-9_\-\+]+/ }
  52 +
  53 + #
49 54 # Admin Area
50 55 #
51 56 namespace :admin do
... ...