// Copyright (c) 2015 qianqiusoft.com // Licensed to You under the GNU Affero GPL v3 // See the LICENSE file at git.qianqiusoft.com/qianqiusoft/light-vocation/LICENSE // http://www.gnu.org/licenses/why-affero-gpl.en.html package utils import ( "crypto/tls" "encoding/json" "fmt" "io" "io/ioutil" "net/http" "net/url" "strings" "time" "git.qianqiusoft.com/qianqiusoft/light-apiengine/models" "git.qianqiusoft.com/qianqiusoft/light-apiengine/config" "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs" ) func PostFile(reader io.Reader) (string, string, int32, error) { urlStr := config.AppConfig.GetKey("storage") params := make(map[string]string) params["genthumb"] = "1" bytess, err := NewHttpUtil().PostFormWithReader(urlStr, params, "file", "11", reader) if err != nil { return "", "", 0, err } fmt.Println(string(bytess)) result := &models.StorageResult{} err = json.Unmarshal(bytess, result) if err != nil { return "", "", 0, err } download := fmt.Sprintf("http://%s:%d/v1/fs_file/%d", result.Ip, result.Port, result.Id) logs.Debug("下载地址:", download) return download, result.Hash, result.Len, nil } func PutFile(reader io.Reader, length int64) (string, string, int32, error) { urlStr := config.AppConfig.GetKey("storage") //beego.AppConfig.String("storage") hreq := http.Request{} fmt.Println(urlStr + "?genthumb=1") hreq.URL, _ = url.Parse(urlStr + "?genthumb=1") hreq.Method = "PUT" hreq.ContentLength = length hreq.Body = ioutil.NopCloser(reader) tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, ResponseHeaderTimeout: time.Second * 60, DisableKeepAlives: true, } client := http.Client{Transport: tr} hresp, err := client.Do(&hreq) if err != nil { return "", "", 0, err } defer hresp.Body.Close() resBody, err := ioutil.ReadAll(hresp.Body) if err != nil { return "", "", 0, err } logs.Debug("上传返回:", string(resBody)) result := &models.StorageResult{} err = json.Unmarshal(resBody, result) if err != nil { return "", "", 0, err } download := fmt.Sprintf("http://%s:%d/v1/fs_file/%d", result.Ip, result.Port, result.Id) logs.Debug("下载地址:", download) return download, result.Hash, result.Len, nil } func GetFile(urlStr string) (io.ReadCloser, error) { if strings.Index(urlStr, "fs_file/") > 0 { i := strings.LastIndex(urlStr, "/") fileid := urlStr[i+1:] storageurl := config.AppConfig.GetKey("storage") //beego.AppConfig.String("storage") urlStr = strings.TrimRight(storageurl, "/") + "/" + fileid fmt.Println(".................................................................." + urlStr) } hreq := http.Request{} hreq.URL, _ = url.Parse(urlStr) hreq.Method = "GET" tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, ResponseHeaderTimeout: time.Second * 60, DisableKeepAlives: true, } client := http.Client{Transport: tr} hresp, err := client.Do(&hreq) if err != nil { return nil, err } return hresp.Body, nil }