summaryrefslogtreecommitdiff
path: root/app/controllers/concerns/snippets_actions.rb
blob: bb46345d362ff996835a16bc364e74d955df0506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module SnippetsActions
  extend ActiveSupport::Concern

  def edit
  end

  def raw
    disposition = params[:inline] == 'false' ? 'attachment' : 'inline'

    workhorse_set_content_type!

    send_data(
      convert_line_endings(blob.data),
      type: 'text/plain; charset=utf-8',
      disposition: disposition,
      filename: Snippet.sanitized_file_name(blob.name)
    )
  end

  def js_request?
    request.format.js?
  end

  private

  def convert_line_endings(content)
    params[:line_ending] == 'raw' ? content : content.gsub(/\r\n/, "\n")
  end
end