2018-07-04 13:32:46 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-15 13:11:44 -05:00
|
|
|
module RecordsUploads
|
2018-01-29 12:57:34 -05:00
|
|
|
module Concern
|
|
|
|
extend ActiveSupport::Concern
|
2017-02-15 13:11:44 -05:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
attr_accessor :upload
|
2017-02-15 13:11:44 -05:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
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-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-29 12:57:34 -05:00
|
|
|
def record_upload(_tempfile = nil)
|
|
|
|
return unless model
|
|
|
|
return unless file && file.exists?
|
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
Upload.transaction { readd_upload }
|
2018-01-29 12:57:34 -05:00
|
|
|
end
|
2019-01-14 19:08:28 -05:00
|
|
|
|
|
|
|
def readd_upload
|
2019-07-11 00:48:50 -04:00
|
|
|
uploads.where(model: model, path: upload_path).delete_all
|
2019-01-14 19:08:28 -05:00
|
|
|
upload.delete if upload
|
|
|
|
|
|
|
|
self.upload = build_upload.tap(&:save!)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-01-29 12:57:34 -05:00
|
|
|
|
|
|
|
def upload_path
|
|
|
|
File.join(store_dir, filename.to_s)
|
|
|
|
end
|
|
|
|
|
Speed up avatar URLs with object storage
With object storage enabled, calling `#filename` on an upload does this:
1. Call the `#filename` method on the CarrierWave object.
2. Generate the URL for that object.
3. If the uploader isn't public, do so by generating an authenticated
URL, including signing that request.
That's all correct behaviour, but for the case where we use `#filename`,
it's typically to generate a GitLab URL. That URL doesn't need to be
signed because we do our own auth.
Signing the URLs can be very expensive, especially in batch (say, we
need to get the avatar URLs for 150 users in one request). It's all
unnecessary work. If we used the `RecordsUploads` concern, we have
already recorded a `path` in the database. That `path` is actually
generated from CarrierWave's `#filename` at upload time, so we don't
need to recompute it - we can just use it and strip off the prefix if
it's available.
On a sample users autocomplete URL, at least 10% of the time before this
change went to signing URLs. After this change, we spend no time in URL
signing, and still get the correct results.
2019-04-02 07:52:28 -04:00
|
|
|
def filename
|
|
|
|
upload&.path ? File.basename(upload.path) : super
|
|
|
|
end
|
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-29 12:57:34 -05:00
|
|
|
def uploads
|
|
|
|
Upload.order(id: :desc).where(uploader: self.class.to_s)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-15 13:11:44 -05:00
|
|
|
|
2018-01-29 16:06:17 -05:00
|
|
|
def build_upload
|
2018-01-29 12:57:34 -05:00
|
|
|
Upload.new(
|
2018-01-29 16:06:17 -05:00
|
|
|
uploader: self.class.to_s,
|
|
|
|
size: file.size,
|
|
|
|
path: upload_path,
|
|
|
|
model: model,
|
2020-04-15 14:09:36 -04:00
|
|
|
mount_point: mounted_as
|
2018-01-29 12:57:34 -05:00
|
|
|
)
|
|
|
|
end
|
2017-05-29 03:54:35 -04:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
# Before removing an attachment, destroy any Upload records at the same path
|
|
|
|
#
|
|
|
|
# Called `before :remove`
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-29 12:57:34 -05:00
|
|
|
def destroy_upload(*args)
|
|
|
|
return unless file && file.exists?
|
2017-02-15 13:11:44 -05:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
self.upload = nil
|
|
|
|
uploads.where(path: upload_path).delete_all
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-15 13:11:44 -05:00
|
|
|
end
|
|
|
|
end
|