2018-09-14 05:42:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-06 15:04:00 +00:00
|
|
|
module SnippetsActions
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def edit
|
2020-03-20 12:10:03 +00:00
|
|
|
# We need to load some info from the existing blob
|
|
|
|
snippet.content = blob.data
|
|
|
|
snippet.file_name = blob.path
|
|
|
|
|
|
|
|
render 'edit'
|
2017-02-06 15:04:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def raw
|
2017-04-30 17:15:20 +00:00
|
|
|
disposition = params[:inline] == 'false' ? 'attachment' : 'inline'
|
|
|
|
|
2018-12-06 21:22:39 +00:00
|
|
|
workhorse_set_content_type!
|
|
|
|
|
2017-02-06 15:04:00 +00:00
|
|
|
send_data(
|
2020-02-25 09:09:10 +00:00
|
|
|
convert_line_endings(blob.data),
|
2017-02-06 15:04:00 +00:00
|
|
|
type: 'text/plain; charset=utf-8',
|
2017-04-30 17:15:20 +00:00
|
|
|
disposition: disposition,
|
2020-02-25 09:09:10 +00:00
|
|
|
filename: Snippet.sanitized_file_name(blob.name)
|
2017-02-06 15:04:00 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-02-06 13:33:18 +00:00
|
|
|
def js_request?
|
|
|
|
request.format.js?
|
|
|
|
end
|
|
|
|
|
2017-02-06 15:04:00 +00:00
|
|
|
private
|
|
|
|
|
2020-03-20 12:10:03 +00:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
def blob
|
|
|
|
return unless snippet
|
|
|
|
|
|
|
|
@blob ||= if Feature.enabled?(:version_snippets, current_user) && !snippet.repository.empty?
|
|
|
|
snippet.blobs.first
|
|
|
|
else
|
|
|
|
snippet.blob
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
2017-02-06 15:04:00 +00:00
|
|
|
def convert_line_endings(content)
|
|
|
|
params[:line_ending] == 'raw' ? content : content.gsub(/\r\n/, "\n")
|
|
|
|
end
|
2020-03-09 12:07:45 +00:00
|
|
|
|
|
|
|
def check_repository_error
|
2020-03-12 15:09:39 +00:00
|
|
|
repository_errors = Array(snippet.errors.delete(:repository))
|
2020-03-09 12:07:45 +00:00
|
|
|
|
2020-03-12 15:09:39 +00:00
|
|
|
flash.now[:alert] = repository_errors.first if repository_errors.present?
|
|
|
|
recaptcha_check_with_fallback(repository_errors.empty?) { render :edit }
|
2020-03-09 12:07:45 +00:00
|
|
|
end
|
2017-02-06 15:04:00 +00:00
|
|
|
end
|