2017-08-17 22:00:37 +05:30
|
|
|
module UploadsActions
|
2018-11-08 19:23:39 +05:30
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2018-05-09 12:01:36 +05:30
|
|
|
include SendFileUpload
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
UPLOAD_MOUNTS = %w(avatar attachment file logo header_logo favicon).freeze
|
|
|
|
|
|
|
|
included do
|
|
|
|
prepend_before_action :set_html_format, only: :show
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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-03-17 18:26:18 +05:30
|
|
|
# This should either
|
|
|
|
# - send the file directly
|
|
|
|
# - or redirect to its URL
|
|
|
|
#
|
2017-08-17 22:00:37 +05:30
|
|
|
def show
|
2018-03-17 18:26:18 +05:30
|
|
|
return render_404 unless uploader&.exists?
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
expires_in 0.seconds, must_revalidate: true, private: true
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
disposition = uploader.image_or_video? ? 'inline' : 'attachment'
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
uploaders = [uploader, *uploader.versions.values]
|
|
|
|
uploader = uploaders.find { |version| version.filename == params[:filename] }
|
|
|
|
|
|
|
|
return render_404 unless uploader
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
send_upload(uploader, attachment: uploader.filename, disposition: disposition)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
private
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
# Explicitly set the format.
|
|
|
|
# Otherwise rails 5 will set it from a file extension.
|
|
|
|
# See https://github.com/rails/rails/commit/84e8accd6fb83031e4c27e44925d7596655285f7#diff-2b8f2fbb113b55ca8e16001c393da8f1
|
|
|
|
def set_html_format
|
|
|
|
request.format = :html
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def uploader_class
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_mount
|
|
|
|
mounted_as = params[:mounted_as]
|
|
|
|
mounted_as if UPLOAD_MOUNTS.include?(mounted_as)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def uploader_mounted?
|
|
|
|
upload_model_class < CarrierWave::Mount::Extension && !upload_mount.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def uploader
|
|
|
|
strong_memoize(:uploader) do
|
|
|
|
if uploader_mounted?
|
|
|
|
model.public_send(upload_mount) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
else
|
|
|
|
build_uploader_from_upload || build_uploader_from_params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_uploader_from_upload
|
2018-05-09 12:01:36 +05:30
|
|
|
return unless uploader = build_uploader
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
upload_paths = uploader.upload_paths(params[:filename])
|
|
|
|
upload = Upload.find_by(uploader: uploader_class.to_s, path: upload_paths)
|
2018-03-17 18:26:18 +05:30
|
|
|
upload&.build_uploader
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_uploader_from_params
|
2018-05-09 12:01:36 +05:30
|
|
|
return unless uploader = build_uploader
|
|
|
|
|
|
|
|
uploader.retrieve_from_store!(params[:filename])
|
|
|
|
uploader
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_uploader
|
|
|
|
return unless params[:secret] && params[:filename]
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
uploader = uploader_class.new(model, secret: params[:secret])
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
return unless uploader.model_valid?
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
uploader
|
|
|
|
end
|
|
|
|
|
|
|
|
def image_or_video?
|
|
|
|
uploader && uploader.exists? && uploader.image_or_video?
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_model
|
|
|
|
nil
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def model
|
|
|
|
strong_memoize(:model) { find_model }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|