You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 satır
4.9KB

  1. package main
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. )
  10. type crmFileInfo struct {
  11. Name string `json:"name"`
  12. Type string `json:"type"`
  13. Role string `json:"role"`
  14. Size int64 `json:"size"`
  15. }
  16. type attachmentID struct {
  17. ID string `json:"attachmentId"`
  18. }
  19. func crmUploadFile(path string) (fileID string, fileInfo crmFileInfo, err error) {
  20. fileInfo, err = getFileInfo4CRM(path)
  21. headers, err := crmPrepareAttachmentHTTPHeader(fileInfo)
  22. data, err := crmFileDataString(path, fileInfo)
  23. url := crmUploadAttachmentURL()
  24. resp, err := postRAW([]byte(data), url, headers)
  25. fileID, err = crmFileIDFromJSON(resp)
  26. log.Println(fileID)
  27. log.Println(err)
  28. return
  29. }
  30. //crmFileIDFromHTTP read attachment id from http response
  31. // as http response cannot be 'rewind', this function is
  32. // specifically used for one purpose only.
  33. // i.e. upload attachment to EspoCRM
  34. func crmFileIDFromJSON(jsonStr string) (fileID string, err error) {
  35. attach := attachmentID{}
  36. err = json.Unmarshal([]byte(jsonStr), &attach)
  37. return attach.ID, err
  38. }
  39. //crmPrepareAttachmentHTTPHeader when uploading a file, we need its mime, auth header, etc.
  40. func crmPrepareAttachmentHTTPHeader(fileInfo crmFileInfo) (headers map[string]string, err error) {
  41. headers = map[string]string{}
  42. headers["Authorization"] = crmAuthHeader()
  43. headers["Accept"] = "application/json"
  44. headers["Content-Type"] = fileInfo.Type
  45. return headers, err
  46. }
  47. func crmUploadAttachmentURL() string {
  48. return CRMConfig.apiUploadAttachmentURL()
  49. }
  50. func crmAuthHeader() string {
  51. //return "Basic cGF0cmljazpiNjFmYWRlMTM5OWYwY2ZjNmZjZjcxNTU0OTljNTNkOQ=="
  52. s := CRMConfig.UserName + ":" + CRMConfig.UserPass
  53. r := base64.StdEncoding.EncodeToString([]byte(s))
  54. return "Basic " + r
  55. }
  56. //crmFileDataString, encode a base64 string of the given file
  57. //so that the receiver EspoCRM can correctly understand and decode it.
  58. //"data:image/png;base64,iVB....="
  59. //
  60. //TODO: for big files, this might ave issue cause out of memory
  61. func crmFileDataString(path string, fileInfo crmFileInfo) (encoded string, err error) {
  62. content, err := ioutil.ReadFile(path)
  63. if err != nil {
  64. return "", err
  65. }
  66. b64content := base64.StdEncoding.EncodeToString(content)
  67. encoded = fmt.Sprintf("data:%s;base64,%s", fileInfo.Type, b64content)
  68. //encode it
  69. return encoded, nil
  70. }
  71. func crmUploadAttachmentMeta(fileID string, fileInfo crmFileInfo) (resp string, err error) {
  72. headers := map[string]string{}
  73. headers["Authorization"] = crmAuthHeader()
  74. headers["Accept"] = "application/json, text/javascript, */*; q=0.01"
  75. headers["Content-Type"] = "application/json"
  76. fileInfo.Role = "Attachment"
  77. jb, _ := json.Marshal(fileInfo)
  78. log.Println(string(jb))
  79. URL := crmAttachmentMetaURL(fileID)
  80. resp, err = putRAW(jb, URL, headers)
  81. return
  82. }
  83. func crmAttachmentMetaURL(fileID string) string {
  84. return CRMConfig.apiEntityURL("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. f, err := crmDownloadAttachment(fileID)
  116. if err != nil {
  117. return
  118. }
  119. err = os.Rename(f, saveAs)
  120. return
  121. }
  122. func crmDownloadAttachment(fileID string) (filename string, err error) {
  123. u := crmURL4DownloadAttachmet(fileID)
  124. headers := map[string]string{}
  125. headers["Authorization"] = crmAuthHeader()
  126. f, _, err := saveURLwithHTTPHeader(u, headers)
  127. log.Println(f)
  128. return f, err
  129. }
  130. func crmURL4DownloadAttachmet(fileID string) string {
  131. return CRMConfig.BaseURL + "?entryPoint=download&id=" + fileID
  132. }