summaryrefslogtreecommitdiff
path: root/app/models/import_export_upload.rb
blob: bc363cce8dd82f60d799c1c6fd4829d53fbdef7b (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
48
49
50
51
52
# frozen_string_literal: true

class ImportExportUpload < ApplicationRecord
  include WithUploads
  include ObjectStorage::BackgroundMove

  belongs_to :project
  belongs_to :group

  # These hold the project Import/Export archives (.tar.gz files)
  mount_uploader :import_file, ImportExportUploader
  mount_uploader :export_file, ImportExportUploader

  # This causes CarrierWave v1 and v3 (but not v2) to upload the file to
  # object storage *after* the database entry has been committed to the
  # database. This avoids idling in a transaction.
  if Gitlab::Utils.to_boolean(ENV.fetch('ENABLE_STORE_EXPORT_FILE_AFTER_COMMIT', true))
    skip_callback :save, :after, :store_export_file!
    set_callback :commit, :after, :store_export_file!
  end

  scope :updated_before, ->(date) { where('updated_at < ?', date) }
  scope :with_export_file, -> { where.not(export_file: nil) }

  def retrieve_upload(_identifier, paths)
    Upload.find_by(model: self, path: paths)
  end

  def export_file_exists?
    !!carrierwave_export_file
  end

  # This checks if the export archive is actually stored on disk. It
  # requires a HEAD request if object storage is used.
  def export_archive_exists?
    !!carrierwave_export_file&.exists?
  # Handle any HTTP unexpected error
  # https://github.com/excon/excon/blob/bbb5bd791d0bb2251593b80e3bce98dbec6e8f24/lib/excon/error.rb#L129-L169
  rescue Excon::Error => e
    # The HEAD request will fail with a 403 Forbidden if the file does not
    # exist, and the user does not have permission to list the object
    # storage bucket.
    Gitlab::ErrorTracking.track_exception(e)
    false
  end

  private

  def carrierwave_export_file
    export_file&.file
  end
end