Alfresco populary known as content management system. It has workflow structure from document creation to publishing.
Here I will explain implmentation of document upload to Alfresco using REST API methods provided by Alfresco by implementing ruby net/http libray.
Alfresco's REST API upload method is http://youralfrescoserver:port/alfresco/service/api/upload
#we need to require two libray's
require 'net/http'
require 'uri'
#we need to have separator for multipart/form-data
BOUNDARY = "AaB03x"
#Here is the initialization for String which we are going to concatinate our form data
@form_params = String.new
def encode_form_data(parameters)
parameters.each do |key, value|
@form_params << "--" + BOUNDARY + "\r\n"
@form_params << "Content-Disposition: form-data; name=\"#{key}\"\r\n" + "\r\n" + "#{value}\r\n"
end
@form_params << "--" + BOUNDARY
end
def encode_file_data(parameters)
full_file_name = 'filename.docx'
file = File.open('filename.docx', "rb") {|f| f.read }
@form_params << ("\r\nContent-Disposition: form-data; name=\"filedata\"; filename=\"#{full_file_name}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type:" + "application/msword" + "\r\n\r\n" + file + "\r\n")
@form_params << "--" + BOUNDARY + "--"
end
#Here we create net/http request object and add basic authentication details
uri = URI('http://alfserver01:8080/alfresco/service/api/upload')
req = Net::HTTP::Post.new(uri.path)
req.basic_auth 'alfrescousername', 'alfrescopasword'
#siteid, containerid and uploaddirectory are three parameter that tells Alfresco where it should save the document.
form_parameter = { 'siteid' => 'publicsite', 'containerid' => 'documentLibrary', 'uploaddirectory' => '/reports'}
#The below two methods constructs the form post to submit to Alfresco
encode_form_data(form_parameter)
encode_file_data
#Adding constructed form details and adding the headers
req.body= @form_params
req["Content-Length"] = req.body.length
req["Content-Type"] = "multipart/form-data, boundary=" + BOUNDARY
Form post is happening here and result will be json object
res = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
puts res.body
else
puts res
end

No comments:
Post a Comment