From 70cd6ef092d326fff6be092eab023e8415708462 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 15 Jan 2013 02:23:02 +0100 Subject: [PATCH] Allow to download logs in plain text --- lib/travis/api/app/helpers/respond_with.rb | 2 +- lib/travis/api/app/middleware/rewrite.rb | 2 +- lib/travis/api/app/responders.rb | 1 + lib/travis/api/app/responders/plain.rb | 22 ++++++++++++++++++++++ spec/unit/endpoint/artifacts_spec.rb | 18 ++++++++++++++++++ 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 lib/travis/api/app/responders/plain.rb diff --git a/lib/travis/api/app/helpers/respond_with.rb b/lib/travis/api/app/helpers/respond_with.rb index f7f8cbff..cfb1cc44 100644 --- a/lib/travis/api/app/helpers/respond_with.rb +++ b/lib/travis/api/app/helpers/respond_with.rb @@ -29,7 +29,7 @@ class Travis::Api::App end def responders(resource, options) - [:Service, :Json, :Image, :Xml].map do |name| + [:Service, :Json, :Image, :Xml, :Plain].map do |name| Responders.const_get(name) end end diff --git a/lib/travis/api/app/middleware/rewrite.rb b/lib/travis/api/app/middleware/rewrite.rb index 2a23b89a..3215602b 100644 --- a/lib/travis/api/app/middleware/rewrite.rb +++ b/lib/travis/api/app/middleware/rewrite.rb @@ -3,7 +3,7 @@ require 'travis/api/app' class Travis::Api::App class Middleware class Rewrite < Middleware - FORMAT = %r(\.(json|xml|png)$) + FORMAT = %r(\.(json|xml|png|txt)$) V1_REPO_URL = %r(^(/[^/]+/[^/]+(?:/builds(?:/[\d]+)?|/cc)?)$) helpers :accept diff --git a/lib/travis/api/app/responders.rb b/lib/travis/api/app/responders.rb index f8994685..a81ac108 100644 --- a/lib/travis/api/app/responders.rb +++ b/lib/travis/api/app/responders.rb @@ -5,6 +5,7 @@ class Travis::Api::App autoload :Base, 'travis/api/app/responders/base' autoload :Image, 'travis/api/app/responders/image' autoload :Json, 'travis/api/app/responders/json' + autoload :Plain, 'travis/api/app/responders/plain' autoload :Service, 'travis/api/app/responders/service' autoload :Xml, 'travis/api/app/responders/xml' end diff --git a/lib/travis/api/app/responders/plain.rb b/lib/travis/api/app/responders/plain.rb new file mode 100644 index 00000000..83bbcd30 --- /dev/null +++ b/lib/travis/api/app/responders/plain.rb @@ -0,0 +1,22 @@ +module Travis::Api::App::Responders + class Plain < Base + def apply? + # make sure that we don't leak anything by processing only Artifact::Log + # instances here. I don't want to create entire new API builder just + # for log's content for now. + # + # TODO: think how to handle other formats correctly + options[:format] == 'txt' && resource.is_a?(Artifact::Log) + end + + def apply + filename = resource.id + disposition = params[:attachment] ? 'attachment' : 'inline' + + headers['Content-Disposition'] = %(#{disposition}; filename="#{filename}") + + endpoint.content_type 'text/plain' + halt resource.content + end + end +end diff --git a/spec/unit/endpoint/artifacts_spec.rb b/spec/unit/endpoint/artifacts_spec.rb index e2b2c8d8..8210f7cd 100644 --- a/spec/unit/endpoint/artifacts_spec.rb +++ b/spec/unit/endpoint/artifacts_spec.rb @@ -9,4 +9,22 @@ describe Travis::Api::App::Endpoint::Artifacts do get("/artifacts/#{id}", {}, 'HTTP_ACCEPT' => 'application/vnd.travis-ci.2+json, */*; q=0.01').should be_ok end end + + describe 'GET /artifacts/:id.txt' do + it 'loads the artifact' do + response = get("/artifacts/#{id}.txt", {}) + + response.should be_ok + response.body.should == artifact.content + response.headers['Content-Disposition'].should == "inline; filename=\"#{artifact.id}\"" + end + + it 'sets Content-Disposition to attachment with attachment=true param' do + response = get("/artifacts/#{id}.txt", {'attachment' => true}) + + response.should be_ok + response.body.should == artifact.content + response.headers['Content-Disposition'].should == "attachment; filename=\"#{artifact.id}\"" + end + end end