Skip to content
Snippets Groups Projects
Commit 01cda139 authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg
Browse files

Download raw files through the API

parent 8419f355
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -81,6 +81,42 @@ module API
end
end
 
# Get file from repository
# File content is Base64 encoded
#
# Parameters:
# file_path (required) - The path to the file. Ex. lib/class.rb
# ref (required) - The name of branch, tag or commit
#
# Example Request:
# GET /projects/:id/repository/files/raw
#
get ":id/repository/files/raw" do
authorize! :download_code, user_project
required_attributes! [:file_path, :ref]
attrs = attributes_for_keys [:file_path, :ref]
ref = attrs.delete(:ref)
file_path = attrs.delete(:file_path)
commit = user_project.commit(ref)
not_found! 'Commit' unless commit
repo = user_project.repository
blob = repo.blob_at(commit.sha, file_path)
if blob
content_type "application/octet-stream"
header['Content-Disposition'] = "attachment; filename=#{blob.name}"
env['api.format'] = :binary
blob.load_all_data!(repo)
blob.data
else
not_found! 'File'
end
end
# Create new file in repository
#
# Parameters:
Loading
Loading
Loading
Loading
@@ -39,6 +39,19 @@ describe API::API, api: true do
end
end
 
describe "GET /projects/:id/repository/files/raw" do
it "only returns the blob" do
params = {
file_path: "CONTRIBUTING.md", # big enough to hit #load_all_data!
ref: 'master',
}
get api("/projects/#{project.id}/repository/files/raw", user), params
expect(response.status).to eq(200)
expect(response.body).to match /shell_commands\.md\)\n\z/
end
end
describe "POST /projects/:id/repository/files" do
let(:valid_params) do
{
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment