Read about our upcoming Code of Conduct on this issue

tree_controller.rb 1.9 KB
Newer Older
1
2
# frozen_string_literal: true

Robert Speicher's avatar
Robert Speicher committed
3
# Controller for viewing a repository's file structure
4
5
class Projects::TreeController < Projects::ApplicationController
  include ExtractsPath
6
  include CreatesCommit
Stan Hu's avatar
Stan Hu committed
7
  include ActionView::Helpers::SanitizeHelper
8
  include RedirectsForMissingPathOnTree
9

10
11
  around_action :allow_gitaly_ref_name_caching, only: [:show]

12
13
  before_action :require_non_empty_project, except: [:new, :create]
  before_action :assign_ref_vars
Stan Hu's avatar
Stan Hu committed
14
  before_action :assign_dir_vars, only: [:create_dir]
15
  before_action :authorize_download_code!
16
  before_action :authorize_edit_tree!, only: [:create_dir]
17

18
19
20
21
  before_action only: [:show] do
    push_frontend_feature_flag(:vue_file_list_lfs_badge)
  end

22
  def show
23
    return render_404 unless @repository.commit(@ref)
24

25
26
    if tree.entries.empty?
      if @repository.blob_at(@commit.id, @path)
27
        return redirect_to project_blob_path(@project, File.join(@ref, @path))
28
      elsif @path.present?
29
        return redirect_to_tree_root_for_missing_path(@project, @ref, @path)
30
31
      end
    end
32

Robert Speicher's avatar
Robert Speicher committed
33
    respond_to do |format|
34
      format.html do
35
        lfs_blob_ids if Feature.disabled?(:vue_file_list, @project, default_enabled: true)
36

37
38
        @last_commit = @repository.last_commit_for_path(@commit.id, @tree.path) || @commit
      end
Robert Speicher's avatar
Robert Speicher committed
39
40
    end
  end
Stan Hu's avatar
Stan Hu committed
41
42

  def create_dir
43
    return render_404 unless @commit_params.values.all?
Stan Hu's avatar
Stan Hu committed
44

45
    create_commit(Files::CreateDirService,  success_notice: _("The directory has been successfully created."),
46
47
                                            success_path: project_tree_path(@project, File.join(@branch_name, @dir_name)),
                                            failure_path: project_tree_path(@project, @ref))
Stan Hu's avatar
Stan Hu committed
48
49
  end

50
51
  private

Stan Hu's avatar
Stan Hu committed
52
  def assign_dir_vars
Douwe Maan's avatar
Douwe Maan committed
53
    @branch_name = params[:branch_name]
54

Stan Hu's avatar
Stan Hu committed
55
56
57
    @dir_name = File.join(@path, params[:dir_name])
    @commit_params = {
      file_path: @dir_name,
58
      commit_message: params[:commit_message]
Stan Hu's avatar
Stan Hu committed
59
60
    }
  end
Robert Speicher's avatar
Robert Speicher committed
61
end