debian-mirror-gitlab/app/controllers/concerns/uploads_actions.rb

99 lines
2.2 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
module UploadsActions
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
UPLOAD_MOUNTS = %w(avatar attachment file logo header_logo).freeze
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'
send_upload(uploader, attachment: uploader.filename, disposition: disposition)
2018-03-17 18:26:18 +05:30
end
private
2017-08-17 22:00:37 +05:30
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