diff options
author | Hannes Rosenögger <123haynes@gmail.com> | 2015-02-14 21:52:45 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-02-18 00:21:13 +0300 |
commit | 9729cc584f5758395960416f308a9c45f698cdee (patch) | |
tree | e4b45ad34333e1a9ca9035bfddf5e7d2ef56932c | |
parent | ca504a77fe994da893cd0632de5e0e7ea5b729fc (diff) |
implement Project::UploadsController
-rw-r--r-- | app/controllers/files_controller.rb | 29 | ||||
-rw-r--r-- | app/controllers/projects/uploads_controller.rb | 16 | ||||
-rw-r--r-- | app/uploaders/file_uploader.rb | 4 | ||||
-rw-r--r-- | config/routes.rb | 5 | ||||
-rw-r--r-- | spec/services/projects/file_service_spec.rb | 8 |
5 files changed, 28 insertions, 34 deletions
diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb index a86340dd9bb..15523cbc2e7 100644 --- a/app/controllers/files_controller.rb +++ b/app/controllers/files_controller.rb @@ -1,5 +1,5 @@ class FilesController < ApplicationController - def download_notes + def download note = Note.find(params[:id]) uploader = note.attachment @@ -14,32 +14,7 @@ class FilesController < ApplicationController not_found! end else - not_found! + redirect_to uploader.url end end - - def download_files - namespace_id = params[:namespace] - project_id = params[:project] - folder_id = params[:folder_id] - filename = params[:filename] - project_with_namespace="#{namespace_id}/#{project_id}" - filename_with_id="#{folder_id}/#{filename}" - - project = Project.find_with_namespace(project_with_namespace) - - uploader = FileUploader.new("#{Rails.root}/uploads","#{project_with_namespace}/#{folder_id}") - uploader.retrieve_from_store!(filename) - - if can?(current_user, :read_project, project) - download(uploader) - else - not_found! - end - end - - def download(uploader) - disposition = uploader.image? ? 'inline' : 'attachment' - send_file uploader.file.path, disposition: disposition - end end diff --git a/app/controllers/projects/uploads_controller.rb b/app/controllers/projects/uploads_controller.rb new file mode 100644 index 00000000000..1c9fb1c86fb --- /dev/null +++ b/app/controllers/projects/uploads_controller.rb @@ -0,0 +1,16 @@ +class Projects::UploadsController < Projects::ApplicationController + layout 'project' + + before_filter :project + + def show + folder_id = params[:folder_id] + filename = params[:filename] + + uploader = FileUploader.new("#{Rails.root}/uploads","#{@project.path_with_namespace}/#{folder_id}") + uploader.retrieve_from_store!(filename) + + disposition = uploader.image? ? 'inline' : 'attachment' + send_file uploader.file.path, disposition: disposition + end +end diff --git a/app/uploaders/file_uploader.rb b/app/uploaders/file_uploader.rb index ac7bd5b27ec..51ae8040e52 100644 --- a/app/uploaders/file_uploader.rb +++ b/app/uploaders/file_uploader.rb @@ -40,7 +40,9 @@ class FileUploader < CarrierWave::Uploader::Base end def secure_url - Gitlab.config.gitlab.relative_url_root + "/files/#{@path}/#{@filename}" + path_array = @path.split('/') + path = File.join(path_array[0],path_array[1],'uploads',path_array[2]) + Gitlab.config.gitlab.relative_url_root + "/#{path}/#{@filename}" end def image? diff --git a/config/routes.rb b/config/routes.rb index bd659ddeabd..d29ad8db639 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -93,8 +93,7 @@ Gitlab::Application.routes.draw do # # Attachments serving # - get 'files/:type/:id/:filename' => 'files#download_notes', constraints: { id: /\d+/, type: /[a-z]+/, filename: /.+/ } - get 'files/:namespace/:project/:folder_id/:filename' => 'files#download_files', constraints: { namespace: /[^\/]+/, project: /[a-zA-Z.\/0-9_\-]+/, filename: /.+/ } + get 'files/:type/:id/:filename' => 'files#download', constraints: { id: /\d+/, type: /[a-z]+/, filename: /.+/ } # # Admin Area @@ -257,6 +256,8 @@ Gitlab::Application.routes.draw do end end + get '/uploads/:folder_id/:filename' => 'uploads#show', constraints: { filename: /.+/ } + get '/compare/:from...:to' => 'compare#show', :as => 'compare', :constraints => { from: /.+/, to: /.+/ } diff --git a/spec/services/projects/file_service_spec.rb b/spec/services/projects/file_service_spec.rb index e2d6766735e..7bbe5b575c9 100644 --- a/spec/services/projects/file_service_spec.rb +++ b/spec/services/projects/file_service_spec.rb @@ -20,7 +20,7 @@ describe Projects::FileService do it { expect(@link_to_file).to have_key('is_image') } it { expect(@link_to_file).to have_value('banana_sample') } it { expect(@link_to_file['is_image']).to equal(true) } - it { expect(@link_to_file['url']).to match("/files/#{@project.path_with_namespace}") } + it { expect(@link_to_file['url']).to match("/#{@project.path_with_namespace}") } it { expect(@link_to_file['url']).to match('banana_sample.gif') } end @@ -38,7 +38,7 @@ describe Projects::FileService do it { expect(@link_to_file).to have_value('dk') } it { expect(@link_to_file).to have_key('is_image') } it { expect(@link_to_file['is_image']).to equal(true) } - it { expect(@link_to_file['url']).to match("/files/#{@project.path_with_namespace}") } + it { expect(@link_to_file['url']).to match("/#{@project.path_with_namespace}") } it { expect(@link_to_file['url']).to match('dk.png') } end @@ -53,7 +53,7 @@ describe Projects::FileService do it { expect(@link_to_file).to have_key('is_image') } it { expect(@link_to_file).to have_value('rails_sample') } it { expect(@link_to_file['is_image']).to equal(true) } - it { expect(@link_to_file['url']).to match("/files/#{@project.path_with_namespace}") } + it { expect(@link_to_file['url']).to match("/#{@project.path_with_namespace}") } it { expect(@link_to_file['url']).to match('rails_sample.jpg') } end @@ -70,7 +70,7 @@ describe Projects::FileService do it { expect(@link_to_file).to have_key('is_image') } it { expect(@link_to_file).to have_value('doc_sample.txt') } it { expect(@link_to_file['is_image']).to equal(false) } - it { expect(@link_to_file['url']).to match("/files/#{@project.path_with_namespace}") } + it { expect(@link_to_file['url']).to match("/#{@project.path_with_namespace}") } it { expect(@link_to_file['url']).to match('doc_sample.txt') } end end |