sugarcrm icon indicating copy to clipboard operation
sugarcrm copied to clipboard

Set_entry not working for binary Documents

Open davidsulc opened this issue 14 years ago • 9 comments

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?

davidsulc avatar Apr 10 '11 17:04 davidsulc

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 avatar Apr 10 '11 23:04 chicks

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

chicks avatar Apr 10 '11 23:04 chicks

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 avatar Apr 10 '11 23:04 chicks

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.).

chicks avatar Apr 10 '11 23:04 chicks

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.

davidsulc avatar Apr 10 '11 23:04 davidsulc

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 avatar Apr 11 '11 00:04 chicks

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

chicks avatar Apr 11 '11 00:04 chicks

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

davidsulc avatar Apr 11 '11 00:04 davidsulc

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.

chicks avatar Apr 11 '11 00:04 chicks