|
|
|
|
|
|
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"log" |
|
|
|
|
|
"os" |
|
|
"os" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type crmFileInfo struct { |
|
|
|
|
|
Name string `json:"name"` |
|
|
|
|
|
Type string `json:"type"` |
|
|
|
|
|
Role string `json:"role"` |
|
|
|
|
|
Size int64 `json:"size"` |
|
|
|
|
|
|
|
|
type crmdFileInfo struct { |
|
|
|
|
|
Name string `json:"name,omitempty"` |
|
|
|
|
|
Type string `json:"type,omitempty"` |
|
|
|
|
|
Role string `json:"role,omitempty"` |
|
|
|
|
|
Size int64 `json:"size,omitempty"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
type attachmentID struct { |
|
|
|
|
|
|
|
|
type crmdAttachmentID struct { |
|
|
ID string `json:"attachmentId"` |
|
|
ID string `json:"attachmentId"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func crmUploadFile(path string) (fileID string, fileInfo crmFileInfo, err error) { |
|
|
|
|
|
|
|
|
type crmdAttachment struct { |
|
|
|
|
|
ID string `json:"id"` //"id": "591e55398345683ee", |
|
|
|
|
|
Name string `json:"name"` //"name": "static_qr_code_without_logo.png", |
|
|
|
|
|
Deleted bool `json:"deleted"` //"deleted": false, |
|
|
|
|
|
Type string `json:"type"` //"type": "image/png", |
|
|
|
|
|
Size int `json:"size"` //"size": 509, |
|
|
|
|
|
SourceID string `json:"sourceId"` //"sourceId": null, |
|
|
|
|
|
CreateAT string `json:"createAt"` //"createdAt": "2017-05-19 02:15:21", |
|
|
|
|
|
Role string `json:"role"` //"role": "Attachment", |
|
|
|
|
|
Storage string `json:"storage"` //"storage": null, |
|
|
|
|
|
StorageFielPath string `json:"storageFilePath"` //"storageFilePath": null, |
|
|
|
|
|
Global bool `json:"global"` // "global": false, |
|
|
|
|
|
ParentID string `json:"parentId"` // "parentId": null, |
|
|
|
|
|
ParentType string `json:"parentType"` // "parentType": null, |
|
|
|
|
|
RelatedID string `json:"relatedId"` // "relatedId": null, |
|
|
|
|
|
RelatedType string `json:"relatedType"` // "relatedType": null, |
|
|
|
|
|
CreatedByID string `json:"createdById"` // "createdById": "1", |
|
|
|
|
|
CreatedbyName string `json:"createdByName"` // "createdByName": "Admin" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func crmUploadFile(path string) (fileID string, fileInfo crmdFileInfo, err error) { |
|
|
|
|
|
|
|
|
fileInfo, err = getFileInfo4CRM(path) |
|
|
fileInfo, err = getFileInfo4CRM(path) |
|
|
headers, err := crmPrepareAttachmentHTTPHeader(fileInfo) |
|
|
headers, err := crmPrepareAttachmentHTTPHeader(fileInfo) |
|
|
|
|
|
|
|
|
url := crmUploadAttachmentURL() |
|
|
url := crmUploadAttachmentURL() |
|
|
resp, err := postRAW([]byte(data), url, headers) |
|
|
resp, err := postRAW([]byte(data), url, headers) |
|
|
fileID, err = crmFileIDFromJSON(resp) |
|
|
fileID, err = crmFileIDFromJSON(resp) |
|
|
log.Println(fileID) |
|
|
|
|
|
log.Println(err) |
|
|
|
|
|
|
|
|
// log.Println(fileID) |
|
|
|
|
|
// log.Println(err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// specifically used for one purpose only. |
|
|
// specifically used for one purpose only. |
|
|
// i.e. upload attachment to EspoCRM |
|
|
// i.e. upload attachment to EspoCRM |
|
|
func crmFileIDFromJSON(jsonStr string) (fileID string, err error) { |
|
|
func crmFileIDFromJSON(jsonStr string) (fileID string, err error) { |
|
|
attach := attachmentID{} |
|
|
|
|
|
|
|
|
attach := crmdAttachmentID{} |
|
|
err = json.Unmarshal([]byte(jsonStr), &attach) |
|
|
err = json.Unmarshal([]byte(jsonStr), &attach) |
|
|
return attach.ID, err |
|
|
return attach.ID, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//crmPrepareAttachmentHTTPHeader when uploading a file, we need its mime, auth header, etc. |
|
|
//crmPrepareAttachmentHTTPHeader when uploading a file, we need its mime, auth header, etc. |
|
|
func crmPrepareAttachmentHTTPHeader(fileInfo crmFileInfo) (headers map[string]string, err error) { |
|
|
|
|
|
|
|
|
func crmPrepareAttachmentHTTPHeader(fileInfo crmdFileInfo) (headers map[string]string, err error) { |
|
|
headers = map[string]string{} |
|
|
headers = map[string]string{} |
|
|
headers["Authorization"] = crmAuthHeader() |
|
|
headers["Authorization"] = crmAuthHeader() |
|
|
headers["Accept"] = "application/json" |
|
|
headers["Accept"] = "application/json" |
|
|
|
|
|
|
|
|
//"data:image/png;base64,iVB....=" |
|
|
//"data:image/png;base64,iVB....=" |
|
|
// |
|
|
// |
|
|
//TODO: for big files, this might ave issue cause out of memory |
|
|
//TODO: for big files, this might ave issue cause out of memory |
|
|
func crmFileDataString(path string, fileInfo crmFileInfo) (encoded string, err error) { |
|
|
|
|
|
|
|
|
func crmFileDataString(path string, fileInfo crmdFileInfo) (encoded string, err error) { |
|
|
content, err := ioutil.ReadFile(path) |
|
|
content, err := ioutil.ReadFile(path) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return "", err |
|
|
return "", err |
|
|
|
|
|
|
|
|
return encoded, nil |
|
|
return encoded, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func crmUploadAttachmentMeta(fileID string, fileInfo crmFileInfo) (resp string, err error) { |
|
|
|
|
|
|
|
|
func crmUploadAttachmentMeta(fileID string, fileInfo crmdFileInfo) (resp string, err error) { |
|
|
headers := map[string]string{} |
|
|
headers := map[string]string{} |
|
|
headers["Authorization"] = crmAuthHeader() |
|
|
headers["Authorization"] = crmAuthHeader() |
|
|
headers["Accept"] = "application/json, text/javascript, */*; q=0.01" |
|
|
headers["Accept"] = "application/json, text/javascript, */*; q=0.01" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fileInfo.Role = "Attachment" |
|
|
fileInfo.Role = "Attachment" |
|
|
jb, _ := json.Marshal(fileInfo) |
|
|
jb, _ := json.Marshal(fileInfo) |
|
|
log.Println(string(jb)) |
|
|
|
|
|
|
|
|
//log.Println(string(jb)) |
|
|
|
|
|
|
|
|
URL := crmAttachmentMetaURL(fileID) |
|
|
URL := crmAttachmentMetaURL(fileID) |
|
|
resp, err = putRAW(jb, URL, headers) |
|
|
resp, err = putRAW(jb, URL, headers) |
|
|
|
|
|
|
|
|
return CRMConfig.apiEntityURL("Attachment", fileID) |
|
|
return CRMConfig.apiEntityURL("Attachment", fileID) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
type attachmentInfo struct { |
|
|
|
|
|
ID string `json:"id"` //"id": "591e55398345683ee", |
|
|
|
|
|
Name string `json:"name"` //"name": "static_qr_code_without_logo.png", |
|
|
|
|
|
Deleted bool `json:"deleted"` //"deleted": false, |
|
|
|
|
|
Type string `json:"type"` // "type": "image\/png", |
|
|
|
|
|
Size int `json:"size"` //"size": 509, |
|
|
|
|
|
SourceID string `json:"sourceId"` //"sourceId": null, |
|
|
|
|
|
CreateAT string `json:"createAt"` //"createdAt": "2017-05-19 02:15:21", |
|
|
|
|
|
Role string `json:"role"` //"role": "Attachment", |
|
|
|
|
|
Storage string `json:"storage"` //"storage": null, |
|
|
|
|
|
StorageFielPath string `json:"storageFilePath"` //"storageFilePath": null, |
|
|
|
|
|
Global bool `json:"global"` // "global": false, |
|
|
|
|
|
ParentID string `json:"parentId"` // "parentId": null, |
|
|
|
|
|
ParentType string `json:"parentType"` // "parentType": null, |
|
|
|
|
|
RelatedID string `json:"relatedId"` // "relatedId": null, |
|
|
|
|
|
RelatedType string `json:"relatedType"` // "relatedType": null, |
|
|
|
|
|
CreatedByID string `json:"createdById"` // "createdById": "1", |
|
|
|
|
|
CreatedbyName string `json:"createdByName"` // "createdByName": "Admin" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func crmCreateAttachment(path string) (result attachmentInfo, err error) { |
|
|
|
|
|
|
|
|
func crmCreateAttachment(path string) (result crmdAttachment, err error) { |
|
|
fileID, fileInfo, err := crmUploadFile(path) |
|
|
fileID, fileInfo, err := crmUploadFile(path) |
|
|
aInfo, err := crmUploadAttachmentMeta(fileID, fileInfo) |
|
|
aInfo, err := crmUploadAttachmentMeta(fileID, fileInfo) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
|
|
|
|
|
|
headers := map[string]string{} |
|
|
headers := map[string]string{} |
|
|
headers["Authorization"] = crmAuthHeader() |
|
|
headers["Authorization"] = crmAuthHeader() |
|
|
f, _, err := saveURLwithHTTPHeader(u, headers) |
|
|
f, _, err := saveURLwithHTTPHeader(u, headers) |
|
|
log.Println(f) |
|
|
|
|
|
|
|
|
//log.Println(f) |
|
|
return f, err |
|
|
return f, err |
|
|
} |
|
|
} |
|
|
|
|
|
|