gitlab-org--gitlab-foss/app/controllers/concerns/uploads_actions.rb

131 lines
3.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module UploadsActions
2017-12-06 06:36:11 -05:00
include Gitlab::Utils::StrongMemoize
include SendFileUpload
2017-12-06 06:36:11 -05:00
UPLOAD_MOUNTS = %w(avatar attachment file logo header_logo favicon).freeze
2018-01-29 12:57:34 -05:00
def create
uploader = UploadService.new(model, params[:file], uploader_class).execute
respond_to do |format|
if uploader
format.json do
render json: { link: uploader.to_h }
end
else
format.json do
render json: 'Invalid file.', status: :unprocessable_entity
end
end
end
end
2018-01-29 12:57:34 -05:00
# This should either
# - send the file directly
# - or redirect to its URL
#
def show
return render_404 unless uploader&.exists?
if cache_publicly?
# We need to reset caching from the applications controller to get rid of the no-store value
headers['Cache-Control'] = ''
expires_in 5.minutes, public: true, must_revalidate: false
else
expires_in 0.seconds, must_revalidate: true, private: true
end
disposition = uploader.image_or_video? ? 'inline' : 'attachment'
2018-04-13 15:29:01 -04:00
uploaders = [uploader, *uploader.versions.values]
uploader = uploaders.find { |version| version.filename == params[:filename] }
2018-04-13 15:29:01 -04:00
return render_404 unless uploader
workhorse_set_content_type!
2018-04-13 15:29:01 -04:00
send_upload(uploader, attachment: uploader.filename, disposition: disposition)
end
2017-12-06 06:36:11 -05:00
def authorize
set_workhorse_internal_api_content_type
authorized = uploader_class.workhorse_authorize(
has_length: false,
maximum_size: Gitlab::CurrentSettings.max_attachment_size.megabytes.to_i)
render json: authorized
2018-09-05 09:24:06 -04:00
rescue SocketError
render json: "Error uploading file", status: :internal_server_error
end
2017-12-06 06:36:11 -05:00
private
2018-01-29 12:57:34 -05:00
def uploader_class
raise NotImplementedError
end
def upload_mount
mounted_as = params[:mounted_as]
mounted_as if UPLOAD_MOUNTS.include?(mounted_as)
end
def uploader_mounted?
upload_model_class < CarrierWave::Mount::Extension && !upload_mount.nil?
end
2017-12-06 06:36:11 -05:00
def uploader
strong_memoize(:uploader) do
2018-01-29 12:57:34 -05:00
if uploader_mounted?
model.public_send(upload_mount) # rubocop:disable GitlabSecurity/PublicSend
else
build_uploader_from_upload || build_uploader_from_params
end
end
end
2017-12-06 06:36:11 -05:00
# rubocop: disable CodeReuse/ActiveRecord
2018-01-29 12:57:34 -05:00
def build_uploader_from_upload
return unless uploader = build_uploader
2017-12-06 06:36:11 -05:00
upload_paths = uploader.upload_paths(params[:filename])
upload = Upload.find_by(uploader: uploader_class.to_s, path: upload_paths)
2018-01-29 12:57:34 -05:00
upload&.build_uploader
end
# rubocop: enable CodeReuse/ActiveRecord
2018-01-29 12:57:34 -05:00
def build_uploader_from_params
return unless uploader = build_uploader
uploader.retrieve_from_store!(params[:filename])
uploader
end
def build_uploader
return unless params[:secret] && params[:filename]
uploader = uploader_class.new(model, secret: params[:secret])
return unless uploader.model_valid?
2018-01-29 12:57:34 -05:00
uploader
2017-12-06 06:36:11 -05:00
end
def image_or_video?
uploader && uploader.exists? && uploader.image_or_video?
end
2018-01-29 12:57:34 -05:00
def find_model
nil
end
def cache_publicly?
false
end
2018-01-29 12:57:34 -05:00
def model
strong_memoize(:model) { find_model }
2017-12-06 06:36:11 -05:00
end
end