lib/buildbox/uploader.rb in buildbox-0.6.beta1 vs lib/buildbox/uploader.rb in buildbox-0.6.beta2
- old
+ new
@@ -4,36 +4,55 @@
module Buildbox
class Uploader
include Celluloid
include Celluloid::Logger
- def upload(credentials, absolute_path, &block)
+ def upload(api, access_token, current_build, relative_path, absolute_path)
info "Uploading #{absolute_path}"
- action = credentials[:action]
- data = credentials[:data]
+ artifact = api.create_artifact(access_token, current_build,
+ :path => relative_path,
+ :file_size => File.size(absolute_path))
- connection = Faraday.new(:url => action[:url]) do |faraday|
+
+ upload_action = artifact[:uploader][:action]
+ form_data = artifact[:uploader][:data].to_hash.dup
+
+ connection = Faraday.new(:url => upload_action[:url]) do |faraday|
faraday.request :multipart
+ faraday.response :raise_error
+
faraday.options[:timeout] = 60
faraday.options[:open_timeout] = 60
faraday.adapter Faraday.default_adapter
end
mime_type = MIME::Types.type_for(absolute_path)[0].to_s
- file_input_key = action[:file_input]
- form_data = credentials[:data].to_hash.dup
+ file_input_key = upload_action[:file_input]
form_data[file_input_key] = Faraday::UploadIO.new(absolute_path, mime_type)
- yield("uploading")
+ api.update_artifact(access_token, current_build, artifact[:id], :state => 'uploading')
- response = connection.post(action[:path], form_data)
+ upload_exception = nil
+ response = nil
- info "Finished uploading #{File.basename(absolute_path)} with a status of #{response.status}"
+ begin
+ response = connection.post(upload_action[:path], form_data)
+ rescue => e
+ upload_exception = e
+ end
- yield("finished")
+ if upload_exception
+ error "Error uploading #{File.basename(absolute_path)} with a status of (#{upload_exception.class.name}: #{upload_exception.message})"
+ finished_state = 'error'
+ else
+ info "Finished uploading #{File.basename(absolute_path)} with a status of #{response.status}"
+ finished_state = 'finished'
+ end
+
+ api.update_artifact(access_token, current_build, artifact[:id], :state => finished_state)
end
end
end