From 576cec6c67dcc4ee00b8220ca1a45385583e25b2 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 17 Sep 2012 13:49:57 -0400 Subject: Add BlobController, remove Refs#blob --- app/controllers/blob_controller.rb | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 app/controllers/blob_controller.rb (limited to 'app/controllers/blob_controller.rb') diff --git a/app/controllers/blob_controller.rb b/app/controllers/blob_controller.rb new file mode 100644 index 00000000000..bb051281013 --- /dev/null +++ b/app/controllers/blob_controller.rb @@ -0,0 +1,62 @@ +# Controller for viewing a file's blame +class BlobController < ApplicationController + # Thrown when given an invalid path + class InvalidPathError < StandardError; end + + include RefExtractor + include Gitlab::Encode + + layout "project" + + before_filter :project + + # Authorize + before_filter :add_project_abilities + before_filter :authorize_read_project! + before_filter :authorize_code_access! + before_filter :require_non_empty_project + + before_filter :define_tree_vars + + def show + if @tree.is_blob? + if @tree.text? + encoding = detect_encoding(@tree.data) + mime_type = encoding ? "text/plain; charset=#{encoding}" : "text/plain" + else + mime_type = @tree.mime_type + end + + send_data( + @tree.data, + type: mime_type, + disposition: 'inline', + filename: @tree.name + ) + else + not_found! + end + end + + private + + def define_tree_vars + @ref, @path = extract_ref(params[:id]) + + @id = File.join(@ref, @path) + @repo = @project.repo + @commit = CommitDecorator.decorate(@project.commit(@ref)) + + @tree = Tree.new(@commit.tree, @project, @ref, @path) + @tree = TreeDecorator.new(@tree) + + raise InvalidPathError if @tree.invalid? + + @hex_path = Digest::SHA1.hexdigest(@path) + + @history_path = project_tree_path(@project, @id) + @logs_path = logs_file_project_ref_path(@project, @ref, @path) + rescue NoMethodError, InvalidPathError + not_found! + end +end -- cgit v1.2.1 From 398ba6f1bb60f176444dedc7b26188e08b920f54 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 17 Sep 2012 14:24:31 -0400 Subject: DRY up Blame, Blob and Tree controllers --- app/controllers/blob_controller.rb | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) (limited to 'app/controllers/blob_controller.rb') diff --git a/app/controllers/blob_controller.rb b/app/controllers/blob_controller.rb index bb051281013..58e70bc9d04 100644 --- a/app/controllers/blob_controller.rb +++ b/app/controllers/blob_controller.rb @@ -16,7 +16,7 @@ class BlobController < ApplicationController before_filter :authorize_code_access! before_filter :require_non_empty_project - before_filter :define_tree_vars + before_filter :assign_ref_vars def show if @tree.is_blob? @@ -37,26 +37,4 @@ class BlobController < ApplicationController not_found! end end - - private - - def define_tree_vars - @ref, @path = extract_ref(params[:id]) - - @id = File.join(@ref, @path) - @repo = @project.repo - @commit = CommitDecorator.decorate(@project.commit(@ref)) - - @tree = Tree.new(@commit.tree, @project, @ref, @path) - @tree = TreeDecorator.new(@tree) - - raise InvalidPathError if @tree.invalid? - - @hex_path = Digest::SHA1.hexdigest(@path) - - @history_path = project_tree_path(@project, @id) - @logs_path = logs_file_project_ref_path(@project, @ref, @path) - rescue NoMethodError, InvalidPathError - not_found! - end end -- cgit v1.2.1 From a1e68a91205186287f21fb5fd1669acebcd7e79e Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 20 Sep 2012 13:55:14 -0400 Subject: Rename RefExtractor to ExtractsPath Update docs a bit --- app/controllers/blob_controller.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'app/controllers/blob_controller.rb') diff --git a/app/controllers/blob_controller.rb b/app/controllers/blob_controller.rb index 58e70bc9d04..33387842ec2 100644 --- a/app/controllers/blob_controller.rb +++ b/app/controllers/blob_controller.rb @@ -1,9 +1,6 @@ # Controller for viewing a file's blame class BlobController < ApplicationController - # Thrown when given an invalid path - class InvalidPathError < StandardError; end - - include RefExtractor + include ExtractsPath include Gitlab::Encode layout "project" -- cgit v1.2.1