package qiniu import ( "crypto/hmac" "crypto/sha1" "encoding/base64" "encoding/json" "io/ioutil" "math" "net/http" "service-api/global" "strconv" "github.com/pili-engineering/pili-sdk-go.v2/pili" ) // 计算token并加入Header func createAuthorization(req *http.Request, reqBody []byte) *http.Request { var ( str string hashed []byte ) str = req.Method + " " + req.URL.Path if req.URL.RawQuery != "" { str += "?" + req.URL.RawQuery } str += "\nHost: " + req.Host if contentType := req.Header.Get("Content-Type"); contentType != "" { str += "\nContent-Type: " + contentType } str += "\n\n" if len(reqBody) != 0 { str += string(reqBody) } key := []byte(global.MG_CONFIG.Qiniu.SecretKey) mac := hmac.New(sha1.New, key) mac.Write([]byte(str)) hashed = mac.Sum(nil) req.Header.Add("Authorization", "Qiniu "+global.MG_CONFIG.Qiniu.AccessKey+":"+base64.URLEncoding.EncodeToString(hashed)) return req } func getVideoInfo(url string) (result VideoInfo, err error) { resp, err := http.Get(url + "?avinfo") if err != nil { return } defer resp.Body.Close() resBody, err := ioutil.ReadAll(resp.Body) if err != nil { return } //fmt.Println(string(resBody)) global.MG_LOG.Info(string(resBody)) err = json.Unmarshal(resBody, &result) return } func GetVideoLen(url string) int { info, err := getVideoInfo(url) if err != nil { return 0 } f1, err := strconv.ParseFloat(info.Format.Duration, 64) if err != nil { return 0 } s1 := strconv.FormatFloat(math.Floor(f1), 'f', -1, 64) i, _ := strconv.Atoi(s1) return i } func ReturnUrl2(key, zone, domain string, start, end int64) (string, error) { mac := &pili.MAC{AccessKey: global.MG_CONFIG.Qiniu.AccessKey, SecretKey: []byte(global.MG_CONFIG.Qiniu.SecretKey)} client := pili.New(mac, nil) hub := client.Hub(zone) stream := hub.Stream(key) fName, err := stream.Save(start, end) if err != nil { return "", err } return "http://" + domain + "/" + fName, nil }