Skip to content

Commit

Permalink
Merge "Add download service"
Browse files Browse the repository at this point in the history
  • Loading branch information
alighm authored and Gerrit Code Review committed Jan 22, 2013
2 parents 385eb67 + 0366435 commit ac562d0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/cloud_controller/api/app_bits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ def upload(id)
HTTP::CREATED
end

def download(id)
app = find_id_and_validate_access(:read, id)

package_path = AppPackage.package_path(id)
unless File.exist?(package_path)
raise Errors::AppPackageNotFound.new(id)
end

if config[:nginx][:use_nginx]
return [200, { "X-Accel-Redirect" => "/droplets/" + "app_#{id}" }, ""]
else
return send_file package_path, :filename => File.basename("#{path}.zip")
end
end

def json_param(name)
raw = params[name]
Yajl::Parser.parse(raw)
Expand All @@ -56,5 +71,7 @@ def json_param(name)
end

put "#{path_id}/bits", :upload

get "#{path_id}/download", :download
end
end
47 changes: 47 additions & 0 deletions spec/api/app_bits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,52 @@ module VCAP::CloudController
include_examples "dev app upload", 201
end
end

describe "GET /v2/app/:id/download" do
let(:tmpdir) { Dir.mktmpdir }
let(:app_obj) { Models::App.make }
let(:app_obj_without_pkg) { Models::App.make }
let(:user) { make_user_for_space(app_obj.space) }
let(:developer) { make_developer_for_space(app_obj.space) }
let(:developer2) { make_developer_for_space(app_obj_without_pkg.space) }

before do
AppPackage.configure(config_override({
:directories => { :droplets => tmpdir }
}))

pkg_path = AppPackage.package_path(app_obj.guid)
File.open(pkg_path, "w") do |f|
f.write("A")
end
end

after do
FileUtils.rm_rf(tmpdir)
end

context "dev app download" do
it "should return 404 for an app without a package" do
get "/v2/apps/#{app_obj_without_pkg.guid}/download", {}, headers_for(developer2)
last_response.status.should == 404
end
it "should return 200 for valid packages" do
get "/v2/apps/#{app_obj.guid}/download", {}, headers_for(developer)
puts last_response.body
last_response.status.should == 200
end
it "should return 404 for non-existent apps" do
get "/v2/apps/abcd/download", {}, headers_for(developer)
last_response.status.should == 404
end
end

context "user app download" do
it "should return 403" do
get "/v2/apps/#{app_obj.guid}/download", {}, headers_for(user)
last_response.status.should == 403
end
end
end
end
end

0 comments on commit ac562d0

Please sign in to comment.