artifacts_controller.rb 2.11 KB
Newer Older
1
class Projects::ArtifactsController < Projects::ApplicationController
2
  include ExtractsPath
3
  include RendersBlob
4

5
  layout 'project'
6
  before_action :authorize_read_build!
7
  before_action :authorize_update_build!, only: [:keep]
8
  before_action :extract_ref_name_and_path
9
  before_action :validate_artifacts!
10
  before_action :entry, only: [:file]
11 12

  def download
13 14 15 16
    if artifacts_file.file_storage?
      send_file artifacts_file.path, disposition: 'attachment'
    else
      redirect_to artifacts_file.url
17 18 19
    end
  end

20
  def browse
21 22
    @path = params[:path]
    directory = @path ? "#{@path}/" : ''
23
    @entry = build.artifacts_metadata_entry(directory)
24

Lin Jen-Shin's avatar
Lin Jen-Shin committed
25
    render_404 unless @entry.exists?
26 27
  end

28
  def file
29
    blob = @entry.blob
30
    conditionally_expand_blob(blob)
31

32 33 34 35 36 37 38 39
    respond_to do |format|
      format.html do
        render 'file'
      end

      format.json do
        render_blob_json(blob)
      end
40
    end
41 42
  end

43
  def raw
44 45 46 47
    path = Gitlab::Ci::Build::Artifacts::Path
      .new(params[:path])

    send_artifacts_entry(build, path)
48 49
  end

50 51
  def keep
    build.keep_artifacts!
52
    redirect_to project_job_path(project, build)
53 54
  end

55
  def latest_succeeded
56
    target_path = artifacts_action_path(@path, project, build)
57

58
    if target_path
59
      redirect_to(target_path)
60 61 62 63 64
    else
      render_404
    end
  end

65 66
  private

67 68 69 70 71 72
  def extract_ref_name_and_path
    return unless params[:ref_name_and_path]

    @ref_name, @path = extract_ref(params[:ref_name_and_path])
  end

73
  def validate_artifacts!
74
    render_404 unless build && build.artifacts?
75 76
  end

77
  def build
78 79 80 81
    @build ||= begin
      build = build_from_id || build_from_ref
      build&.present(current_user: current_user)
    end
82 83 84
  end

  def build_from_id
85
    project.builds.find_by(id: params[:job_id]) if params[:job_id]
86 87 88
  end

  def build_from_ref
89
    return unless @ref_name
90

91 92
    builds = project.latest_successful_builds_for(@ref_name)
    builds.find_by(name: params[:job])
93 94 95 96 97
  end

  def artifacts_file
    @artifacts_file ||= build.artifacts_file
  end
98

99 100
  def entry
    @entry = build.artifacts_metadata_entry(params[:path])
101 102 103

    render_404 unless @entry.exists?
  end
104
end