Project

General

Profile

Actions

Feature #8171

closed

Adding attachments through the REST API

Added by Robert Gstöhl over 13 years ago. Updated over 12 years ago.

Status:
Closed
Priority:
Normal
Category:
REST API
Target version:
Start date:
2011-04-17
Due date:
% Done:

0%

Estimated time:
Resolution:
Fixed

Description

This seems to be requested often:

http://www.redmine.org/issues/4968
https://www.chiliproject.org/issues/166

Currently people are working around this limitation in various ways (adding attachments by mail, constructing multipart form requests, ...).

I just started learning rails and the redmine internals, but wouldn't it be possible to just pass the attachment as base64 encoded string inside the issue, -json / xml?

On the issue controller side, something like this might work (it did work for me while testing things out with curl):

diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index 99e461d..e358785 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -144,6 +144,18 @@ class Attachment < ActiveRecord::Base
     if attachments && attachments.is_a?(Hash)
       attachments.each_value do |attachment|
         file = attachment['file']
+        if file.is_a?(Hash)
+          #Looks like we got a base64 encoded file from the api
+          require "base64" 
+          require "tempfile" 
+          base64encodedData = file["data"]
+          tempfile = Tempfile.new("redmine_rest_api_attachment")
+          tempfile.content_type = file["content_type"]
+          tempfile.original_filename = file["original_filename"]
+          tempfile.write(Base64.decode64(file["data"]))
+          tempfile.rewind
+          file = tempfile
+        end
         next unless file && file.size > 0
         a = Attachment.create(:container => obj, 
                               :file => file,
diff --git a/config/initializers/tempfile_for_attachments.rb b/config/initializers/tempfile_for_attachments.rb
new file mode 100644
index 0000000..779140e
--- /dev/null
+++ b/config/initializers/tempfile_for_attachments.rb
@@ -0,0 +1,4 @@
+require 'tempfile'
+class Tempfile
+   attr_accessor :content_type, :original_filename
+end

Not pretty but kind of works. If someone could help me out a bit, I guess we could implement something much cleaner pretty quickly.

Bonus points if the thing works out of the box with active resource ;)


Related issues

Related to Redmine - Feature #4968: REST API new featuresClosed2010-03-02

Actions
Actions

Also available in: Atom PDF