2018-11-08 19:23:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module RecordsUploads
|
2018-03-17 18:26:18 +05:30
|
|
|
module Concern
|
|
|
|
extend ActiveSupport::Concern
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
attr_accessor :upload
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
included do
|
|
|
|
after :store, :record_upload
|
|
|
|
before :remove, :destroy_upload
|
|
|
|
end
|
|
|
|
|
|
|
|
# After storing an attachment, create a corresponding Upload record
|
|
|
|
#
|
|
|
|
# NOTE: We're ignoring the argument passed to this callback because we want
|
|
|
|
# the `SanitizedFile` object from `CarrierWave::Uploader::Base#file`, not the
|
|
|
|
# `Tempfile` object the callback gets.
|
|
|
|
#
|
|
|
|
# Called `after :store`
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def record_upload(_tempfile = nil)
|
|
|
|
return unless model
|
|
|
|
return unless file && file.exists?
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
Upload.transaction { readd_upload }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
def readd_upload
|
2023-06-20 00:43:36 +05:30
|
|
|
Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification.temporary_ignore_tables_in_transaction(
|
|
|
|
%w[uploads], url: "https://gitlab.com/gitlab-org/gitlab/-/issues/398199"
|
|
|
|
) do
|
|
|
|
uploads.where(model: model, path: upload_path).delete_all
|
|
|
|
upload.delete if upload
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
self.upload = build_upload.tap(&:save!)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
def upload_path
|
|
|
|
File.join(store_dir, filename.to_s)
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def filename
|
|
|
|
upload&.path ? File.basename(upload.path) : super
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
private
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def uploads
|
|
|
|
Upload.order(id: :desc).where(uploader: self.class.to_s)
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def build_upload
|
|
|
|
Upload.new(
|
|
|
|
uploader: self.class.to_s,
|
|
|
|
size: file.size,
|
|
|
|
path: upload_path,
|
|
|
|
model: model,
|
|
|
|
mount_point: mounted_as
|
|
|
|
)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# Before removing an attachment, destroy any Upload records at the same path
|
|
|
|
#
|
|
|
|
# Called `before :remove`
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def destroy_upload(*args)
|
|
|
|
return unless file && file.exists?
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
self.upload = nil
|
|
|
|
uploads.where(path: upload_path).delete_all
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|