Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

149 lines
4.9KB

  1. package main
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. )
  9. type crmFileInfo struct {
  10. Name string `json:"name"`
  11. Type string `json:"type"`
  12. Role string `json:"role"`
  13. Size int64 `json:"size"`
  14. }
  15. type attachmentID struct {
  16. ID string `json:"attachmentId"`
  17. }
  18. func crmUploadFile(path string) (fileID string, fileInfo crmFileInfo, err error) {
  19. fileInfo, err = getFileInfo4CRM(path)
  20. headers, err := crmPrepareAttachmentHTTPHeader(fileInfo)
  21. data, err := crmFileDataString(path, fileInfo)
  22. url := crmUploadAttachmentURL()
  23. resp, err := postRAW([]byte(data), url, headers)
  24. fileID, err = crmFileIDFromJSON(resp)
  25. log.Println(fileID)
  26. log.Println(err)
  27. return
  28. }
  29. //crmFileIDFromHTTP read attachment id from http response
  30. // as http response cannot be 'rewind', this function is
  31. // specifically used for one purpose only.
  32. // i.e. upload attachment to EspoCRM
  33. func crmFileIDFromJSON(jsonStr string) (fileID string, err error) {
  34. attach := attachmentID{}
  35. err = json.Unmarshal([]byte(jsonStr), &attach)
  36. return attach.ID, err
  37. }
  38. //crmPrepareAttachmentHTTPHeader when uploading a file, we need its mime, auth header, etc.
  39. func crmPrepareAttachmentHTTPHeader(fileInfo crmFileInfo) (headers map[string]string, err error) {
  40. headers = map[string]string{}
  41. headers["Authorization"] = crmAuthHeader()
  42. headers["Accept"] = "application/json"
  43. headers["Content-Type"] = fileInfo.Type
  44. return headers, err
  45. }
  46. func crmUploadAttachmentURL() string {
  47. return CRMConfig.BaseURL + "api/v1/Attachment/action/upload"
  48. }
  49. func crmAuthHeader() string {
  50. //return "Basic cGF0cmljazpiNjFmYWRlMTM5OWYwY2ZjNmZjZjcxNTU0OTljNTNkOQ=="
  51. s := CRMConfig.UserName + ":" + CRMConfig.UserPass
  52. r := base64.StdEncoding.EncodeToString([]byte(s))
  53. return "Basic " + r
  54. }
  55. //crmFileDataString, encode a base64 string of the given file
  56. //so that the receiver EspoCRM can correctly understand and decode it.
  57. //"data:image/png;base64,iVB....="
  58. //
  59. //TODO: for big files, this might ave issue cause out of memory
  60. func crmFileDataString(path string, fileInfo crmFileInfo) (encoded string, err error) {
  61. content, err := ioutil.ReadFile(path)
  62. if err != nil {
  63. return "", err
  64. }
  65. b64content := base64.StdEncoding.EncodeToString(content)
  66. encoded = fmt.Sprintf("data:%s;base64,%s", fileInfo.Type, b64content)
  67. //encode it
  68. return encoded, nil
  69. }
  70. func crmUploadAttachmentMeta(fileID string, fileInfo crmFileInfo) (resp string, err error) {
  71. headers := map[string]string{}
  72. headers["Authorization"] = crmAuthHeader()
  73. headers["Accept"] = "application/json, text/javascript, */*; q=0.01"
  74. headers["Content-Type"] = "application/json"
  75. fileInfo.Role = "Attachment"
  76. jb, _ := json.Marshal(fileInfo)
  77. log.Println(string(jb))
  78. URL := crmAttachmentMetaURL(fileID)
  79. resp, err = putRAW(jb, URL, headers)
  80. return
  81. }
  82. func crmAttachmentMetaURL(fileID string) string {
  83. //return "https://c.hitxy.org.au/api/v1/Attachment/" + fileID
  84. return CRMConfig.BaseURL + "api/v1/Attachment/" + fileID
  85. }
  86. type attachmentInfo struct {
  87. ID string `json:"id"` //"id": "591e55398345683ee",
  88. Name string `json:"name"` //"name": "static_qr_code_without_logo.png",
  89. Deleted bool `json:"deleted"` //"deleted": false,
  90. Type string `json:"type"` // "type": "image\/png",
  91. Size int `json:"size"` //"size": 509,
  92. SourceID string `json:"sourceId"` //"sourceId": null,
  93. CreateAT string `json:"createAt"` //"createdAt": "2017-05-19 02:15:21",
  94. Role string `json:"role"` //"role": "Attachment",
  95. Storage string `json:"storage"` //"storage": null,
  96. StorageFielPath string `json:"storageFilePath"` //"storageFilePath": null,
  97. Global bool `json:"global"` // "global": false,
  98. ParentID string `json:"parentId"` // "parentId": null,
  99. ParentType string `json:"parentType"` // "parentType": null,
  100. RelatedID string `json:"relatedId"` // "relatedId": null,
  101. RelatedType string `json:"relatedType"` // "relatedType": null,
  102. CreatedByID string `json:"createdById"` // "createdById": "1",
  103. CreatedbyName string `json:"createdByName"` // "createdByName": "Admin"
  104. }
  105. func crmCreateAttachment(path string) (result attachmentInfo, err error) {
  106. fileID, fileInfo, err := crmUploadFile(path)
  107. aInfo, err := crmUploadAttachmentMeta(fileID, fileInfo)
  108. if err != nil {
  109. return
  110. }
  111. json.Unmarshal([]byte(aInfo), &result)
  112. return
  113. }
  114. func crmDownloadAttachmentAs(fileID, saveAs string) (err error) {
  115. return nil
  116. }
  117. func crmDownloadAttachment(fileID string) (err error) {
  118. u := crmURL4DownloadAttachmet(fileID)
  119. headers := map[string]string{}
  120. headers["Authorization"] = crmAuthHeader()
  121. f, _, err := saveURLwithHTTPHeader(u, headers)
  122. log.Println(f)
  123. return nil
  124. }
  125. func crmURL4DownloadAttachmet(fileID string) string {
  126. return CRMConfig.BaseURL + "?entryPoint=download&id=" + fileID
  127. }