summaryrefslogtreecommitdiff
path: root/app/controllers/uploads_controller.rb
diff options
context:
space:
mode:
authorVinnie Okada <vokada@mrvinn.com>2015-03-17 20:53:09 -0600
committerVinnie Okada <vokada@mrvinn.com>2015-03-17 20:53:09 -0600
commitfeeffc442618d92040cd1cc38158b689a09988fd (patch)
treeb19c0ac2ddae23d830bbc69b99d920eec1f81363 /app/controllers/uploads_controller.rb
parent1a9c2ddc55cf563ea42d67811a19b2693d7a44e9 (diff)
parent5bbc70da9cb439342bdbe022988e4e734d891f44 (diff)
downloadgitlab-ce-feeffc442618d92040cd1cc38158b689a09988fd.tar.gz
Merge branch 'master' into markdown-tags
Use the latest HTML pipeline gem
Diffstat (limited to 'app/controllers/uploads_controller.rb')
-rw-r--r--app/controllers/uploads_controller.rb71
1 files changed, 71 insertions, 0 deletions
diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb
new file mode 100644
index 00000000000..c5f3da54ea2
--- /dev/null
+++ b/app/controllers/uploads_controller.rb
@@ -0,0 +1,71 @@
+class UploadsController < ApplicationController
+ skip_before_filter :authenticate_user!
+ before_filter :find_model, :authorize_access!
+
+ def show
+ uploader = @model.send(upload_mount)
+
+ unless uploader.file_storage?
+ return redirect_to uploader.url
+ end
+
+ unless uploader.file && uploader.file.exists?
+ return not_found!
+ end
+
+ disposition = uploader.image? ? 'inline' : 'attachment'
+ send_file uploader.file.path, disposition: disposition
+ end
+
+ private
+
+ def find_model
+ unless upload_model && upload_mount
+ return not_found!
+ end
+
+ @model = upload_model.find(params[:id])
+ end
+
+ def authorize_access!
+ authorized =
+ case @model
+ when Project
+ can?(current_user, :read_project, @model)
+ when Group
+ can?(current_user, :read_group, @model)
+ when Note
+ can?(current_user, :read_project, @model.project)
+ else
+ # No authentication required for user avatars.
+ true
+ end
+
+ return if authorized
+
+ if current_user
+ not_found!
+ else
+ authenticate_user!
+ end
+ end
+
+ def upload_model
+ upload_models = {
+ user: User,
+ project: Project,
+ note: Note,
+ group: Group
+ }
+
+ upload_models[params[:model].to_sym]
+ end
+
+ def upload_mount
+ upload_mounts = %w(avatar attachment file)
+
+ if upload_mounts.include?(params[:mounted_as])
+ params[:mounted_as]
+ end
+ end
+end