2020-08-12 00:09:52 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
module Artifactable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2021-03-31 15:10:27 +00:00
|
|
|
include ObjectStorable
|
2022-08-05 21:09:12 +00:00
|
|
|
include Gitlab::Ci::Artifacts::Logger
|
2020-08-27 15:10:21 +00:00
|
|
|
|
2021-03-31 15:10:27 +00:00
|
|
|
STORE_COLUMN = :file_store
|
|
|
|
NotSupportedAdapterError = Class.new(StandardError)
|
2020-08-12 00:09:52 +00:00
|
|
|
FILE_FORMAT_ADAPTERS = {
|
2022-08-31 15:12:39 +00:00
|
|
|
# While zip is a streamable file format, performing streaming
|
|
|
|
# reads requires that each entry in the zip has certain headers
|
|
|
|
# present at the front of the entry. These headers are OPTIONAL
|
|
|
|
# according to the file format specification. GitLab Runner uses
|
|
|
|
# Go's `archive/zip` to create zip archives, which does not include
|
|
|
|
# these headers. Go maintainers have expressed that they don't intend
|
|
|
|
# to support them: https://github.com/golang/go/issues/23301#issuecomment-363240781
|
|
|
|
#
|
|
|
|
# If you need GitLab to be able to read Artifactables, store them in
|
|
|
|
# raw or gzip format instead of zip.
|
2020-08-12 00:09:52 +00:00
|
|
|
gzip: Gitlab::Ci::Build::Artifacts::Adapters::GzipStream,
|
|
|
|
raw: Gitlab::Ci::Build::Artifacts::Adapters::RawStream
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
included do
|
|
|
|
enum file_format: {
|
|
|
|
raw: 1,
|
|
|
|
zip: 2,
|
|
|
|
gzip: 3
|
|
|
|
}, _suffix: true
|
2020-09-01 12:11:01 +00:00
|
|
|
|
2020-12-22 12:10:09 +00:00
|
|
|
scope :expired_before, -> (timestamp) { where(arel_table[:expire_at].lt(timestamp)) }
|
2022-05-30 06:09:32 +00:00
|
|
|
scope :expired, -> { expired_before(Time.current) }
|
2021-03-31 15:10:27 +00:00
|
|
|
scope :project_id_in, ->(ids) { where(project_id: ids) }
|
2020-08-12 00:09:52 +00:00
|
|
|
end
|
2020-08-27 15:10:21 +00:00
|
|
|
|
|
|
|
def each_blob(&blk)
|
|
|
|
unless file_format_adapter_class
|
|
|
|
raise NotSupportedAdapterError, 'This file format requires a dedicated adapter'
|
|
|
|
end
|
|
|
|
|
2022-08-05 21:09:12 +00:00
|
|
|
log_artifacts_filesize(file.model)
|
2022-07-06 12:08:36 +00:00
|
|
|
|
2020-08-27 15:10:21 +00:00
|
|
|
file.open do |stream|
|
|
|
|
file_format_adapter_class.new(stream).each_blob(&blk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def file_format_adapter_class
|
|
|
|
FILE_FORMAT_ADAPTERS[file_format.to_sym]
|
|
|
|
end
|
2020-08-12 00:09:52 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-01 18:13:56 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Ci::Artifactable.prepend_mod
|