summaryrefslogtreecommitdiff
path: root/app/uploaders/job_artifact_uploader.rb
blob: 5ee8c42f5102452b4b8dfb9f5f8c75467065fb3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

class JobArtifactUploader < GitlabUploader
  extend Workhorse::UploadPath
  include ObjectStorage::Concern
  include ObjectStorage::CDN::Concern

  UnknownFileLocationError = Class.new(StandardError)

  storage_location :artifacts

  alias_method :upload, :model

  def cached_size
    return model.size if model.size.present? && !model.file_changed?

    size
  end

  def store_dir
    dynamic_segment
  end

  private

  def dynamic_segment
    # This now tests model.created_at because it can for some reason be nil in the test suite,
    # and it's not clear if this is intentional or not
    raise ObjectNotReadyError, 'JobArtifact is not ready' unless model.id && model.created_at

    if model.hashed_path?
      hashed_path
    elsif model.legacy_path?
      legacy_path
    else
      raise UnknownFileLocationError
    end
  end

  def hashed_path
    Gitlab::HashedPath.new(model.created_at.utc.strftime('%Y_%m_%d'), model.job_id, model.id, root_hash: model.project_id)
  end

  def legacy_path
    File.join(model.created_at.utc.strftime('%Y_%m'), model.project_id.to_s, model.job_id.to_s)
  end
end