diff options
Diffstat (limited to 'lib/api')
-rw-r--r-- | lib/api/files.rb | 93 |
1 files changed, 41 insertions, 52 deletions
diff --git a/lib/api/files.rb b/lib/api/files.rb index c86d4661909..309e48ad688 100644 --- a/lib/api/files.rb +++ b/lib/api/files.rb @@ -21,6 +21,43 @@ module API branch_name: attrs[:branch_name], } end + + def send_file(raw:) + required_attributes! [:file_path, :ref] + + ref = params[:ref] + file_path = params[:file_path] + + commit = user_project.commit(ref) + not_found! 'Commit' unless commit + + repository = user_project.repository + blob = repository.blob_at(commit.sha, file_path) + not_found! 'File' unless blob + + blob.load_all_data!(repository) + if raw + content_type "application/octet-stream" + header['Content-Disposition'] = "attachment; filename=#{blob.name}" + env['api.format'] = :binary + + blob.data + else + status(200) + + { + file_name: blob.name, + file_path: blob.path, + size: blob.size, + encoding: 'base64', + content: Base64.strict_encode64(blob.data), + ref: ref, + blob_id: blob.id, + commit_id: commit.id, + last_commit_id: repository.last_commit_for_path(commit.sha, file_path).id + } + end + end end resource :projects do @@ -47,38 +84,10 @@ module API # "last_commit_id": "570e7b2abdd848b95f2f578043fc23bd6f6fd24d", # } # - get ":id/repository/files" do + get ':id/repository/files' 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 - blob.load_all_data!(repo) - status(200) - - { - file_name: blob.name, - file_path: blob.path, - size: blob.size, - encoding: "base64", - content: Base64.strict_encode64(blob.data), - ref: ref, - blob_id: blob.id, - commit_id: commit.id, - last_commit_id: repo.last_commit_for_path(commit.sha, file_path).id - } - else - not_found! 'File' - end + send_file(raw: false) end # Get file from repository @@ -91,30 +100,10 @@ module API # Example Request: # GET /projects/:id/repository/files/raw # - get ":id/repository/files/raw" do + 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 + send_file(raw: true) end # Create new file in repository |