-
Notifications
You must be signed in to change notification settings - Fork 7
/
nosToken.go
127 lines (119 loc) · 3.57 KB
/
nosToken.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package api
import (
"crypto/md5"
"encoding/json"
"fmt"
"github.com/XiaoMengXinX/Music163Api-Go/types"
"github.com/XiaoMengXinX/Music163Api-Go/utils"
"path"
)
// NosTokenAPI 获取 NosToken API (用于文件上传)
const NosTokenAPI = "/api/nos/token/alloc"
// NosTokenReq 获取 NosToken 的 body json
type NosTokenReq struct {
Filename string `json:"filename"`
Local string `json:"local"`
NosProduct int `json:"nos_product"`
FileSize int `json:"fileSize"`
Type string `json:"type"`
Md5 string `json:"md5"`
Ext string `json:"ext"`
Header string `json:"header"`
ER string `json:"e_r"`
}
// CreateNosTokenReqJson 创建请求 body json
func CreateNosTokenReqJson(filePath string) (string, []byte, error) {
file, err := utils.ReadFile(filePath)
if len(file) <= 32 {
return "", file, fmt.Errorf("文件不能为空 ")
}
fileType, _ := utils.DetectFileType(file[:32])
size := len(file)
md5str := fmt.Sprintf("%+x", md5.Sum(file))
fileName := path.Base(filePath)
fileExt := path.Ext(filePath)
reqBody := NosTokenReq{
Filename: fileName,
Local: "false",
NosProduct: 0,
FileSize: size,
Md5: md5str,
Ext: fileExt,
Type: fileType,
Header: "{}",
ER: "true",
}
reqBodyJson, _ := json.Marshal(reqBody)
return string(reqBodyJson), file, err
}
// GetNosToken 获取 NosToken
func GetNosToken(data utils.RequestData, filePath string) (result types.NosTokenData, file []byte, err error) {
var options utils.EapiOption
options.Path = NosTokenAPI
options.Url = "https://music.163.com/eapi/nos/token/alloc"
reqBodyJson, file, err := CreateNosTokenReqJson(filePath)
if err != nil {
return result, file, err
}
options.Json = reqBodyJson
resBody, _, err := utils.EapiRequest(options, data)
if err != nil {
return result, file, err
}
err = json.Unmarshal([]byte(resBody), &result)
result.RawJson = resBody
return result, file, err
}
// MlogNosTokenReq 获取 NosToken 的 body json
type MlogNosTokenReq struct {
BizKey string `json:"bizKey"`
Filename string `json:"filename"`
Bucket string `json:"bucket"`
Md5 string `json:"md5"`
Type string `json:"type"`
FileSize int `json:"fileSize"`
Header string `json:"header"`
ER string `json:"e_r"`
}
// CreateMlogNosTokenReqJson 创建请求 body json
func CreateMlogNosTokenReqJson(filePath string) (string, []byte, error) {
file, err := utils.ReadFile(filePath)
if len(file) <= 32 {
return "", file, fmt.Errorf("文件不能为空 ")
}
fileType, _ := utils.DetectFileType(file[:32])
size := len(file)
md5str := fmt.Sprintf("%+x", md5.Sum(file))
fileName := path.Base(filePath)
reqBody := MlogNosTokenReq{
BizKey: string(utils.RandHex(8)),
Filename: fileName,
Bucket: "yyimgs",
Md5: md5str,
Type: fileType,
FileSize: size,
Header: "{}",
ER: "true",
}
reqBodyJson, _ := json.Marshal(reqBody)
return string(reqBodyJson), file, err
}
// GetMlogNosToken 获取用于 Mlog 的 NosToken
func GetMlogNosToken(data utils.RequestData, filePath string) (result types.NosTokenData, file []byte, err error) {
var options utils.EapiOption
options.Path = "/api/nos/token/whalealloc"
options.Url = "https://music.163.com/eapi/nos/token/whalealloc"
reqBodyJson, file, err := CreateMlogNosTokenReqJson(filePath)
if err != nil {
return result, file, err
}
options.Json = reqBodyJson
resBody, _, err := utils.EapiRequest(options, data)
if err != nil {
return result, file, err
}
err = json.Unmarshal([]byte(resBody), &result)
result.RawJson = resBody
result.Type = 1
return result, file, err
}