aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/api_controller.rb
diff options
context:
space:
mode:
authorRobin Houston <robin.houston@gmail.com>2012-05-28 17:00:34 +0100
committerRobin Houston <robin.houston@gmail.com>2012-06-06 19:35:12 +0100
commite19238dd262e3254c0cd269f2f3b28f3cee0fd09 (patch)
treecfcd6592f4f0742aa3dccbb2f90ec6b1055c14ac /app/controllers/api_controller.rb
parent198083920f4cbcf50e61eab9e3d74edda1405e22 (diff)
WIP attachment uploading via API
Diffstat (limited to 'app/controllers/api_controller.rb')
-rw-r--r--app/controllers/api_controller.rb22
1 files changed, 21 insertions, 1 deletions
diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb
index 2c7f9a4a9..5661819b9 100644
--- a/app/controllers/api_controller.rb
+++ b/app/controllers/api_controller.rb
@@ -74,6 +74,7 @@ class ApiController < ApplicationController
def add_correspondence
request = InfoRequest.find(params[:id])
json = ActiveSupport::JSON.decode(params[:correspondence_json])
+ attachments = params[:attachments]
direction = json["direction"]
body = json["body"]
@@ -105,8 +106,12 @@ class ApiController < ApplicationController
errors << "Failed to parse 'sent_at' field as ISO8601 time: #{sent_at_str}"
end
+ if direction == "request" && !attachments.nil?
+ errors << "You cannot attach files to messages in the 'request' direction"
+ end
+
if !errors.empty?
- render :json => { "errors" => errors }
+ render :json => { "errors" => errors }, :status => 500
return
end
@@ -144,6 +149,21 @@ class ApiController < ApplicationController
request.incoming_messages << incoming_message
request.save!
+
+ attachments.each_with_index do |attachment, i|
+ filename = File.basename(attachment.original_filename)
+ body = attachment.read
+ content_type = AlaveteliFileTypes.filename_and_content_to_mimetype(filename, body) || 'application/octet-stream'
+
+ a = FoiAttachment.new(
+ :incoming_message_id => incoming_message.id,
+ :filename => filename,
+ :content_type => content_type
+ )
+ a.body = body
+ a.save!
+ end
+
request.log_event("response",
:api => true,
:email => nil,