2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class ImportExportUpload < ApplicationRecord
|
2018-06-25 09:10:26 -04:00
|
|
|
include WithUploads
|
|
|
|
include ObjectStorage::BackgroundMove
|
|
|
|
|
|
|
|
belongs_to :project
|
2019-11-11 16:06:20 -05:00
|
|
|
belongs_to :group
|
2018-06-25 09:10:26 -04:00
|
|
|
|
2018-07-26 06:55:21 -04:00
|
|
|
# These hold the project Import/Export archives (.tar.gz files)
|
2018-06-25 09:10:26 -04:00
|
|
|
mount_uploader :import_file, ImportExportUploader
|
|
|
|
mount_uploader :export_file, ImportExportUploader
|
|
|
|
|
2021-06-15 02:10:17 -04:00
|
|
|
# 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
|
|
|
|
|
2021-06-08 14:10:23 -04:00
|
|
|
scope :updated_before, ->(date) { where('updated_at < ?', date) }
|
|
|
|
scope :with_export_file, -> { where.not(export_file: nil) }
|
|
|
|
|
2018-06-25 09:10:26 -04:00
|
|
|
def retrieve_upload(_identifier, paths)
|
|
|
|
Upload.find_by(model: self, path: paths)
|
|
|
|
end
|
2021-06-15 02:10:17 -04:00
|
|
|
|
|
|
|
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
|
2018-06-25 09:10:26 -04:00
|
|
|
end
|