job_artifact_uploader.rb 1.04 KB
Newer Older
1 2 3
class JobArtifactUploader < GitlabUploader
  extend Workhorse::UploadPath
  include ObjectStorage::Concern
4

5 6
  ObjectNotReadyError = Class.new(StandardError)

7
  storage_options Gitlab.config.artifacts
8

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

12
    size
13 14
  end

15 16 17 18
  def store_dir
    dynamic_segment
  end

19
  def open
20 21 22
    if file_storage?
      File.open(path, "rb") if path
    else
23
      ::Gitlab::Ci::Trace::HttpIO.new(url, cached_size) if url
24
    end
25 26
  end

27 28
  private

29
  def dynamic_segment
30 31
    raise ObjectNotReadyError, 'JobArtifact is not ready' unless model.id

32
    creation_date = model.created_at.utc.strftime('%Y_%m_%d')
33

34
    if model.current_path?
35 36
      File.join(disk_hash[0..1], disk_hash[2..3], disk_hash,
                creation_date, model.job_id.to_s, model.id.to_s)
37
    elsif model.legacy_path?
38
      File.join(model.created_at.utc.strftime('%Y_%m'), model.project_id.to_s, model.job_id.to_s)
39
    end
40 41 42
  end

  def disk_hash
43
    @disk_hash ||= Digest::SHA2.hexdigest(model.project_id.to_s)
44 45
  end
end