blob_controller.rb 6.61 KB
Newer Older
1
# Controller for viewing a file's blame
2
class Projects::BlobController < Projects::ApplicationController
3
  include ExtractsPath
4
  include CreatesCommit
5
  include RendersBlob
6
  include ActionView::Helpers::SanitizeHelper
7

8
  # Raised when given an invalid file path
9
  InvalidPathError = Class.new(StandardError)
10

11 12
  prepend_before_action :authenticate_user!, only: [:edit]

13 14
  before_action :require_non_empty_project, except: [:new, :create]
  before_action :authorize_download_code!
15
  before_action :authorize_edit_tree!, only: [:new, :create, :update, :destroy]
16 17 18
  before_action :assign_blob_vars
  before_action :commit, except: [:new, :create]
  before_action :blob, except: [:new, :create]
19
  before_action :require_branch_head, only: [:edit, :update]
20
  before_action :editor_variables, except: [:show, :preview, :diff]
21
  before_action :validate_diff_params, only: :diff
22
  before_action :set_last_commit_sha, only: [:edit, :update]
23 24 25 26

  def new
    commit unless @repository.empty?
  end
27

28
  def create
29
    create_commit(Files::CreateService, success_notice: "The file has been successfully created.",
30
                                        success_path: -> { project_blob_path(@project, File.join(@branch_name, @file_path)) },
Douwe Maan's avatar
Douwe Maan committed
31
                                        failure_view: :new,
32
                                        failure_path: project_new_blob_path(@project, @ref))
33
  end
34

35
  def show
36
    conditionally_expand_blob(@blob)
Douwe Maan's avatar
Douwe Maan committed
37

38 39
    respond_to do |format|
      format.html do
40
        show_html
41 42 43
      end

      format.json do
44 45
        page_title @blob.path, @ref, @project.name_with_namespace

46
        show_json
47 48
      end
    end
49 50
  end

51
  def edit
52
    if can_collaborate_with_project?
53
      blob.load_all_data!
54 55 56
    else
      redirect_to action: 'show'
    end
57 58 59
  end

  def update
60
    @path = params[:file_path] if params[:file_path].present?
61
    create_commit(Files::UpdateService, success_path: -> { after_edit_path },
Douwe Maan's avatar
Douwe Maan committed
62
                                        failure_view: :edit,
63
                                        failure_path: project_blob_path(@project, @id))
64 65 66 67

  rescue Files::UpdateService::FileChangedError
    @conflict = true
    render :edit
68 69 70 71
  end

  def preview
    @content = params[:content]
72
    @blob.load_all_data!
73
    diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
74 75
    diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
    diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
76
    @diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
77 78 79 80

    render layout: false
  end

81
  def destroy
82
    create_commit(Files::DeleteService, success_notice: "The file has been successfully deleted.",
83
                                        success_path: -> { project_tree_path(@project, @branch_name) },
84
                                        failure_view: :show,
85
                                        failure_path: project_blob_path(@project, @id))
86 87
  end

skv's avatar
skv committed
88
  def diff
89 90
    apply_diff_view_cookie!

91 92 93 94 95
    @blob.load_all_data!
    @lines = Gitlab::Highlight.highlight(@blob.path, @blob.data, repository: @repository).lines

    @form = UnfoldForm.new(params)
    @lines = @lines[@form.since - 1..@form.to - 1].map(&:html_safe)
skv's avatar
skv committed
96 97 98 99 100 101 102 103 104 105 106 107

    if @form.bottom?
      @match_line = ''
    else
      lines_length = @lines.length - 1
      line = [@form.since, lines_length].join(',')
      @match_line = "@@ -#{line}+#{line} @@"
    end

    render layout: false
  end

108 109 110
  private

  def blob
111
    @blob ||= @repository.blob_at(@commit.id, @path)
112

113 114 115
    if @blob
      @blob
    else
116 117
      if tree = @repository.tree(@commit.id, @path)
        if tree.entries.any?
118
          return redirect_to project_tree_path(@project, File.join(@ref, @path))
119 120 121
        end
      end

122
      return render_404
123
    end
124
  end
125 126 127 128

  def commit
    @commit = @repository.commit(@ref)

129
    return render_404 unless @commit
130 131 132 133 134 135 136
  end

  def assign_blob_vars
    @id = params[:id]
    @ref, @path = extract_ref(@id)

  rescue InvalidPathError
137
    render_404
138 139
  end

140 141
  def after_edit_path
    from_merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).execute.find_by(iid: params[:from_merge_request_iid])
142
    if from_merge_request && @branch_name == @ref
143
      diffs_project_merge_request_path(from_merge_request.target_project, from_merge_request) +
144 145
        "##{hexdigest(@path)}"
    else
146
      project_blob_path(@project, File.join(@branch_name, @path))
147
    end
148
  end
149

150
  def editor_variables
151
    @branch_name = params[:branch_name]
152 153 154

    @file_path =
      if action_name.to_s == 'create'
155 156 157
        if params[:file].present?
          params[:file_name] = params[:file].original_filename
        end
158
        File.join(@path, params[:file_name])
159 160
      elsif params[:file_path].present?
        params[:file_path]
161 162 163 164
      else
        @path
      end

165 166 167 168 169
    if params[:file].present?
      params[:content] = Base64.encode64(params[:file].read)
      params[:encoding] = 'base64'
    end

170 171 172
    @commit_params = {
      file_path: @file_path,
      commit_message: params[:commit_message],
173
      previous_path: @path,
174
      file_content: params[:content],
175 176
      file_content_encoding: params[:encoding],
      last_commit_sha: params[:last_commit_sha]
177 178
    }
  end
179 180 181 182 183 184

  def validate_diff_params
    if [:since, :to, :offset].any? { |key| params[key].blank? }
      render nothing: true
    end
  end
185 186

  def set_last_commit_sha
187 188
    @last_commit_sha = Gitlab::Git::Commit
      .last_for_path(@repository, @ref, @path).sha
189
  end
190 191 192 193 194 195 196 197 198 199 200 201 202

  def show_html
    environment_params = @repository.branch_exists?(@ref) ? { ref: @ref } : { commit: @commit }
    @environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last
    @last_commit = @repository.last_commit_for_path(@commit.id, @blob.path)

    render 'show'
  end

  def show_json
    json = blob_json(@blob)
    return render_404 unless json

Jacob Schatz's avatar
Jacob Schatz committed
203 204 205 206
    path_segments = @path.split('/')
    path_segments.pop
    tree_path = path_segments.join('/')

207
    render json: json.merge(
208
      id: @blob.id,
209 210 211 212 213 214 215 216 217 218 219 220 221
      path: blob.path,
      name: blob.name,
      extension: blob.extension,
      size: blob.raw_size,
      mime_type: blob.mime_type,
      binary: blob.raw_binary?,
      simple_viewer: blob.simple_viewer&.class&.partial_name,
      rich_viewer: blob.rich_viewer&.class&.partial_name,
      show_viewer_switcher: !!blob.show_viewer_switcher?,
      render_error: blob.simple_viewer&.render_error || blob.rich_viewer&.render_error,
      raw_path: project_raw_path(project, @id),
      blame_path: project_blame_path(project, @id),
      commits_path: project_commits_path(project, @id),
Jacob Schatz's avatar
Jacob Schatz committed
222
      tree_path: project_tree_path(project, File.join(@ref, tree_path)),
223 224 225
      permalink: project_blob_path(project, File.join(@commit.id, @path))
    )
  end
226
end