2020-04-21 11:21:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
module Helpers
|
|
|
|
module SnippetsHelpers
|
2020-07-09 08:08:56 -04:00
|
|
|
extend Grape::API::Helpers
|
|
|
|
|
|
|
|
params :raw_file_params do
|
|
|
|
requires :file_path, type: String, file_path: true, desc: 'The url encoded path to the file, e.g. lib%2Fclass%2Erb'
|
|
|
|
requires :ref, type: String, desc: 'The name of branch, tag or commit'
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
def content_for(snippet)
|
2020-04-23 14:09:46 -04:00
|
|
|
if snippet.empty_repo?
|
2020-07-09 08:08:56 -04:00
|
|
|
env['api.format'] = :txt
|
|
|
|
content_type 'text/plain'
|
|
|
|
header['Content-Disposition'] = 'attachment'
|
|
|
|
|
2020-04-23 14:09:46 -04:00
|
|
|
snippet.content
|
|
|
|
else
|
2020-04-21 11:21:10 -04:00
|
|
|
blob = snippet.blobs.first
|
2020-07-09 08:08:56 -04:00
|
|
|
|
|
|
|
send_git_blob(blob.repository, blob)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
2020-07-09 08:08:56 -04:00
|
|
|
|
|
|
|
def file_content_for(snippet)
|
|
|
|
repo = snippet.repository
|
|
|
|
commit = repo.commit(params[:ref])
|
|
|
|
not_found!('Reference') unless commit
|
|
|
|
|
|
|
|
blob = repo.blob_at(commit.sha, params[:file_path])
|
|
|
|
not_found!('File') unless blob
|
|
|
|
|
|
|
|
send_git_blob(repo, blob)
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|