2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-06 10:04:00 -05:00
|
|
|
module SnippetsActions
|
|
|
|
extend ActiveSupport::Concern
|
2020-06-22 08:08:47 -04:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
include RendersNotes
|
|
|
|
include RendersBlob
|
|
|
|
include PaginatedCollection
|
|
|
|
include Gitlab::NoteableMetadata
|
2020-06-22 08:08:47 -04:00
|
|
|
include Snippets::SendBlob
|
2020-07-10 14:09:45 -04:00
|
|
|
include SnippetsSort
|
2020-11-24 10:09:13 -05:00
|
|
|
include RedisTracking
|
2017-02-06 10:04:00 -05:00
|
|
|
|
2020-03-31 14:07:42 -04:00
|
|
|
included do
|
2020-06-16 11:08:32 -04:00
|
|
|
skip_before_action :verify_authenticity_token,
|
|
|
|
if: -> { action_name == 'show' && js_request? }
|
|
|
|
|
2020-12-02 16:09:44 -05:00
|
|
|
track_redis_hll_event :show, name: 'i_snippets_show', feature: :usage_data_i_snippets_show, feature_default_enabled: true
|
2020-11-24 10:09:13 -05:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
respond_to :html
|
2020-03-31 14:07:42 -04:00
|
|
|
end
|
|
|
|
|
2020-10-12 11:08:32 -04:00
|
|
|
def edit; end
|
2017-02-06 10:04:00 -05:00
|
|
|
|
2020-06-22 08:08:47 -04:00
|
|
|
# This endpoint is being replaced by Snippets::BlobController#raw
|
|
|
|
# Support for old raw links will be maintainted via this action but
|
|
|
|
# it will only return the first blob found,
|
|
|
|
# see: https://gitlab.com/gitlab-org/gitlab/-/issues/217775
|
2017-02-06 10:04:00 -05:00
|
|
|
def raw
|
2018-12-06 16:22:39 -05:00
|
|
|
workhorse_set_content_type!
|
|
|
|
|
2020-03-30 14:08:07 -04:00
|
|
|
# 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
|
2020-06-22 08:08:47 -04:00
|
|
|
send_snippet_blob(snippet, blob)
|
2020-03-30 14:08:07 -04:00
|
|
|
end
|
2017-02-06 10:04:00 -05:00
|
|
|
end
|
|
|
|
|
2018-02-06 08:33:18 -05:00
|
|
|
def js_request?
|
|
|
|
request.format.js?
|
|
|
|
end
|
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
def show
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
@note = Note.new(noteable: @snippet, project: @snippet.project)
|
|
|
|
@noteable = @snippet
|
|
|
|
|
|
|
|
@discussions = @snippet.discussions
|
|
|
|
@notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes), @noteable)
|
|
|
|
render 'show'
|
|
|
|
end
|
|
|
|
|
|
|
|
format.js do
|
|
|
|
if @snippet.embeddable?
|
2020-07-27 08:09:50 -04:00
|
|
|
conditionally_expand_blobs(blobs)
|
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
render 'shared/snippets/show'
|
|
|
|
else
|
|
|
|
head :not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
2017-02-06 10:04:00 -05:00
|
|
|
private
|
|
|
|
|
2020-03-20 08:10:03 -04:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
def blob
|
2020-07-27 08:09:50 -04:00
|
|
|
@blob ||= blobs.first
|
|
|
|
end
|
2020-03-20 08:10:03 -04:00
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
def blobs
|
|
|
|
@blobs ||= if snippet.empty_repo?
|
|
|
|
[snippet.blob]
|
|
|
|
else
|
|
|
|
snippet.blobs
|
|
|
|
end
|
2020-03-20 08:10:03 -04:00
|
|
|
end
|
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
2017-02-06 10:04:00 -05:00
|
|
|
def convert_line_endings(content)
|
|
|
|
params[:line_ending] == 'raw' ? content : content.gsub(/\r\n/, "\n")
|
|
|
|
end
|
|
|
|
end
|