file_service.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "mime/multipart"
  10. "net/http"
  11. "strings"
  12. )
  13. type FileService struct{}
  14. type innerFileMeta struct {
  15. Count int `json:"count"`
  16. Fid string `json:"fid"`
  17. Url string `json:"url"`
  18. PublicUrl string `json:"publicUrl"`
  19. }
  20. const FILE_SERVER_URL = "http://localhost:9333"
  21. func (fsrv *FileService) Upload(fileReader io.Reader) (interface{}, error) {
  22. res, err := http.Post(FILE_SERVER_URL+"/dir/assign", "application/x-www-form-urlencoded", nil)
  23. if err != nil {
  24. return "", err
  25. }
  26. defer res.Body.Close()
  27. buf, err := ioutil.ReadAll(res.Body)
  28. if err != nil {
  29. return "", err
  30. }
  31. var metaf = &innerFileMeta{}
  32. err = json.Unmarshal(buf, metaf)
  33. if err != nil {
  34. return "", err
  35. }
  36. var purl = "http://" + metaf.PublicUrl + "/" + metaf.Fid
  37. request, err := newfileUploadRequest(purl, nil, "file", "test.jpg", fileReader)
  38. //rbytes, _ := httputil.DumpRequest(request, true)
  39. if err != nil {
  40. return "", err
  41. }
  42. client := &http.Client{}
  43. resp, err := client.Do(request)
  44. if err != nil {
  45. return "", err
  46. } else {
  47. //body := &bytes.Buffer{}
  48. //_, err := body.ReadFrom(resp.Body)
  49. resp.Body.Close()
  50. return metaf, nil
  51. }
  52. }
  53. type fileLocation struct {
  54. PublicUrl string `json:"publicUrl"`
  55. Url string `json:"url"`
  56. }
  57. type fileLookupResponse struct {
  58. Locations []fileLocation `json:"locations"`
  59. }
  60. func (fsrv *FileService) GetImage(fid string) (interface{}, error) {
  61. fidParts := strings.Split(fid, ",")
  62. if len(fidParts) != 2 {
  63. return nil, errors.New("wrong file id")
  64. }
  65. resp, err := http.Get(fmt.Sprintf("%s/dir/lookup?volumeId=%s", FILE_SERVER_URL, fidParts[0]))
  66. if err != nil {
  67. return nil, err
  68. }
  69. je := json.NewDecoder(resp.Body)
  70. fr := fileLookupResponse{}
  71. err = je.Decode(&fr)
  72. resp.Body.Close()
  73. if err != nil {
  74. return nil, err
  75. }
  76. return fmt.Sprintf("http://%s/%s", fr.Locations[0].PublicUrl, fid), nil
  77. //fr.Locations[0].PublicUrl
  78. }
  79. // Creates a new file upload http request with optional extra params
  80. func newfileUploadRequest(uri string, params map[string]string, paramName, fileName string, fileReader io.Reader) (*http.Request, error) {
  81. body := &bytes.Buffer{}
  82. writer := multipart.NewWriter(body)
  83. part, err := writer.CreateFormFile(paramName, fileName)
  84. if err != nil {
  85. return nil, err
  86. }
  87. _, err = io.Copy(part, fileReader)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if params != nil {
  92. for key, val := range params {
  93. _ = writer.WriteField(key, val)
  94. }
  95. }
  96. err = writer.Close()
  97. if err != nil {
  98. return nil, err
  99. }
  100. req, err := http.NewRequest("POST", uri, body)
  101. if err != nil {
  102. return nil, err
  103. }
  104. req.Header.Set("Content-Type", writer.FormDataContentType())
  105. return req, nil
  106. }