2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module SnippetsActions
|
|
|
|
extend ActiveSupport::Concern
|
2020-04-22 19:07:51 +05:30
|
|
|
include SendsBlob
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_action :redirect_if_binary, only: [:edit, :update]
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def edit
|
2020-04-22 19:07:51 +05:30
|
|
|
# We need to load some info from the existing blob
|
|
|
|
snippet.content = blob.data
|
|
|
|
snippet.file_name = blob.path
|
|
|
|
|
|
|
|
render 'edit'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def raw
|
2019-02-15 15:39:39 +05:30
|
|
|
workhorse_set_content_type!
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
# Until we don't migrate all snippets to version
|
|
|
|
# snippets we need to support old `SnippetBlob`
|
|
|
|
# blobs
|
|
|
|
if defined?(blob.snippet)
|
|
|
|
send_data(
|
|
|
|
convert_line_endings(blob.data),
|
|
|
|
type: 'text/plain; charset=utf-8',
|
|
|
|
disposition: content_disposition,
|
|
|
|
filename: Snippet.sanitized_file_name(blob.name)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
send_blob(
|
|
|
|
snippet.repository,
|
|
|
|
blob,
|
|
|
|
inline: content_disposition == 'inline',
|
|
|
|
allow_caching: snippet.public?
|
|
|
|
)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def js_request?
|
|
|
|
request.format.js?
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def content_disposition
|
|
|
|
@disposition ||= params[:inline] == 'false' ? 'attachment' : 'inline'
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
def blob
|
|
|
|
return unless snippet
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
@blob ||= if snippet.empty_repo?
|
2020-04-22 19:07:51 +05:30
|
|
|
snippet.blob
|
2020-05-24 23:13:21 +05:30
|
|
|
else
|
|
|
|
snippet.blobs.first
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def convert_line_endings(content)
|
|
|
|
params[:line_ending] == 'raw' ? content : content.gsub(/\r\n/, "\n")
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def handle_repository_error(action)
|
|
|
|
errors = Array(snippet.errors.delete(:repository))
|
|
|
|
|
|
|
|
flash.now[:alert] = errors.first if errors.present?
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
recaptcha_check_with_fallback(errors.empty?) { render action }
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
def redirect_if_binary
|
|
|
|
redirect_to gitlab_snippet_path(snippet) if blob&.binary?
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|