BigW Consortium Gitlab

tree_controller.rb 1.67 KB
Newer Older
1
# Controller for viewing a repository's file structure
2 3
class Projects::TreeController < Projects::ApplicationController
  include ExtractsPath
4
  include CreatesCommit
Stan Hu committed
5
  include ActionView::Helpers::SanitizeHelper
6

7 8
  before_action :require_non_empty_project, except: [:new, :create]
  before_action :assign_ref_vars
Stan Hu committed
9
  before_action :assign_dir_vars, only: [:create_dir]
10
  before_action :authorize_download_code!
11
  before_action :authorize_edit_tree!, only: [:create_dir]
12

13
  def show
14
    return render_404 unless @repository.commit(@ref)
15

16 17
    if tree.entries.empty?
      if @repository.blob_at(@commit.id, @path)
Vinnie Okada committed
18 19 20 21
        redirect_to(
          namespace_project_blob_path(@project.namespace, @project,
                                      File.join(@ref, @path))
        ) and return
22
      elsif @path.present?
23
        return render_404
24 25
      end
    end
26

27 28 29 30 31 32
    respond_to do |format|
      format.html
      # Disable cache so browser history works
      format.js { no_cache_headers }
    end
  end
Stan Hu committed
33 34

  def create_dir
35
    return render_404 unless @commit_params.values.all?
Stan Hu committed
36

37
    create_commit(Files::CreateDirService,  success_notice: "The directory has been successfully created.",
38
                                            success_path: namespace_project_tree_path(@project.namespace, @project, File.join(@target_branch, @dir_name)),
39
                                            failure_path: namespace_project_tree_path(@project.namespace, @project, @ref))
Stan Hu committed
40 41
  end

42 43
  private

Stan Hu committed
44
  def assign_dir_vars
45 46
    @target_branch = params[:target_branch]

Stan Hu committed
47 48 49 50 51 52
    @dir_name = File.join(@path, params[:dir_name])
    @commit_params = {
      file_path: @dir_name,
      commit_message: params[:commit_message],
    }
  end
53
end