filedb.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2015 qianqiusoft.com
  2. // Licensed to You under the GNU Affero GPL v3
  3. // See the LICENSE file at git.qianqiusoft.com/qianqiusoft/light-vocation/LICENSE
  4. // http://www.gnu.org/licenses/why-affero-gpl.en.html
  5. package utils
  6. import (
  7. "crypto/tls"
  8. "encoding/json"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "net/http"
  13. "net/url"
  14. "strings"
  15. "time"
  16. "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
  17. "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
  18. "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
  19. )
  20. func PostFile(reader io.Reader) (string, string, int32, error) {
  21. urlStr := config.AppConfig.GetKey("storage")
  22. params := make(map[string]string)
  23. params["genthumb"] = "1"
  24. bytess, err := NewHttpUtil().PostFormWithReader(urlStr, params, "file", "11", reader)
  25. if err != nil {
  26. return "", "", 0, err
  27. }
  28. fmt.Println(string(bytess))
  29. result := &models.StorageResult{}
  30. err = json.Unmarshal(bytess, result)
  31. if err != nil {
  32. return "", "", 0, err
  33. }
  34. download := fmt.Sprintf("http://%s:%d/v1/fs_file/%d", result.Ip, result.Port, result.Id)
  35. logs.Debug("下载地址:", download)
  36. return download, result.Hash, result.Len, nil
  37. }
  38. func PutFile(reader io.Reader, length int64) (string, string, int32, error) {
  39. urlStr := config.AppConfig.GetKey("storage") //beego.AppConfig.String("storage")
  40. hreq := http.Request{}
  41. fmt.Println(urlStr + "?genthumb=1")
  42. hreq.URL, _ = url.Parse(urlStr + "?genthumb=1")
  43. hreq.Method = "PUT"
  44. hreq.ContentLength = length
  45. hreq.Body = ioutil.NopCloser(reader)
  46. tr := &http.Transport{
  47. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  48. ResponseHeaderTimeout: time.Second * 60,
  49. DisableKeepAlives: true,
  50. }
  51. client := http.Client{Transport: tr}
  52. hresp, err := client.Do(&hreq)
  53. if err != nil {
  54. return "", "", 0, err
  55. }
  56. defer hresp.Body.Close()
  57. resBody, err := ioutil.ReadAll(hresp.Body)
  58. if err != nil {
  59. return "", "", 0, err
  60. }
  61. logs.Debug("上传返回:", string(resBody))
  62. result := &models.StorageResult{}
  63. err = json.Unmarshal(resBody, result)
  64. if err != nil {
  65. return "", "", 0, err
  66. }
  67. download := fmt.Sprintf("http://%s:%d/v1/fs_file/%d", result.Ip, result.Port, result.Id)
  68. logs.Debug("下载地址:", download)
  69. return download, result.Hash, result.Len, nil
  70. }
  71. func GetFile(urlStr string) (io.ReadCloser, error) {
  72. if strings.Index(urlStr, "fs_file/") > 0 {
  73. i := strings.LastIndex(urlStr, "/")
  74. fileid := urlStr[i+1:]
  75. storageurl := config.AppConfig.GetKey("storage") //beego.AppConfig.String("storage")
  76. urlStr = strings.TrimRight(storageurl, "/") + "/" + fileid
  77. fmt.Println(".................................................................." + urlStr)
  78. }
  79. hreq := http.Request{}
  80. hreq.URL, _ = url.Parse(urlStr)
  81. hreq.Method = "GET"
  82. tr := &http.Transport{
  83. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  84. ResponseHeaderTimeout: time.Second * 60,
  85. DisableKeepAlives: true,
  86. }
  87. client := http.Client{Transport: tr}
  88. hresp, err := client.Do(&hreq)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return hresp.Body, nil
  93. }