2017-05-01 09:14:35 -04:00
|
|
|
module UploadsActions
|
2017-12-06 06:36:11 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
UPLOAD_MOUNTS = %w(avatar attachment file logo header_logo).freeze
|
|
|
|
|
2017-05-01 09:14:35 -04:00
|
|
|
def create
|
|
|
|
link_to_file = UploadService.new(model, params[:file], uploader_class).execute
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if link_to_file
|
|
|
|
format.json do
|
|
|
|
render json: { link: link_to_file }
|
|
|
|
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
|
|
|
|
#
|
2017-05-01 09:14:35 -04:00
|
|
|
def show
|
|
|
|
return render_404 unless uploader.exists?
|
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
if uploader.file_storage?
|
|
|
|
disposition = uploader.image_or_video? ? 'inline' : 'attachment'
|
|
|
|
expires_in 0.seconds, must_revalidate: true, private: true
|
2017-05-01 09:14:35 -04:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
send_file uploader.file.path, disposition: disposition
|
|
|
|
else
|
|
|
|
redirect_to uploader.url
|
|
|
|
end
|
2017-05-01 09:14:35 -04:00
|
|
|
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
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
def build_uploader_from_upload
|
|
|
|
return nil unless params[:secret] && params[:filename]
|
2017-12-06 06:36:11 -05:00
|
|
|
|
2018-01-29 12:57:34 -05:00
|
|
|
upload_path = uploader_class.upload_path(params[:secret], params[:filename])
|
|
|
|
upload = Upload.find_by(uploader: uploader_class.to_s, path: upload_path)
|
|
|
|
upload&.build_uploader
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_uploader_from_params
|
2018-01-29 16:06:17 -05:00
|
|
|
uploader = uploader_class.new(model, secret: params[:secret])
|
2018-01-29 12:57:34 -05:00
|
|
|
uploader.retrieve_from_store!(params[:filename])
|
|
|
|
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 model
|
|
|
|
strong_memoize(:model) { find_model }
|
2017-12-06 06:36:11 -05:00
|
|
|
end
|
2017-05-01 09:14:35 -04:00
|
|
|
end
|