2018-09-14 05:42:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-01 13:14:35 +00:00
|
|
|
module UploadsActions
|
2019-12-03 18:06:49 +00:00
|
|
|
extend ActiveSupport::Concern
|
2017-12-06 11:36:11 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2018-03-09 14:16:06 +00:00
|
|
|
include SendFileUpload
|
2017-12-06 11:36:11 +00:00
|
|
|
|
2017-09-26 07:54:32 +00:00
|
|
|
UPLOAD_MOUNTS = %w(avatar attachment file logo header_logo favicon).freeze
|
2018-01-29 17:57:34 +00:00
|
|
|
|
2019-12-03 18:06:49 +00:00
|
|
|
included do
|
|
|
|
prepend_before_action :set_request_format_from_path_extension
|
|
|
|
end
|
|
|
|
|
2017-05-01 13:14:35 +00:00
|
|
|
def create
|
2019-01-03 05:17:07 +00:00
|
|
|
uploader = UploadService.new(model, params[:file], uploader_class).execute
|
2017-05-01 13:14:35 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2019-01-03 05:17:07 +00:00
|
|
|
if uploader
|
2017-05-01 13:14:35 +00:00
|
|
|
format.json do
|
2019-01-03 05:17:07 +00:00
|
|
|
render json: { link: uploader.to_h }
|
2017-05-01 13:14:35 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json do
|
2019-03-27 16:52:52 +00:00
|
|
|
render json: _('Invalid file.'), status: :unprocessable_entity
|
2017-05-01 13:14:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
# This should either
|
|
|
|
# - send the file directly
|
|
|
|
# - or redirect to its URL
|
|
|
|
#
|
2017-05-01 13:14:35 +00:00
|
|
|
def show
|
2018-02-21 16:09:30 +00:00
|
|
|
return render_404 unless uploader&.exists?
|
2017-05-01 13:14:35 +00:00
|
|
|
|
2019-10-14 15:06:07 +00:00
|
|
|
# We need to reset caching from the applications controller to get rid of the no-store value
|
|
|
|
headers['Cache-Control'] = ''
|
|
|
|
headers['Pragma'] = ''
|
|
|
|
|
|
|
|
ttl, directives = *cache_settings
|
2019-11-01 15:06:21 +00:00
|
|
|
ttl ||= 0
|
2019-10-14 15:06:07 +00:00
|
|
|
directives ||= { private: true, must_revalidate: true }
|
|
|
|
|
|
|
|
expires_in ttl, directives
|
2017-05-01 13:14:35 +00:00
|
|
|
|
2019-10-09 12:06:13 +00:00
|
|
|
disposition = uploader.embeddable? ? 'inline' : 'attachment'
|
2018-03-09 14:16:06 +00:00
|
|
|
|
2018-04-13 19:29:01 +00:00
|
|
|
uploaders = [uploader, *uploader.versions.values]
|
|
|
|
uploader = uploaders.find { |version| version.filename == params[:filename] }
|
2018-04-12 12:11:21 +00:00
|
|
|
|
2018-04-13 19:29:01 +00:00
|
|
|
return render_404 unless uploader
|
2018-04-12 12:11:21 +00:00
|
|
|
|
2018-12-06 21:22:39 +00:00
|
|
|
workhorse_set_content_type!
|
2018-04-13 19:29:01 +00:00
|
|
|
send_upload(uploader, attachment: uploader.filename, disposition: disposition)
|
2017-05-01 13:14:35 +00:00
|
|
|
end
|
2017-12-06 11:36:11 +00:00
|
|
|
|
2018-06-12 15:54:37 +00: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 13:24:06 +00:00
|
|
|
rescue SocketError
|
2019-03-27 16:52:52 +00:00
|
|
|
render json: _("Error uploading file"), status: :internal_server_error
|
2018-06-12 15:54:37 +00:00
|
|
|
end
|
|
|
|
|
2017-12-06 11:36:11 +00:00
|
|
|
private
|
|
|
|
|
2019-12-03 18:06:49 +00:00
|
|
|
# Based on ActionDispatch::Http::MimeNegotiation. We have an
|
|
|
|
# initializer that monkey-patches this method out (so that repository
|
|
|
|
# paths don't guess a format based on extension), but we do want this
|
|
|
|
# behavior when serving uploads.
|
|
|
|
def set_request_format_from_path_extension
|
|
|
|
path = request.headers['action_dispatch.original_path'] || request.headers['PATH_INFO']
|
|
|
|
|
|
|
|
if match = path&.match(/\.(\w+)\z/)
|
|
|
|
format = Mime[match.captures.first]
|
|
|
|
|
|
|
|
request.format = format.symbol if format
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-29 17:57:34 +00: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 11:36:11 +00:00
|
|
|
def uploader
|
|
|
|
strong_memoize(:uploader) do
|
2018-01-29 17:57:34 +00: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 11:36:11 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-29 17:57:34 +00:00
|
|
|
def build_uploader_from_upload
|
2018-03-02 15:41:40 +00:00
|
|
|
return unless uploader = build_uploader
|
2017-12-06 11:36:11 +00:00
|
|
|
|
2018-03-02 15:41:40 +00:00
|
|
|
upload_paths = uploader.upload_paths(params[:filename])
|
2019-07-11 04:48:50 +00:00
|
|
|
upload = Upload.find_by(model: model, uploader: uploader_class.to_s, path: upload_paths)
|
2019-10-03 00:05:59 +00:00
|
|
|
upload&.retrieve_uploader
|
2018-01-29 17:57:34 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-01-29 17:57:34 +00:00
|
|
|
|
|
|
|
def build_uploader_from_params
|
2018-03-02 15:41:40 +00:00
|
|
|
return unless uploader = build_uploader
|
|
|
|
|
|
|
|
uploader.retrieve_from_store!(params[:filename])
|
|
|
|
uploader
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_uploader
|
|
|
|
return unless params[:secret] && params[:filename]
|
|
|
|
|
2018-01-29 21:06:17 +00:00
|
|
|
uploader = uploader_class.new(model, secret: params[:secret])
|
2018-02-21 16:09:30 +00:00
|
|
|
|
2018-03-02 15:41:40 +00:00
|
|
|
return unless uploader.model_valid?
|
2018-02-21 16:09:30 +00:00
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
uploader
|
2017-12-06 11:36:11 +00:00
|
|
|
end
|
|
|
|
|
2019-10-09 12:06:13 +00:00
|
|
|
def embeddable?
|
|
|
|
uploader && uploader.exists? && uploader.embeddable?
|
2017-12-06 11:36:11 +00:00
|
|
|
end
|
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
def find_model
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2019-10-14 15:06:07 +00:00
|
|
|
def cache_settings
|
|
|
|
[]
|
2019-01-21 21:25:54 +00:00
|
|
|
end
|
|
|
|
|
2018-01-29 17:57:34 +00:00
|
|
|
def model
|
|
|
|
strong_memoize(:model) { find_model }
|
2017-12-06 11:36:11 +00:00
|
|
|
end
|
2019-07-09 18:51:42 +00:00
|
|
|
|
|
|
|
def workhorse_authorize_request?
|
|
|
|
action_name == 'authorize'
|
|
|
|
end
|
2017-05-01 13:14:35 +00:00
|
|
|
end
|