chicks / sugarcrm

A ruby based REST Client for SugarCRM
MIT License
90 stars 64 forks source link

Set_entry not working for binary Documents #41

Open davidsulc opened 13 years ago

davidsulc commented 13 years ago
excelfile = File.read(File.join(File.dirname(__FILE__),"test_excel.xls"))
exceldoc = SugarCRM::Document.new
exceldoc.active_date = Date.today
exceldoc.document_name = "test_excel.xls"
exceldoc.filename = "test_excel.xls"
exceldoc.revision = 0
exceldoc.uploadfile = Base64.encode64(excelfile)
exceldoc.save!

The call is successful, but file is not readable in SugarCRM (and isn't uploaded: cache/uploads doesn't contain the file).

Given the filename contains binary garbage instead of the provided name, it's possible the REST API is expecting parameters in a certain order, and completely ignoring the key-value concept. (I seem to recall we ran into this issue before.)

Chicks, can you check with one of the engineers in which order we should be passing the parameters to create a Document instance with the set_entry REST API call?

chicks commented 13 years ago

I honestly don't think it's possible to do this via set_entry because the base64 encoded contents of "uploadfile" get converted to UTF-8 automatically. Here is a workaround:

# Read in your file
file = File.read("/Users/chicks/Documents/Sugarcon Presentation.pptx")
# Setup the Document object
d = SugarCRM::Document.create(
  :revision     => 0,
  :active_date  => Date.today,
  :filename     => "Sugarcon Presentation.pptx", 
  :document_name=> "Sugarcon Presentation.pptx",
  :uploadfile   => "Sugarcon Presentation.pptx"
)
# Create a new document revision on that document object
SugarCRM.connection.set_document_revision(d.id, d.revision + 1, {:file => file, :file_name => "Sugarcon Presentation.pptx"})
# Remove the original document revision
SugarCRM::DocumentRevision.find_all_by_document_id_and_revision(d.id,0).first.delete!
chicks commented 13 years ago

Additionally, the "uploadfile" property is a bit ambiguous. I'm currently investigating how to utilize this property via REST.

chicks commented 13 years ago

So if : uploadfile = Base64.encode64(file) you get something like this in the log: Sun Apr 10 15:58:04 2011 [5219][1][DEBUG] Localization: translating [ UEsDBBQABgAIAAAAIQCsmJF53gEAAA0OAAATAAgCW0NvbnRlbnRfVHlwZXNd

Whereas if: :uploadfile => "Sugarcon Presentation.pptx" you get something like this Sun Apr 10 16:22:28 2011 [7447][1][DEBUG] Localization: translating [ Sugarcon Presentation.pptx ] into UTF-8

My hunch is that we need to pass a filename and the file contents to uploadfile, so something like this: :uploadfile => {:filename => "Sugarcon Presentation.pptx", :file => Base64.encode64(file)} but, I haven't gotten this working yet. I'm still digging around in the code to figure it out.

chicks commented 13 years ago

BTW, I've ruled out the parameter ordering problem (this is still an issue, but only with the REST API method arguments, not the actual values being passed.).

davidsulc commented 13 years ago

Ah, ok.

It'd be cool if we can create documents (and upload the file) in one call, but at least the work around makes it possible.

chicks commented 13 years ago

Yeah, documents are pretty bad. It might make more sense to just add some methods via the extension framework to fix this.

Also, this doesn't work... I'm investigating why: SugarCRM::Document.last.revisions

chicks commented 13 years ago

Ahhh... I just realized that I opened a bug for this awhile back. Will track in a separate issue.

davidsulc commented 13 years ago

Yeah, I finally got around to submitting bugs for the REST API. I'm probably on a dart board somewhere in the Sugar office ;-)

chicks commented 13 years ago

Hahahaha... No worries, I'm sure Jenny Gonsalves hates you now. :)

But honestly they should be thanking you - a lot of people probably bang their head on this stuff and never file a bug. I'll make sure they get assigned.