-
Notifications
You must be signed in to change notification settings - Fork 16
/
example_cloud_upload_test.go
180 lines (162 loc) · 4.77 KB
/
example_cloud_upload_test.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// MIT License
//
// Copyright (c) 2024 chaunsin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package example
import (
"fmt"
"io"
"os"
"path/filepath"
"testing"
"github.com/chaunsin/netease-cloud-music/api/weapi"
"github.com/chaunsin/netease-cloud-music/pkg/utils"
"github.com/dhowden/tag"
)
// TestCloudUpload 云盘上传.执行之前需要执行一次登录example_login_test.go
func TestCloudUpload(t *testing.T) {
api := weapi.New(cli)
// 1.读取文件
var (
filename = "../testdata/music/本兮 - 逢场作戏.mp3"
ext = "mp3"
bitrate = "999000"
// bitrate = "128000"
)
file, err := os.Open(filename)
if err != nil {
t.Fatal(err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
t.Fatal(err)
}
data, err := io.ReadAll(file)
if err != nil {
t.Fatalf("ReadAll: %v", err)
}
md5, err := utils.MD5Hex(data)
if err != nil {
t.Fatalf("MD5Hex: %v", err)
}
// 重新设置文件指针到开头
_, err = file.Seek(0, io.SeekStart)
if err != nil {
t.Fatalf("Seek: %v", err)
}
// 2.检查是否需要登录
if api.NeedLogin(ctx) {
t.Fatal("need login")
}
// 3.检查此文件是否需要上传
var checkReq = weapi.CloudUploadCheckReq{
Bitrate: bitrate,
Ext: ext,
Length: fmt.Sprintf("%d", stat.Size()),
Md5: md5,
SongId: "0",
Version: "1",
}
resp, err := api.CloudUploadCheck(ctx, &checkReq)
if err != nil {
t.Fatal(err)
}
t.Logf("CloudUploadCheck resp: %+v\n", resp)
if resp.Code != 200 {
t.Logf("CloudUploadCheck resp: %+v\n", resp)
}
// 4.获取上传凭证
var allocReq = weapi.CloudTokenAllocReq{
Bucket: "", // jd-musicrep-privatecloud-audio-public
Ext: ext,
Filename: filepath.Base(filename),
Local: "false",
NosProduct: "3",
Type: "audio",
Md5: md5,
}
allocResp, err := api.CloudTokenAlloc(ctx, &allocReq)
if err != nil {
t.Fatalf("CloudTokenAlloc: %v", err)
}
t.Logf("CloudTokenAlloc resp: %+v\n", allocResp)
if allocResp.Code != 200 {
t.Logf("CloudTokenAlloc resp: %+v\n", allocResp)
}
// 5.上传文件
if resp.NeedUpload {
var uploadReq = weapi.CloudUploadReq{
Bucket: allocResp.Bucket,
ObjectKey: allocResp.ObjectKey,
Token: allocResp.Token,
Filepath: filename,
}
uploadResp, err := api.CloudUpload(ctx, &uploadReq)
if err != nil {
t.Fatalf("CloudUpload: %v", err)
}
t.Logf("CloudUpload resp: %+v\n", uploadResp)
if uploadResp.ErrCode != "0" {
t.Logf("CloudUpload resp: %+v\n", uploadResp)
}
}
// 6.上传歌曲相关信息
metadata, err := tag.ReadFrom(file)
if err != nil {
t.Fatalf("ReadFrom: %v", err)
}
var InfoReq = weapi.CloudInfoReq{
Md5: md5,
SongId: resp.SongId,
Filename: stat.Name(),
Song: utils.Ternary(metadata.Title() != "", metadata.Title(), filepath.Base(filename)),
Album: utils.Ternary(metadata.Album() != "", metadata.Album(), "未知专辑"),
Artist: utils.Ternary(metadata.Artist() != "", metadata.Artist(), "未知艺术家"),
Bitrate: bitrate,
ResourceId: allocResp.ResourceID,
}
infoResp, err := api.CloudInfo(ctx, &InfoReq)
if err != nil {
t.Fatalf("CloudInfo: %v", err)
}
t.Logf("CloudInfo resp: %+v\n", infoResp)
if infoResp.Code != 200 {
t.Fatalf("CloudInfo: %v", infoResp)
}
// 7.对上传得歌曲进行发布,和自己账户做关联,不然云盘列表看不到上传得歌曲信息
var publishReq = weapi.CloudPublishReq{
SongId: infoResp.SongId,
}
publishResp, err := api.CloudPublish(ctx, &publishReq)
if err != nil {
t.Fatalf("CloudPublish: %v", err)
}
t.Logf("CloudPublish resp: %+v\n", publishResp)
switch publishResp.Code {
case 200:
t.Logf("上传成功: %s", filename)
case 201:
t.Logf("重复上传: %s", filename)
default:
t.Fatalf("上传失败: %s: %+v", filename, publishResp)
}
}